Thread and interfaces C++

1.3k views Asked by At

I have some issue to create different threads using interfaces and factory:

I have two interfaces that are derived (here by one class but eventually more..). I use a factory to create an object of the desired derived class. As I run them within different threads, I used what the factories return me to give as parameter of the thread constructor.

#include <iostream>
#include <thread>

class Base
{
    public:
        virtual ~Base () {}

        virtual void operator () () = 0;
};


class Derived : public Base
{
    public:
        virtual void operator () ()
        {
            std::cout << "Derived of Base!" << std::endl;
        }
};

enum BaseType
{
    derived = 1000
};

class BaseFactory
{
    public:
        static Base *createBase (BaseType bt)
        {
            switch (bt)
            {
                case derived:
                    return new Derived;
                default:
                    return NULL;
            }
        }
};


class OtherBase
{
    public:
        virtual ~OtherBase () {}

        virtual void operator () () = 0;
};

class OtherDerived : public OtherBase
{
    public:
        virtual void operator () ()
        {
            std::cout <<  "OtherDerived of OtherBase!" << std::endl;
        }
};

enum OtherBaseType
{
    otherderived = 1100
};

class OtherBaseFactory
{
    public:
        static OtherBase *createOtherBase (OtherBaseType obt)
        {
            switch (obt)
            {
                case otherderived:
                    return new OtherDerived;
                default:
                    return NULL;
            }
        }
};



int main (int argc, const char *argv[])
{
    Base *pBase = BaseFactory::createBase(derived);
    OtherBase *pOBase = OtherBaseFactory::createOtherBase(otherderived);

    std::thread *t1 = new std::thread ((*pBase)());
    std::thread *t2 = new std::thread ((*pOBase)());

    t1->join();
    t2->join();

    delete t1;
    delete t2;

    return 0;
}

When compiling, I have an issue at the creation of each thread:

test.cxx:87:50: error: invalid use of void expression
test.cxx:88:51: error: invalid use of void expression

I believe the problem comes from I give as parameter to create threads object of type Base and OtherBase (thus the interfaces). However I don't really know how to solve this problem.

2

There are 2 answers

2
nneonneo On BEST ANSWER

std::thread's constructor can take a member function pointer as the first argument, and will automatically dereference and call the member function pointer on the second argument.

Therefore, you can write:

std::thread *t1 = new std::thread (&Base::operator(), pBase);
std::thread *t2 = new std::thread (&OtherBase::operator(), pOBase);

This is probably simpler than using std::bind.

1
Dmitry Poroh On

As compiler said - you asking to create thread with argument set to void. This is because you call the object function (operator()) by (*pBase)().

I suppose you need create a function that bound to the appropriate object. You can do it with std::bind like this:

std::bind(&Base::operator(), pBase)

So the thread creation should look like:

std::thread *t1 = new std::thread (std::bind(&Base::operator(), pBase));