+ 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

19th Mar 2022, 1:16 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
6 Respuestas
+ 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.
19th Mar 2022, 2:18 PM
Dennis
Dennis - avatar
+ 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
19th Mar 2022, 7:49 PM
Dennis
Dennis - avatar
+ 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.
19th Mar 2022, 10:48 PM
Brian
Brian - avatar
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 ?
19th Mar 2022, 4:01 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
Thanks
19th Mar 2022, 9:10 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
Thanks will go through it
20th Mar 2022, 5:52 AM
Ketan Lalcheta
Ketan Lalcheta - avatar