<分区>
我有一个类有一个方法需要连续运行但也能够接收来自用户的输入。所以我想我会使用线程单独运行该方法。
代码看起来像这样(只是主干):
class SystemManager
{
private:
int command;
bool commandAcK;
bool running;
//other vars
public:
SystemManager()
{
//initialisation
}
void runningAlgorithm()
{
while (running)
{
if (commandAcK)
{
//solve command
}
//run algorithm
//print results
}
}
void readCmd()
{
cin >> command;
commandAcK = true;
}
};
int main()
{
SystemManager *SM = new SystemManager;
thread tRunning = SM->runningAlgorithm();
}
现在错误看起来像这样:
不存在从“void
”转换为“std::thread
”的合适的构造函数
Error C2440 'initializing': cannot convert from 'void' to 'std::thread'
我找到了一个新方法,它没有给我任何错误
std::thread tRunning(&SystemManager::runningAlgorithm, SystemManager());
我不明白的第一件事是这个方法不使用类的实例,只是使用通用函数。我如何将它链接到特定实例?我需要它以便它可以读取变量的值。
其次SystemManager前面的"&"
是做什么的?
(&SystemManager::runningAlgorithm)
第三,有更好的方法吗?你有什么想法吗?
提前谢谢你。