Boost logo

Boost Users :

Subject: Re: [Boost-users] How to terminate a specific thread among other threads
From: Igor R (boost.lists_at_[hidden])
Date: 2012-04-15 10:47:53


> Thanks for your meticulous observation :)
> so instead of just retyping lockguard inside all of if statements , one at
> the very beginning of the function in question would suffice right ? :)

Well, locking the whole thread function you actually prevent the
threads from running in parallel. You could just lock the shared
resource, copy it (if it's inexpensive), and release:
lock_guard<mutex> locker(mutex);
// copy id objects here to some local array
locker.unlock();
// proceed.

>> int main()
>> {
>>
>>     threadarray[0] = boost::thread(t,5);
>>     threadarray[1] = boost::thread(t,6);
>>     threadarray[2] = boost::thread(t,7);

You should lock the above as well, because when you're moving a thread
object to threadarray, the thread function is already being runnning.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net