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.
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::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 ??????
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::spawn(func, ?????);
}
I just want to know how to pass parameters to a thread function that i pass to spawn() ????????
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.
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.
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.
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.
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.
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.
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.
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