How can I distribute a program's processes between threads

I've been writing a brute force algorithm for a side hobby and I need some advice how I'd distribute code processes from different spawned threads. After reading TRPL over and over, I don't understand whether the "process" being parallelized are different functions or the main function.

I'd appreciate it if I got some advice on this :slightly_smiling_face:.

I'm not sure I follow your question. Generally when spawning threads, it runs the closure you pass to it.

std::thread::spawn(|| do_something())

This creates a new thread. On that thread, the provided closure is run. In this case, the closure just calls the do_something function and then exits.

Maybe this is a clearer example:

std::thread::spawn(||{
    let a = hello;
    println!("{a} world");
})

A closure is basically an unnamed function if that is unfamiliar to you.

1 Like

Can I spawn multiple threads from a closure?

I mean, any thread can spawn new threads. You just have to be careful. You might have a problem where unlimited threads spawn.

It is easier to just spawn new threads from the main thread.

I suggest you take a look at Rayon. It may or may not be for you but everyone developing in rust generally should know it exists.

1 Like

I'll start reading into Rayon! Thanks, was able to get a grasp of what the hell I'm trying to do

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.