4

got this problem - in title..

I have this code:

#include <thread>
#include <iostream>

void my_thread_func()
{
    std::cout<<"hello"<<std::endl;
}

int main()
{
    std::thread t(my_thread_func);
    t.join();
}

taken somewhere from web. compiler options -pthread -std=gnu++0x (also tried -std=c++0x) and I have segfault. All is on Debian on vmBox.. I have launched other codes before, and they worked. Suddenly I have segfault on threads with std::thread in all working apps.

EDIT: this is from gdb:

(gdb) where
#0  0x00000000 in ?? ()
#1  0x08048dc9 in thread<void (*)()> (this=0xbffff3fc, 
    __f=0x8048b9f <my_thread_func()>) at /usr/include/c++/4.4/thread:129
#2  0x08048bea in main () at ../test.cpp:18

(when I launch more advanced apps with std::thread t(&ClassName::my_thread_func,ptr) error is same, but other line [thread:133])

I was searching trough the web but I have not found nothing suitable.

Wiciu
  • 125
  • 1
  • 7

2 Answers2

7

compile with g++ -std=c++0x -lpthread. Note the l before pthread.

user2k5
  • 827
  • 1
  • 7
  • 9
1

Gcc 4.4 support for C++11 features is extremely limited, especially in the threading area. I have seen lots of thread related crashes in gcc 4.4 and 4.5 due to the library not being ready.

I am pretty confident that it is a compiler "bug"* and suggest that you upgrade to at least gcc 4.7.

*"bug" because gcc 4.4 never claimed to fully support std::thread, as such you can't really expect it to work.

edit: one thing that just came to my mind is, that sometimes linking statically to glibc and/or libpthread did not work in those old versions.

PlasmaHH
  • 15,673
  • 5
  • 44
  • 57