+ 1
Why thread are not started
Refer code below : Can anyone help me understand why thread id displayed on output is same for all ? https://code.sololearn.com/c376HUoS5a9L/?ref=app
6 Réponses
+ 4
It's because you call join right after it.
Join causes the program to wait until the thread has finished before it continues.
If you add a 2nd loop that joins you might have some crashes because of 2 threads accessing the same resource.
'A' and 'D', 'B' and 'E' share the same object, also rand() is not thread safe as far as I know, nor is cout.
+ 1
I assume you use some kind of variable to keep track when a thread has finished and then you do the next task.
I haven't implemented a thread pool yet myself but here is a library I found you could take a look at to see how they implement one.
https://github.com/bshoshany/thread-pool/blob/master/thread_pool.hpp
+ 1
Awhile back I played with thread pooling in this code.
https://code.sololearn.com/c6FuhkxWUITe/?ref=app
It's mess to read, and I don't remember much about what I was thinking at the time. It works, but I make no warranty about its robustness.
0
Thanks Dennis
I am least concerned about cout and rand as of now but thanks to bring it to my attention.
I updated the code and added thread joining in different loop.
Also reduced vector size and kept only three elements which is same as max number of threads.
What if I want to use that three threads as pool and want to use fourth value of vector once either of three is done with processing ?
0
Thanks
0
Thanks will go through it