You have an error here:
boost::thread tWorker(&ThreadWorker, (i + 1));
vecThreads.add_thread(&tWorker);
You create a local object tWorker
that is deleted just after call to add_thread()
. So vecThreads contains the dangling pointers to thread
s. When you call vecThreads.interrupt_all()
you get undefined behavior because vecThreads
tries to access the deleted thread
objects and I suppose your program just terminates because of access violation or something.
You have to change your code to something like this:
boost::thread* ptWorker = new boost::thread(&ThreadWorker, (i + 1));
vecThreads.add_thread(ptWorker);
Please note that you don't need to delete
those thread
objects yourself. thread_group
will delete
them itself.
ADDITION:
The problem with terminate()
may be caused by destructor of http::client
throwing an exception. Please try this to possibly eliminate that problem in TestRequest()
:
try{
http::client client;
try{
// other code
}
catch (){}
}
catch(){}
Also I'd suggest resetting vecThreads
after interrupt_all()
. For example you can define it as boost::scoped_ptr
and then do pvecThreads.reset(new boost::thread_group())
after the call to interrupt_all()
.
At present the interrupted threads still remain in the thread_group
after the interruption and then you try to interrupt
them again along with the new threads added to the thread_group
later in ResetWorkerThreads()
.