Hi all!
I am building a shared library which uses Boost.thread internally. As a
result, Boost.system is also used since Boost.thread depends on that. My
shared library exports a C interface, so I want to hide all my internal
exception handling and thread usage etc from the end user. It is supposed to
be a black box so to speak. However, when I link with a client application,
while the program runs fine - as soon as it is time to stop the processing
by invoking a library function I get:
terminate called after throwing an instance of 'boost::thread_interrupted'
When building the shared library, I use static linking for both Boost.thread
and Boost.system so the outside world is never meant to see them. I am on
GCC 4.7 on Ubuntu 12. On Windows, I have no such problems (neither with MSVC
or MinGw).
Below is a minimalistic example that reproduces the problem.
Here first is the code for testlib.cpp and testlib.h.
testlib.cpp:
#include <boost/thread/thread.hpp>
void thread_func()
{
while(1)
{
boost::this_thread::interruption_point();
}
}
void do_processing()
{
// Start a thread that will execute the function above.
boost::thread worker(thread_func);
// We assume the thread started properly for the purposes of this example.
// Now let's interrupt the thread.
worker.interrupt();
// And now let's wait for it to finish.
worker.join();
}
And now testlib.h:
#ifndef TESTLIB_H
#define TESTLIB_H
void do_processing();
#endif
I build this into a shared library with the following command:
g++ -static-libgcc -static -s -DNDEBUG -I /usr/boost_1_54_0 -L
/usr/boost_1_54_0/stage/lib -Wall -shared -fPIC -o libtestlib.so
testlib.cpp -lboost_thread -lboost_system -lpthread -O3
Then, I have the code for a trivial client program which looks as follows:
#include "testlib.h"
#include <cstdio>
int main()
{
do_processing();
printf("Execution completed properly.\n");
return 0;
}
I build the client as follows:
g++ -DNDEBUG -I /usr/boost_1_54_0 -L ./ -Wall -o client
client.cpp -ltestlib -O3
When I run the client, I get:
terminate called after throwing an instance of 'boost::thread_interrupted'
Aborted (core dumped)
I am not explicitly catching the thread interruption exception, but
according to the Boost documentation Boost.thread does that and terminates
the given thread only. I tried explicitly catching the exception from within
the thread_func function, but that made no difference.
I tried including the -fexceptions flag when compiling the shared library,
as well as with and without -g and with and without -O3. I also tried with
an exception that is not thrown from Boost but rather from the same object
file that is actually meant to catch it. No luck. In short, absolutely no
exceptions are caught internally in the shared library even though I have
catch handlers for them.
Thanks very much in advance for any help.
Kind regards,
Philip Bennefall