Thread: std::thread segfault

  1. #1
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229

    std::thread segfault

    I just decided to start playing with my shiny new GCC-4.4's std::thread, and... well... it kept segfaulting on me .

    Admittedly I didn't RTFM, since TFM for C++0x is somewhat hard to find currently, and I assumed it's going to be very similar to boost::thread.

    Code:
    #include <thread>
    
    #include <unistd.h>
    
    void t() {
            sleep(5);
    }
    
    int main() {
            std::thread t1(&t);
            std::thread t2(&t);
    
            t1.join();
            t2.join();
    }
    Anyone wants to venture a guess at what might be wrong?

    Code:
    cyberfish@cyberfish-desktop:/tmp$ g++-4.4 -g -std=gnu++0x a.cpp
    cyberfish@cyberfish-desktop:/tmp$ gdb a.out
    GNU gdb 6.8-debian
    Copyright (C) 2008 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
    This is free software: you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
    and "show warranty" for details.
    This GDB was configured as "x86_64-linux-gnu"...
    (gdb) r
    Starting program: /tmp/a.out 
    
    Program received signal SIGSEGV, Segmentation fault.
    0x0000000000000000 in ?? ()
    (gdb) bt
    #0  0x0000000000000000 in ?? ()
    #1  0x00007fa9de939fa7 in std::thread::_M_start_thread (this=0x7fffe6da40e0, 
        __b=DWARF-2 expression error: DW_OP_reg operations must be used either alone or in conjuction with DW_OP_piece.
    )
        at /home/cyberfish/gcc_svn/gcc/build/x86_64-linux-gnu/libstdc++-v3/include/x86_64-linux-gnu/bits/gthr-default.h:682
    #2  0x0000000000400e6c in thread<void (*)()> (this=0x7fffe6da40e0, 
        __f=0x400c0c <t()>)
        at /opt/gcc/lib/gcc/x86_64-linux-gnu/4.4.0/../../../../include/c++/4.4.0/thread:134
    #3  0x0000000000400c38 in main () at a.cpp:10
    (gdb) l
    1	#include <thread>
    2	
    3	#include <unistd.h>
    4	
    5	void t() {
    6		sleep(5);
    7	}
    8	
    9	int main() {
    10		std::thread t1(&t);
    (gdb)
    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by cyberfish
    Admittedly I didn't RTFM, since TFM for C++0x is somewhat hard to find currently, and I assumed it's going to be very similar to boost::thread.
    Check out the standards committee website for the current working draft of C++0x.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Thanks!

    I read the part on std::thread, and still don't see anything wrong.

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Solution found on GCC mailing list -
    Timothy Brownawell - Re: std::thread segfault

    add "-pthread"

    Working great now!

    Code:
    #include <thread>
    #include <mutex>
    
    #include <iostream>
    
    #include <unistd.h>
    
    std::mutex io_mutex;
    
    void f(int id) {
    	for (int i = 0; i < 5; ++i) {
    		sleep(1);
    		{
    			std::lock_guard<std::mutex> lock(io_mutex); //scoped lock
    			std::cout << "T" << id << ":" << i << std::endl;
    		}
    	}
    }
    
    int main() {
    	std::thread t1(f, 0);
    	std::thread t2(f, 1);
    
    	t1.join();
    	t2.join();
    
    	return 0;
    }
    cyberfish@cyberfish-desktop:/tmp$ g++-4.4 -std=gnu++0x a.cpp -pthread
    cyberfish@cyberfish-desktop:/tmp$ ./a.out
    T0:0
    T1:0
    T0:1
    T1:1
    T0:2
    T1:2
    T0:3
    T1:3
    T0:4
    T1:4

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    I hate that "-pthread" flag. A lot of times, everything links and seems to work just fine without it. Other times it explodes. I can find no rhyme or reason to it.

    In this day and age, I'd prefer it if we had to explicitly specify "-no-pthread" instead of the other way around...
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segfault with additional variable?
    By misterFry in forum C++ Programming
    Replies: 11
    Last Post: 11-12-2008, 10:55 AM
  2. malloc() resulting in a SegFault?!
    By cipher82 in forum C++ Programming
    Replies: 21
    Last Post: 09-18-2008, 11:24 AM
  3. use of printf prevents segfault!
    By MK27 in forum C Programming
    Replies: 31
    Last Post: 08-27-2008, 12:38 PM
  4. Other programming questions: segfault in free
    By Neeharika in forum C Programming
    Replies: 2
    Last Post: 02-21-2006, 06:35 AM
  5. Segfault and Warning help
    By Uncle Rico in forum C Programming
    Replies: 1
    Last Post: 03-25-2005, 02:51 PM