Boost logo

Boost Users :

Subject: Re: [Boost-users] How to terminate a specific thread among other threads
From: Vicente J. Botet Escriba (vicente.botet_at_[hidden])
Date: 2012-04-15 09:00:13


Le 15/04/12 14:19, Master a écrit :
> Hello all .
> i have been trying to selectively dispose a thread among some other
> threads , but so far i wasnt successful in finding a way to do it .
> suppose i have a simple function which identifies any threads which
> gets into it and if a specific one is located ,it is disposed .
> simply sth like this :
>
Hi,
> #include <boost/signal.hpp>
> #include <iostream>
> #include <boost/thread.hpp>
> using namespace std;
> boost::thread threadarray[3];
>
> int t(int x)
> {
> boost::id id();
Do you mean boost::thread::id?
> switch(boost::this_thread::get_id())
boost::thread::id can not be used in a switch. :( You can use if instead.

In addition the access to threadarray is not thread-safe, so you can not
be sure it has been initialized as you could expect. You will need to
protect it (read/write access) with a mutex :(
> {
> case threadarray[0].get_id():
> cout<<"thread 1";
> break;
> case threadarray[1].get_id():
> *DISPOSE THIS THREAD or CHANGE OWNERSHITP, OR GET ANOTHER JOB TO IT ,
> OR CHECK IF IT WAS SUCCESSFUL OR NOT*
Could you clarify what exactly do you want to do if this is important
for you?
> break;
> case threadarray[3].get_id():
> cout<<"thread 3";
> break;
> default:
> cout<<"default";
> }
> return 0;
> }
>
> 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();
>
> for(int i = 0; i <3;i++)
> {
> threadarray[i].join();
> }
> system("pause");
> }
>
> i cant either use a switch statement to identify which thread im
> dealing with at the moment , nor can i find any means to keep track of
> my threads , and manage them in situations like this ( i already tried
> vector(boost::thread> my vect; and tries to use
> std::for_each(vect.begin(),vect.end(),somefunction_accepting
> boost::thread) , this doesnt work !! )
You need to use a move aware vector, e.f. boost::container::vector. If
you don't have access to them use

std::vector<thread*> threadarray;

or
boost::thread* threadarray[3];

Please, in the future, could you show what error have you getting, the
platform, the version ,... when you say that your code doesn't work so
that we can help you or improve the library if a bug is found?
>
> any idea how i can achieve sth similar?
>
HTH,
Vicente



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