Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

instant segmentation fault with debian and std::thread c++

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.

like image 498
Wiciu Avatar asked Feb 20 '23 10:02

Wiciu


1 Answers

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

like image 79
user2k5 Avatar answered Mar 06 '23 10:03

user2k5