线程和接口C ++ [英] Thread and interfaces C++

查看:214
本文介绍了线程和接口C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些问题使用接口和工厂创建不同的线程:

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;
}

在编译时,我在创建每个线程时遇到问题: p>

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


$ b $ p

我相信问题来自我给参数创建线程对象类型Base和OtherBase(因此是接口)。
但是我真的不知道如何解决这个问题。

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.

推荐答案

std: :thread 的构造函数可以接受一个成员函数指针作为第一个参数,并将自动解引用并调用第二个参数的成员函数指针。

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.

因此,您可以写:

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

这可能比使用 std :: bind

这篇关于线程和接口C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆