【发布时间】:2019-04-16 09:31:15
【问题描述】:
我正在尝试实现一个 API,让用户可以并行创建两个通信通道。一个通道使用 TCP,另一个使用 UDP。我有两个类代表两个通道。这些类实现了不同的功能。我希望两个通道的功能并行运行。为此,我使用std::thread 创建两个线程,一个用于每个通道(类)。
思路如下
头文件看起来像
class Channel_1
{
public:
int myfunc(int a, int b);
};
class Channel_2
{
public:
int anotherfunc(int a, int b);
};
在主 cpp 文件中 包含头文件
int main()
{
int a = 10, b = 20;
Channel_1 ch1;
Channel_2 ch2;
std::thread t(ch1.myfunc, a,b);
return 0;
}
我收到错误消息,指出不存在构造函数 std::thread 的实例。
我有以下问题。
- 我们不能从线程中的类调用函数吗 构造函数?
- 这种使用线程调用不同类的函数的想法有意义吗?
【问题讨论】:
标签: c++ multithreading sockets c++11 stdthread