8

I have code where objects that are intended to execute in separate thread derive from a base class with a pure virtual Run function. I cannot get the following (simplified test code) to run the new thread.

#include <iostream>
#include <thread>
#include <functional>

class Base {
public:
    virtual void Run() = 0;
    void operator()() { Run(); }
};

class Derived : public Base {
public:
    void Run() { std::cout << "Hello" << std::endl; }
};

void ThreadTest(Base& aBase) {
    std::thread t(std::ref(aBase));
    t.join();
}

int main(/*blah*/) {
    Base* b = new Derived();
    ThreadTest(*b);
}

The code compiles fine (which is half the battle) but "Hello" never gets printed. If I was doing something wrong I'd expect a runtime error at some point. I'm using gcc.

Edit: The code above fails to compile on VS2012, with: error C2064: term does not evaluate to a function taking 0 arguments

You need to use a lambda instead of std::ref, i.e.

void ThreadTest(Base& aBase)
{
    std::thread t([&] ()
    {
        aBase.Run();
    });
    t.join();
}
UpAndAdam
  • 4,515
  • 3
  • 28
  • 46
James
  • 9,064
  • 3
  • 31
  • 49
  • This has nothing to do with the STL, which never had any threading support. The std::thread class is a C++ 11 feature. – Ulrich Eckhardt Jan 30 '13 at 17:40
  • I can't reproduce this using g++ 4.7.2. Seems to work fine for me. – NPE Jan 30 '13 at 17:59
  • 1
    Maybe the same problem as http://stackoverflow.com/questions/6485705/why-does-this-simple-stdthread-example-not-work Try to add -pthread to g++ command line. – Cyrille Ka Jan 30 '13 at 18:00
  • This works as expected on clang 3.3 with libc++. – Roman Kutlak Jan 30 '13 at 18:01
  • I don't understand? If the lib is not linked, should there not be linker errors, (there always is when I forget to link something)? – Martin James Jan 30 '13 at 18:04
  • 1
    @ckarmann you were right. There was the std::system_error being thrown but it wasn't displayed on the Code::Blocks terminal. It's there when I run it from a proper terminal. Add an answer if you want the points! – James Jan 30 '13 at 18:06
  • Please do not change the question if you already have an accepted answer. Asks a new question if you have any questions left. – Stephan Dollberg Jan 30 '13 at 20:40
  • @bamboon I changed it because that code above doesn't compile on VS2012. It might have saved someone a headache – James Jan 30 '13 at 20:56
  • No, you replaced a totally valid code snippet with one that contains a bug. (Not calling join) – Stephan Dollberg Jan 30 '13 at 20:59
  • @bamboon It would have been quicker for you to add that call to `join` than delete the whole thing – James Jan 30 '13 at 21:02
  • No, because it changes the intent of the question. Your new code snippet doesn't compile because it has syntax errors. – Stephan Dollberg Jan 30 '13 at 21:06
  • @bamboon The origianl code didn't compile on VS2012. The new one fixes that – James Jan 30 '13 at 21:30
  • No, it contains syntax errors. – Stephan Dollberg Jan 30 '13 at 21:32
  • 2
    @bamboon You are right. Fixed. You know, the whole point of this site is collaboration and knowledge sharing. You could have fixed it once you spotted it – James Jan 30 '13 at 21:46

1 Answers1

3

You need to add -pthread to g++ command line, as explained in this answer to a similar question: https://stackoverflow.com/a/6485728/39622.

Community
  • 1
  • 1
Cyrille Ka
  • 15,328
  • 5
  • 38
  • 58