C++0x: thread, gcc or my error?
Asked Answered
L

2

8

Is it GCC 4.7.0 or is it me? What do I do wrong?

This throws an std::system_error "operation not permitted" exception:

struct DumbFib {
    size_t operator()(size_t n) { return fib(n); }
    static size_t fib(size_t n) {
        return n<2 ? 1 : fib(n-2)+fib(n-1);
    }
};

void sample() {
    DumbFib dumbfib;
    thread th{ dumbfib, 35 };    // <- system_error!
    th.join();
};

while this works:

void work(size_t loop) {
    for(int l = loop; l>0; --l) {
        for(int i = 1000*1000; i>0; --i)
            ;
        cerr << l << "...";
    }
    cerr << endl;
}

int main() {
    //sample();
    thread t { work, 100 };     // <- fine
    t.join();
}

The difference is, of course:

  • The not-working code uses a Functor (class with operator())
  • The working code uses a function-pointer.

Do I use the functor wrong, somewhere? I can not see where, do you? Is it a hint that the gdb has this in its stack:

#7  ... in std::thread::_M_start_thread (..., __b=warning: RTTI symbol not found\
  for class 'std::_Sp_counted_ptr_inplace<std::thread::_Impl<std::\
  _Bind_simple<DumbFib()(int)> >, ..., (__gnu_cxx::_Lock_policy)2>

Notes: I also tried

  • Initialize DumbFib first, giving it a member-variable n_=35, same result.
  • Giving the functor directly with thread th{ DumbFib, 35 }; or thread th{ DumbFib{}, 35 };
Lair answered 9/9, 2011 at 9:24 Comment(2)
What system are you running this on?Anallise
linux 64bit (ubuntu 10.4 LTS), gcc-4.7.0, svn checkout from last week.Lair
A
13

When compiling your code with g++, use the -pthread option.

Anallise answered 9/9, 2011 at 18:51 Comment(1)
How embarrassing .. I had that error once and thought I corrected it. I didn't. So, now it works! Great!Lair
J
0

I was also facing similar problem, and thanks Jason, it solved my problem

The exact options will be

g++ code.cpp -lpthread -std=c++0x

That's what I have to do on g++ version 4.6.3

Joyann answered 11/6, 2013 at 17:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.