Thread: C++0x::thread tools

  1. #1
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875

    C++0x::thread tools

    Greetings.
    I was recently playing with gcc 4.4.1 and trying to implement some simple code using the experimental support for std::thread in GCC. The only thing I found about support for it was passing -std=c++0x to g++ at compile time...which allows it to compile but even the simplest example segfaults when I go to create an instance of the thread class:
    Code:
    int testThread()
    {
       int nRC = 0;
       thread t(workFunc, "Foo", 10); // <<== segfaults here
       t.join();
       return nRC;
    }
    Now experience tells me that there must be some other lib to link (although I would hope that the compile would produce at least a warning here or something..) but not sure.

    There is nothing remarkable about workFunc() though I think the problem is happening before I get there. The call stack looks like this:
    Code:
    (gdb) where
    #0  _dl_fixup (l=<value optimized out>, reloc_arg=<value optimized out>)
        at dl-runtime.c:139
    #1  0x00f31fc0 in _dl_runtime_resolve () at ../sysdeps/i386/dl-trampoline.S:37
    #2  0x00e26d71 in std::thread::_M_start_thread(std::shared_ptr<std::thread::_Impl_base>) () from /usr/lib/libstdc++.so.6
    #3  0x0030b156 in thread<int (&)(std::string, int), char const (&) [4], int> (
        this=0xbffff398, __f=@0x30ad43, __args#0=..., __args#1=@0xbffff394)
        at /usr/include/c++/4.4/thread:133
    #4  0x0030aed5 in testThread () at /home/jeff/dev/threadcpp/foo.cpp:27
    #5  0x08048d38 in main (argc=1, argv=0xbffff4a4)
        at /home/jeff/dev/threadcpp/foo_main.cpp:47
    (gdb)
    Anyhow, unlike everyone else posting here this is not a rush, nothing is "due" I was just curious and wanted to play with some of the new concurrency functionality...

    Many thanks in advance

    Jeff
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Try adding "-pthreads" to the command line when linking (via g++, -lpthread otherwise).

    gg

  3. #3
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Quote Originally Posted by Codeplug View Post
    Try adding "-pthreads" to the command line when linking (via g++, -lpthread otherwise).

    gg
    Really? I would have thought it would confuse the compiler by trying to link in both libs (pthreads and the new "standard"). Will give it a go....tks
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  4. #4
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Wow, that did it...not sure if I am pleased or not, was expecting it to use some new code, not just pthreads...not that there is anything wrong w/pthreads, just thought we were getting new code...

    Well Tks.
    Last edited by jeffcobb; 01-28-2010 at 04:19 PM.
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    On Linux, pthreads is the "OS threading package" - which is what a std:: implementation would use under the hood.

    Perhaps in the future, it'll see that threads are being used and automatically link in what's needed. Though I don't know of precedence for this kind of behavior in gcc/g++.

    gg

  6. #6
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Quote Originally Posted by Codeplug View Post
    On Linux, pthreads is the "OS threading package" - which is what a std:: implementation would use under the hood.

    Perhaps in the future, it'll see that threads are being used and automatically link in what's needed. Though I don't know of precedence for this kind of behavior in gcc/g++.

    gg
    Yep that was the behavior I was expecting...I mean when was the last time you had to link (specifically) something that was platform-specific to use anything else in the std namespace? Me: never.


    Oh well. this is only given "experimental" status so I guess I should be glad/lucky that I get to play with any of this at all considering how far out the std is....

    Now to play with synchronization goodies....heh...the thrills are cheap around here today!
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. tools for static code analysis
    By lehe in forum C++ Programming
    Replies: 2
    Last Post: 03-18-2009, 12:41 PM
  2. A few interesting tools...
    By Mad_guy in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 03-10-2009, 06:07 PM
  3. GNU Auto build tools
    By jmd15 in forum Linux Programming
    Replies: 2
    Last Post: 07-03-2008, 09:19 AM
  4. Debugging tools for windows
    By 0x90 in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2006, 02:21 PM
  5. Code coverage tools
    By bswole in forum Linux Programming
    Replies: 2
    Last Post: 03-20-2002, 08:16 AM