Threads

Hello at all,

I'm new here. And I would like to write a Code so
the functions are computed parallel 2 or more functions.
If there only void Functions and there not changing an Member Variables of the
class its working with detach.
How would it work with a int or other function ?
or when I want to change a member variables ?
Thank you for your help
Best Regards
CHris
If you need to share a data between threads or to change a common variable, then synchronization with, for example, mutex must be used. It's not a very small topic with a lot of details, so I suggest reading some C++ book about multithreading. Shortly, to work with common struct you would need something like:

1
2
3
4
5
6
7
8
9
struct shared;
mutex m;

void thread()
{
    m.lock();
    ProcessStruct(shared);
    m.unlock();
}



If you remove mutex here, the struct shared will be corrupted.
Last edited on
Thanks for your answer
Could you recommend a good book?
Too much time passed since I learned multithreading so best I can do is to recommend searching amazon.com and reading contents/reviews. "C++ Concurrency in Action: Practical Multithreading" https://www.amazon.com/C-Concurrency-Action-Practical-Multithreading/dp/1933988770/ref=sr_1_sc_1?ie=UTF8&qid=1483185365&sr=8-1-spell has good reviews for example. Though special book for multithreading might be too much for your purposes, and most OS-specific programming books have a chapter on multithreading.
Last edited on
Topic archived. No new replies allowed.