
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 ? :) did your suggestions and the code is compiled just fine , and i get the expected behaviors :) this is the code i came up with so far : //in the name of GOD
//Seyyed Hossein Hasan Pour //Learning Boost Threading- How to end/terminate a thread //How to selectively specify a thread and terminate it
#include <boost/signal.hpp> #include <iostream> #include <boost/thread.hpp> using namespace std;
boost::thread threadarray[3]; boost::mutex mutex; int t(int someArg) { boost::lock_guard<boost::mutex> locker(mutex); if(threadarray[0].get_id() == boost::this_thread::get_id()) { std::cout<<boost::this_thread::get_id()<<"\t is thread 1 with arg "<<someArg<<std::endl; } else if(threadarray[1].get_id() == boost::this_thread::get_id()) { //Terminating the thread return 0; } else if(threadarray[2].get_id() == boost::this_thread::get_id()) { std::cout<<boost::this_thread::get_id()<<"\t is thread 3 with arg "<<someArg<<std::endl; } return 0; }
int main() {
threadarray[0] = boost::thread(t,5); threadarray[1] = boost::thread(t,6); threadarray[2] = boost::thread(t,7);
for(int i = 0; i <3;i++) { threadarray[i].join(); } system("pause"); return 0; }