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 13:37:48


> i should lock that too? but those threads are somewhere else ! only main thread is doing the assignment!

a) the main thread modifies "threadarray" object;
b) the other threads read "threadarray" object (technically speaking,
they might modify it as well because of vector's operator[] nature).
You should synchronize (a) and (b) - using mutex.

> would locking pause all these threads ?

It will pause, if you do as follows:
int main()
{
  lock_guard<mutex> lock(mutex);
  threadarray[0] = boost::thread(t,5);
  threadarray[1] = boost::thread(t,6);
  threadarray[2] = boost::thread(t,7);
  lock.unlock();
  //...
}

int t(int x)
{
  lock_guard<mutex> lock(mutex);
  // access threadarray
  lock.unlock()
  //...
}

This way the thread function t() accesses threadarray only after it's
filled in main().

> and what do you mean by :
>> // copy id objects here to some local array
>  whats your meaning about id objects ?

thread::get_id() and this_thread::get_id() return you thread::id instances:
http://www.boost.org/doc/libs/1_49_0/doc/html/thread/thread_management.html#thread.thread_management.thread.id

> whats the local array ? an array declared inside that function ?

Yes.

> (would be shared among other threads too right?

No. Local objects are not shared between threads.


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