Why is it spawned each time? Thread is a "thread of execution". When you launch it in c/c++, it starts in the beginning of some function and ends when the function ends. So if you return from the thread function, this particular thread exits.
thanks , thats great .:) i though the only way to terminate a thread is to use interrupts and an un-handled exception generated by sleep() :) by the way it is the source code whith my latest knowledge . it now compiles fine thanks to you guys , kills a thread , and then specifically i can manage each thread i want . 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? //in the name of GOD
//Seyyed Hossein Hasan Pour //Learning Boost Threading //How to selectively specify a thread and terminate it
#include
#include <iostream> #include using namespace std; boost::thread threadarray[3]; boost::mutex mutex; int t(int x) { if(threadarray[0].get_id() == boost::this_thread::get_id()) { boost::lock_guardboost::mutex locker(mutex); std::cout<<"thread 1"; } else if(threadarray[1].get_id() == boost::this_thread::get_id()) { return 0; } else if(threadarray[2].get_id() == boost::this_thread::get_id()) { boost::lock_guardboost::mutex locker(mutex); std::cout<<"thread 3"; } 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"); return 0; }