I am trying to setup my application so that it runs as a daemon. One aspect that I need to run a sanity check on is to determine if there are currently any parent threads running. Currently this is what I am testing with, when all threads are finish running, I then join all the threads to terminate them. Doing so should give me a thread group size of 0. However, this is now the case. Currently after I join the threads, the thread group size is still 0. Here is my current test code that I am working with. Any help with this is greatly appreciated. int main(int argc, char *argv[]) { InitConfig mconfig; if(mconfig.setConfig()){ boost::thread_group Tg; cout << "Thread Size Before: " << Tg.size() << endl; // DISPLAYS 0 for(int i = 1; i < 25; i++){ dmParse parse; Tg.create_thread(boost::bind(&dmParse::onInit,parse,i)); } cout << "Thread Size: " << Tg.size() << endl; // DISPLAYS 24 Tg.join_all(); cout << "Thread Count After:" << Tg.size() << endl; // DISPLAYS 24 }
On Tue, Apr 7, 2009 at 14:45, Jeremy Rottman
Currently this is what I am testing with, when all threads are finish running, I then join all the threads to terminate them. Doing so should give me a thread group size of 0. However, this is now the case. Currently after I join the threads, the thread group size is still 0 [sic? 24, probably].
Given that create_thread returns a pointer to the new thread, it makes since that join_all wouldn't remove the threads, since then the pointer would dangle. I don't know the best way to do what you want, though.
Anyone have any insight as to why these threads are not being terminated? I
have been doing a lot of different tests to determine where it might be
going wrong. And thus far, I have not been able to find exactly where or why
the threads will not join properly.
Here is my my recent code.
int sayHello(int x){
cout << "Hello world from thread: " << x << endl;
}
int main(int argc, char *argv[]) {
InitConfig mconfig;
if(mconfig.setConfig()){
boost::thread_group Tg;
// int pid;
//
// if(fork()) return 0;
//
// chdir("/");
// setsid();
// umask(0);
//
// pid = fork();
//
// if(pid){
// printf("Daemon Started: %d\n", pid);
// return 0;
// }
//while(1){
//if(Tg.size() == 0){
cout << "Thread Size Before: " << Tg.size() << endl;
for(int i = 1; i < 25; i++){
dmParse parse;
//Tg.create_thread(boost::bind(&dmParse::onInit,parse,i));
Tg.create_thread(boost::bind(sayHello,i));
}
cout << "Thread Size: " << Tg.size() << endl;
Tg.join_all();
Tg.interrupt_all();
cout << "Thread Count After:" << Tg.size() << endl;
//}else{
//cout << "Threads Running, taking a nap. Thread Count: " <<
Tg.size() << endl;
//sleep(60);
//}
//}
}
}
On Tue, Apr 7, 2009 at 11:54 AM, Scott McMurray
On Tue, Apr 7, 2009 at 14:45, Jeremy Rottman
wrote: Currently this is what I am testing with, when all threads are finish running, I then join all the threads to terminate them. Doing so should
give
me a thread group size of 0. However, this is now the case. Currently after I join the threads, the thread group size is still 0 [sic? 24, probably].
Given that create_thread returns a pointer to the new thread, it makes since that join_all wouldn't remove the threads, since then the pointer would dangle.
I don't know the best way to do what you want, though. _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Jeremy Rottman wrote:
Anyone have any insight as to why these threads are not being terminated? I have been doing a lot of different tests to determine where it might be going wrong. And thus far, I have not been able to find exactly where or why the threads will not join properly.
[code that won't compile snipped] a) What makes you think that the threads aren't being terminated? b) It would be a lot more helpful if you provide code that compiles doesn't have dependencies others are unlikely to have. A full set of includes at the top would also have helped. After modifying your code I'm not sure what your problem is. join_all/interrupt_all will not remove threads from the thread group. n
What I am trying to do is remove threads from the thread group after
they have run. So that when my daemon iterates it will start new threads
if none exist.
Here is my current code modified so that it will build without any extra
dependencies.
#include
Jeremy Rottman wrote:
Anyone have any insight as to why these threads are not being terminated? I have been doing a lot of different tests to determine where it might be going wrong. And thus far, I have not been able to find exactly where or why the threads will not join properly.
[code that won't compile snipped]
a) What makes you think that the threads aren't being terminated?
b) It would be a lot more helpful if you provide code that compiles doesn't have dependencies others are unlikely to have.
A full set of includes at the top would also have helped.
After modifying your code I'm not sure what your problem is.
join_all/interrupt_all will not remove threads from the thread group.
n
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Jeremy Rottman wrote:
What I am trying to do is remove threads from the thread group after they have run. So that when my daemon iterates it will start new threads if none exist.
Okay, it sounds to me as if you are confusing the thread's existence with its state. Just because a thread has completed its task and is no longer running does not mean it does not exist, there is still a C++ object on the heap that holds some state, and this object is still referenced by the thread_group. If you want threads to be removes from the thread group once they have completed their task you will have to do this yourself, the thread_group class doesn't provide this for you. The way in which you go about keeping track of the threads will really depend on what you're doing with them. Regards, Nige
On Wed, Apr 8, 2009 at 13:44, Jeremy Rottman
What I am trying to do is remove threads from the thread group after they have run. So that when my daemon iterates it will start new threads if none exist.
Why would you want to close down the threads and re-open them? Why not just leave the threads running and have them pull tasks from a queue? Unless your hardware is quite special, you don't normally want 25 active threads anyway...
The main reason I want to remove the thread is when the daemon iterates, I do not want it to create new threads if threads are currently running. I am currently testing with 25 threads, the total thread count is dynamic. To sum it up, if threads are active and running, I do not want new threads to be created until no threads are active. The issue I am having is trying to determine if there are active threads still running code. Scott McMurray wrote:
On Wed, Apr 8, 2009 at 13:44, Jeremy Rottman
wrote: What I am trying to do is remove threads from the thread group after they have run. So that when my daemon iterates it will start new threads if none exist.
Why would you want to close down the threads and re-open them? Why not just leave the threads running and have them pull tasks from a queue?
Unless your hardware is quite special, you don't normally want 25 active threads anyway... _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
On Wed, Apr 8, 2009 at 14:30, Jeremy Rottman
The main reason I want to remove the thread is when the daemon iterates, I do not want it to create new threads if threads are currently running. I am currently testing with 25 threads, the total thread count is dynamic. To sum it up, if threads are active and running, I do not want new threads to be created until no threads are active. The issue I am having is trying to determine if there are active threads still running code.
After join_all() has returned, there are no "active threads still running code." This in no way implies a result of 0 from size(). It seems you're doing this: thread_group tg; while (something) { create_some_threads(tg, ...); tg.join_all(); } and are disappointed that !tg.empty() when the loop restarts. Have you considered just doing this? while (something) { thread_group tg; create_some_threads(tg, ...); tg.join_all(); }
participants (3)
-
Jeremy Rottman
-
Nigel Rantor
-
Scott McMurray