Passing parameters to a thread function


#1

Dear Gurus, please help me to find out if it is possible to pass parameters for thread function.
For example: // C++ code
void func(int x,int y)
{
std::cout<<x+y<<std::endl;
}
void main()
{
std::thread Ex=new std::thread(func,5,5);
*Ex.join()
}

I have searched the enet and havent found any information on passing parameters to thread function in Rust.


#2
let the_text = "abc";
let thread = ::std::thread::spawn(|| {
    println!("The text is: {}", the_text);
});
thread.join().unwrap();

#3

Thanks.
But here is another question:
Do i always have to spawn the closures???
Because i tried to spawn a fn and it worked:
fn func()
{
println!(“Thread function”);
}
fn main(){
let thr=std::thread::spawn(func);
thr.join();
}
and it also works.
I think it is pretty uncomfortable to describe a closure with parameters in a spawn() method.
Is there a way in Rust to pass a fn and its parameters to operate with in spawn method ???


#4

No. you’re not required to always use closures.

The definition of std::thread::spawn() is as follows:

pub fn spawn<F, T>(f: F) -> JoinHandle<T> 
    where
    F: FnOnce() -> T,
    F: Send + 'static,
    T: Send + 'static, 

The argument you’re passing, f, is the function. The function, when called, should return a T, which you then receive wrapped in a JoinHandle.

See the doc page for std::thread::spawn() and std::marker::Send for further details. In general, you’ll be able to pass it a function most of the time.


#5

Thanks.
Could you please give me an example of spawn() with parameters for thread method that is passing to spawn(). ???
For example:
fn func(x:i32,y:i32)
{
println!("{}",x+y);
}

fn main()
{
std::thread::spawn(func, ???);
}
I just want to know how to pass parameters to a thread function that i pass to spawn() ???


#6

The closure given to spawn is more like a C++ callable, rather than a function pointer. Your example would be:

fn func(x: i32, y: i32) {
    println!("{}", x + y);
}

fn main() {
    let h = std::thread::spawn(|| func(5,5));
    h.join();
}

#7
std::thread::spawn(|| func(1, 2));

There is no other way to pass parameters. It would require some kind of variable generics, which Rust doesn’t have.


#8

Dear Vitalyd,
Thanks very much.
To be honest i turned out to be dummy not to guess that i can call a function with params in a closure, even though i practiced calling fns in a closure.


#9

Another thing i want to ask is:
Is it possible to declare a fn that accept a Macros as one of parameter. If it is what is the syntax then. I havent managed to find any information about it anywhere so far.


#10

Macros only happen at parse time; they’re gone by runtime, so cannot be passed to a function.

(Similarly, if you #define MAX(a,b) ((a)>(b)?(a):(b)) in C++ you can’t pass MAX to a function.)


#11

Thank you Sir.
Does it mean that there is neither reference nor pointer to a Macros because it cannot be interpreted as Type, right ??


#12

That’s correct. At runtime, there’s nothing left of macros.


#13

BTW, if you put your code in ``` It will get pretty printed into a formatted code block. Add the name of the language and you may get coloring as well.


#14

Dear Pros. Please help to figure out what to do to use @Type and ~Type.
Also it would be great to get any ref to info about working with files using Rust especially using traits ReaderUtil and WriterUtil.


#15

I suspect you’re looking at some old version of Rust. What exactly do you want to do?

Also, please start a new thread if you’re switching gears.


#16

Sir I want to use Pointers to @ and ~ blocks of memory as is written in an article. But using such syntax doesnt work, nor works the code writing and reading in and from the file.


#17

#18

Right - the ~ and @ modifiers don’t exist in Rust today - you’re looking at a very old article on Rust. Please start a new topic/thread to discuss whatever question you have on reading/writing from/to files.


#19

Dear fellows.
Could you please help to find out how can i pass a mutable reference to objects among the threads.
I want to change values of vector in another thread for example,
the book says that i can use Arc but i cant emagine the right syntax.


#20

let mut ptd=Arc::new(vec![5,6,7,8]);
thread::spawn(move||{ for i in 0…ptd.len(){
ptd[i]+=10;
}}).join();

error[E0596]: cannot borrow immutable borrowed content as mutable
ptd[i]+=10;
| ^^^ cannot borrow as mutable
thread::spawn(move||{ for i in 0…ptd.len(){
| ------ value moved (into closure) here

33 | for i in 0…ptd.len(){
| ^^^ value used here after move