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 09:45:59


> but there is a problem , the first thread doesnt print anything on the cosole ! while the others do! i even used mutex
> , but seems its not the case  . any idea?

Your code is not thread-safe. See my comments below.

>> int t(int x)
>> {
>>     if(threadarray[0].get_id() == boost::this_thread::get_id())

Here and below in this thread function you access "threadarray"
without protecting it with mutex.
<...>

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

Here you write to "threadarray" without protecting it with a mutex.
Besides, some (or all) the threads that you launched above might have
been already executed their thread functions and exited - before you
have a chance to store their objects in "threadarray".


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