
I'm wondering what the best approach is to signal a thread to exit. Just allocate a boolean variable for each thread and pass the address to
Soren Dreijer wrote:
the thread starting function. The loop of the thread breaks if the flag
is reset; the thread exits. This scheme is so simple that it doesn't
even require synchronization beyond making the pointer the thread
accesses volatile.
You could use a shared_ptr to manage the memory, but personally I would
simply use a scoped_ptr in the thread's main function.
The following code is conceptual. I can't remember the exact API of
Boost.Threads.
void thread_proc(volatile bool *flag)
{
scoped_ptr<bool> guard(flag);
while(*flag) {
// Work
}
}
map