小编Ian*_*Ian的帖子

当recv阻塞时,如何正确清理?

考虑下面的示例代码(我快速输入它作为示例,如果有错误则无关紧要 - 我对理论感兴趣).

bool shutDown = false; //global

int main()
{
  CreateThread(NULL, 0, &MessengerLoop, NULL, 0, NULL);
  //do other programmy stuff...
}


DWORD WINAPI MessengerLoop( LPVOID lpParam )
{
  zmq::context_t context(1);
  zmq::socket_t socket (context, ZMQ_SUB);
  socket.connect("tcp://localhost:5556");
  socket.setsockopt(ZMQ_SUBSCRIBE, "10001 ", 6);

  while(!shutDown)
  {
    zmq_msg_t getMessage;
    zmq_msg_init(&getMessage);
    zmq_msg_recv (&getMessage, socket, 0); //This line will wait forever for a message
    processMessage(getMessage); 
  }
}
Run Code Online (Sandbox Code Playgroud)

创建一个线程以等待传入消息并适当地处理它们.线程循环直到shutDown设置为true.

在ZeroMQ中,指南明确说明了必须清理的内容,即消息,套接字和上下文.

我的问题是:因为recv永远等待消息,阻塞线程,如果从未收到消息,我怎么能安全地关闭这个线程?

c++ zeromq

4
推荐指数
1
解决办法
5835
查看次数

如何将std :: string环境块传递给CreateProcess?

我目前正在尝试将CreateProcess与Path,Arguments和Environment Variables一起使用.我的变量存储在字符串中.

在下面的示例中,filePath和cmdArgs工作正常,但我无法使envVars工作.

std::string filePath = "C:\\test\\DummyApp.exe";
std::string cmdArgs  = "Arg1 Arg2 Arg3";
std::string envVars  = "first=test\0second=jam\0";  // One

//LPTSTR testStr = "first=test\0second=jam\0";      // Two

CreateProcess(
   LPTSTR(filePath.c_str()),           //path and application name
   LPTSTR(cmdArgs.c_str()),            // Command line
   NULL,                               // Process handle not inheritable
   NULL,                               // Thread handle not inheritable
   TRUE,                               // Set handle inheritance
   0,                                  // Creation flags
   LPTSTR(envVars.c_str()),            // environment block
   //testStr                      //this line works
   NULL,                               // Use parent's starting directory 
   &si,                                // Pointer to STARTUPINFO structure
   &pi ) …
Run Code Online (Sandbox Code Playgroud)

c++ windows createprocess

3
推荐指数
1
解决办法
2442
查看次数

标签 统计

c++ ×2

createprocess ×1

windows ×1

zeromq ×1