How to use boost_threads with clang?

I am having trouble using boost_threads with clang. The clang version is 3.6.0 and the version 1.55.0 is the new Ubuntu 15.04. The program that was used to work with previous versions of clang now segfaults at startup. No problem using g ++ instead.

Here is a sample program to illustrate a point.

#include <iostream>
#include <boost/thread.hpp>

using namespace std;

void output() {
  try {
    int x = 0;
    for (;;) {
      boost::this_thread::sleep(boost::posix_time::milliseconds(100));
      cerr << x++ << endl;
    }
  } catch (boost::thread_interrupted&) {}
}

int main(int argc, char* argv[]) {
  try {
    boost::thread output_worker(output);
    boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
    output_worker.interrupt();
    output_worker.join();
  } catch (...) {
    cerr << "Unexpected error!" << endl;
    exit(1);
  }
}

      

If I compile it with g ++ it works, i.e.

g++ thread.cpp -lboost_thread -lboost_system

      

If I compile it with clang

clang++ thread.cpp -lboost_thread -lboost_system

      

I am getting segfault with gdb trace below

Starting program: /home/dejan/test/a.out 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7bd0580 in boost::exception_ptr boost::exception_detail::get_static_exception_object<boost::exception_detail::bad_alloc_>() ()
   from /usr/lib/x86_64-linux-gnu/libboost_thread.so.1.55.0
(gdb) bt
#0  0x00007ffff7bd0580 in boost::exception_ptr boost::exception_detail::get_static_exception_object<boost::exception_detail::bad_alloc_>() ()
   from /usr/lib/x86_64-linux-gnu/libboost_thread.so.1.55.0
#1  0x00007ffff7bcb16a in ?? () from /usr/lib/x86_64-linux-gnu/libboost_thread.so.1.55.0
#2  0x00007ffff7de95ba in call_init (l=<optimized out>, argc=argc@entry=1, argv=argv@entry=0x7fffffffdf98, env=env@entry=0x7fffffffdfa8)
   at dl-init.c:72
#3  0x00007ffff7de96cb in call_init (env=<optimized out>, argv=<optimized out>, argc=<optimized out>, l=<optimized out>) at dl-init.c:30
#4  _dl_init (main_map=0x7ffff7ffe188, argc=1, argv=0x7fffffffdf98, env=0x7fffffffdfa8) at dl-init.c:120
#5  0x00007ffff7dd9d0a in _dl_start_user () from /lib64/ld-linux-x86-64.so.2
#6  0x0000000000000001 in ?? ()
#7  0x00007fffffffe2fe in ?? ()
#8  0x0000000000000000 in ?? ()

      

Am I doing something wrong?

+3


source to share


1 answer


Compiling with use clang -std=c++11

allows you to improve your internal implementation and actually solves the segmentation problem. It's not a perfect solution, but I'll go with our code.



+1


source







All Articles