
hp.shen@hyaptech.com wrote:
We can create a thread very easily. While, I don't know how to suspend and then resume it.
As far as I'm aware, Boost.Thread doesn't provide any way to asynchronously suspend and resume threads from other threads. Neither does the proposed standard threads library. In POSIX, there are no pthread_* calls to do this. Solaris apparently has/had thr_suspend() and thr_resume(). LinuxThreads may work if you send SIGSTOP and SIGCONT to the thread, but POSIX says that they should affect the whole process and I guess that that is what NPTL (the newer Linux pthreads implementation) does. Does anyone know what Windows has? One possible implementation, for POSIX systems, is to have a condition variable per thread and a signal handler. To suspend a thread, send it the signal; the signal handler waits on the condition. To resume the thread, wake the condition. I think that that could be done on top of the existing Boost.Thread or the proposed std::threads, though maybe some sort of hook is needed so that the signal handler can be set up when a new thread is created. But in general, I suspect that suspend()/resume() is used to implement "do-it-yourself scheduling". Wouldn't it be better to instead let the operating system do the scheduling, and to just change the thread priorities as necessary? i.e. replace thread_suspend() with set_priority(low) and thread_resume() with set_priority(high). Of course, Boost.Threads and std::threads don't have functions for that either. POSIX has functions to do this; presumably they could be used with std::threads using the "native handle" functionality. Can they be used with Boost.Threads? Is there enough commonality between Windows and POSIX to do something cross-platform? Regards, Phil.