Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;杀死等待的std::线程_C++_Multithreading_Stdthread - Fatal编程技术网

C++ C++;杀死等待的std::线程

C++ C++;杀死等待的std::线程,c++,multithreading,stdthread,C++,Multithreading,Stdthread,我的辅助线程侦听通道rabbit,并希望在调用方法stopListen()时将其从主线程中删除 void PFClient::startListen() { 此->停止=错误; this->t=thread(&PFClient::callback,this); } void PFClient::callback() { PFAPIResponse*响应; 字符*温度; 而(!此->停止) { 尝试 { std::字符串接收器_data=“”; //std::wcout BasicConsumeM

我的辅助线程侦听通道rabbit,并希望在调用方法stopListen()时将其从主线程中删除

void PFClient::startListen()
{
此->停止=错误;
this->t=thread(&PFClient::callback,this);
}
void PFClient::callback()
{
PFAPIResponse*响应;
字符*温度;
而(!此->停止)
{
尝试
{
std::字符串接收器_data=“”;
//std::wcout BasicConsumeMessage()->Message()->Body();
温度=&接收器_数据[0];
…使用接收到的数据执行某些操作
void PFClient::stopListen()
{
此->停止=真;
}
信号量工作不正常,因为它只有在收到下一条消息后才能工作。 我尝试分离(),终止(),但不起作用。
我怎样才能终止这个进程?

正如我在评论中所建议的,您可以使用无阻塞功能

如果您使用的库没有提供,则可能是有效的替代方案:

while (this->stop == false) {
  auto receiver_data = std::async(/* ... address function and object*/);

  // wait for the message or until the main thread force the stop
  while (this->stop == false &&
         (receiver_data.wait_for(std::chrono::milliseconds(1000) == std::future_status::timeout))
    ;

  if (this->stop == false && receiver_data.vaild() == true) {
    // handle the message
  }
}

你真的应该避免杀掉它(资源泄漏等)。你能使用无阻塞接收函数吗?每个像样的库都应该有一个用于function@deviantfan我知道,但是如果我想怎么做呢?thx,我会试试这个!!这是同步方法
while (this->stop == false) {
  auto receiver_data = std::async(/* ... address function and object*/);

  // wait for the message or until the main thread force the stop
  while (this->stop == false &&
         (receiver_data.wait_for(std::chrono::milliseconds(1000) == std::future_status::timeout))
    ;

  if (this->stop == false && receiver_data.vaild() == true) {
    // handle the message
  }
}