简体   繁体   English

使用std :: thread从C ++ 11中的两个类调用函数

[英]Using std::thread to call functions from two Classes in C++11

I am trying to implement an API that should let the user create two communication channels in parallel. 我正在尝试实现一个API,该API应该允许用户并行创建两个通信通道。 One channel uses TCP and the other uses UDP. 一个通道使用TCP,另一个通道使用UDP。 I have two classes representing the two channels. 我有两个代表两个渠道的班级。 These classes have different functions implemented. 这些类实现了不同的功能。 I would like the functions from the two channels to run in parallel. 我希望两个通道中的函数并行运行。 For that I am using std::thread to create two threads, one for each channel (class). 为此,我使用std::thread创建两个线程,每个通道(类)一个。 The idea is the following following The header file looks like 这个想法是下面的头文件看起来像

class Channel_1
{
public:
     int myfunc(int a, int b);
};

class Channel_2
{
public:
    int anotherfunc(int a, int b);
};

In the main cpp file include the header file 在主cpp文件中包括头文件

int main()
{
  int a = 10, b = 20;
  Channel_1 ch1;
  Channel_2 ch2;

  std::thread t(ch1.myfunc, a,b);
  return 0;
}

I get the error saying that no instance of the constructor std::thread exists. 我收到错误消息,指出不存在构造函数std::thread实例。

I have the following questions. 我有以下问题。

  1. Can't we call the functions from a class in the thread constructor? 我们不能从线程构造函数中的类调用函数吗?
  2. Does this idea of using threads for calling functions from different classes make sense? 使用线程从不同类调用函数的这种想法有意义吗?

You actually have two problems: 您实际上有两个问题:

  1. The syntax is wrong. 语法错误。 You need to pass a pointer to the member function, as in 您需要传递一个指向成员函数的指针,如

     std::thread t(&Channel_1::myfunc, a, b); 
  2. Non-static member functions needs to be called on an instance of the class. 非静态成员函数需要在类的实例上调用。 This instance must be passed as the first argument: 该实例必须作为第一个参数传递:

     std::thread t(&Channel_1::myfunc, ch1, a, b); 

As Some programmer dude said, you need to supply a pointer to the function (either a static or non-static). 就像一些程序员所说的那样,您需要提供一个指向函数的指针(静态或非静态)。 A third option could be to make a base class that implements a start() method that uses this thread constructor: 第三种选择是制作一个基类,该基类实现使用此线程构造函数的start()方法:

template< class Function, class... Args > 
explicit thread( Function&& f, Args&&... args );

Example using a lambda. 使用lambda的示例。 It's not meant to be a complete working example, It's only meant to show how the third thread constructor could be used. 它并不意味着是一个完整的工作示例,这只是意在显示第三线的构造如何被使用。

class thread_base {
private:
    std::thread m_th;
    bool m_terminated;

    void proxy() {
        // in the new thread
        // add thread_base setup here if needed and
        // call the execute() method in the derived class
        execute();
    }
public:
    thread_base() : m_th(), m_terminated(false) {}
    thread_base(const thread_base&) = delete;
    thread_base& operator=(const thread_base&) = delete;
    thread_base(thread_base&&) = default;
    thread_base& operator=(thread_base&&) = default;
    virtual ~thread_base() { join(); }

    virtual void start() {
        if(joinable()) throw std::runtime_error("thread already running");
        else m_th = std::thread( [this] { proxy(); } );
    }

    inline bool joinable() { return m_th.joinable(); }
    inline void join() { if(joinable()) { m_th.join(); m_terminated=false; } }
    inline void terminate() { m_terminated=true; }
    inline bool terminated() { return m_terminated; }

    virtual void execute() = 0; // implement this in your derived classes
};


class Channel_1 : public thread_base {
    void execute() {
        // runs in a thread
        // with a volontary check if a soft termination
        // request has been issued
        while( !terminated() ) {
            // work
        }
    }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM