lundi 20 avril 2015

C++11 Multithreading with a shared object [duplicate]

This question already has an answer here:

In C++11, I want to create objects of classes Foo and Alpha (foo and alpha, respectively). I then want to create a thread which calls a function of alpha, passing foo as the argument. Whilst this thread runs, I then want to be able to modify foo.

Here's my code so far:

#include <thread>
#include <iostream>

class Foo
{
public:
    int x;
    void Bar()
    {
        std::cout << x << std::endl;
    }
};

class Alpha
{
public:
    void Beta(Foo& foo)
    {
        while (true)
        {
            foo.Bar();
        }
    }
};

int main()
{
    Foo foo;
    foo.x = 5;
    Alpha alpha;
    std::thread beta_thread(&Alpha::Beta, alpha, foo);
    beta_thread.join();

    while (true)
    {
        foo.x++;
    }

    return 0;
}

However, this gives me a compile error:

/usr/include/c++/4.8/functional:1697: error: no type named 'type' in 'class std::result_of<std::_Mem_fn<void (Alpha::*)(Foo&)>(Alpha, Foo)>'
       typedef typename result_of<_Callable(_Args...)>::type result_type;
                                                             ^

If I pass foo by value rather than reference (void Beta(Foo foo)), then it compiles ok. However, I believe that I need to pass by reference so that when I modify foo in the main loop, it will change the same instance that I passed to Beta.

Any help?

Aucun commentaire:

Enregistrer un commentaire