|
Boost Users : |
Subject: [Boost-users] Boostlog and ASIO crash in shared library
From: Ray Cheng (ray_at_[hidden])
Date: 2014-09-16 03:08:07
Hi,
My project use a simple main using dlopen to "run" the real main function
in target shared library. I use both Boost Log and ASIO (for threadpool) in
the project. In Linux I found the ASIO got weird behavior with some
un-initialized variables and crashing all the time. I am able to trim down
my problem into a small program as below:
// *** threadpool.cpp ***
#include <iostream>
#include <memory>
#include <boost/asio/io_service.hpp>
#include <boost/bind.hpp>
#include <boost/thread/thread.hpp>
void print() {
std::cout << "print" << std::endl;
}
struct ThreadPool
{
void start()
{
boost::asio::io_service ioService;
boost::thread_group threadpool;
std::unique_ptr<boost::asio::io_service::work> work =
std::make_unique<boost::asio::io_service::work>(ioService);
threadpool.create_thread(boost::bind(&boost::asio::io_service::run,
&ioService));
for (int i=0; i<100; ++i)
{
ioService.post(&print);
}
std::cout << "Post all calculation tasks" << std::endl;
work.reset();
threadpool.join_all();
ioService.stop();
std::cout << "Finished all calculation tasks" << std::endl;
}
};
extern "C" void createthreadpool()
{
ThreadPool pool;
pool.start();
}
// *** threadpool.cpp ***
g++ -O0 -std=gnu++1y -fPIC -c -g -DLinux -DGNU_GCC -I${BOOST_HOME}/include
threadpool.cpp
g++ -shared -o libthreadpool.so threadpool.o -L${BOOST_HOME}/lib
-lboost_system -lboost_date_time -lboost_thread -lboost_filesystem
-lboost_regex -lboost_chrono -ldl -lstdc++
// *** main.cpp ***
#include <dlfcn.h>
#include <iostream>
int main(int argc, char** argv) {
typedef void* createProcess();
auto service = dlopen("libthreadpool.so", RTLD_LAZY);
createProcess* make = reinterpret_cast<createProcess*>(dlsym(service,
"createthreadpool"));
std::cout << "dlsym" << std::endl;
make();
return 0;
}
// *** main.cpp ***
g++ -O0 -std=gnu++1y -fPIC -c -g -DLinux -DGNU_GCC main.cpp
// Link against boost_log to simulate my problem (although it is not using
any boost_log). This version will crash
g++ -o main.bad main.o -L${BOOST_HOME}/lib -lboost_system -lboost_date_time
-lboost_thread -lboost_filesystem -lboost_regex -lboost_chrono -lboost_log
-ldl -lstdc++
// it works fine If link against libthreadpool, surprising me
g++ -o main main.o -L/home/sysmmbuild/dev/boost -lthreadpool
-L${BOOST_HOME}/lib -lboost_system -lboost_date_time -lboost_thread
-lboost_filesystem -lboost_regex -lboost_chrono -lboost_log -ldl -lstdc++
One more finding is, if compile boost_log without thread support, the
main.bad also working fine.
I have attached the code and make.sh as make script.
Software version
Redhat enterprise 6.5
g++ 4.9.1
Boost 1.56.0
Rgds,
Ray
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net