我如何运行的线程异步的C / C特异功能++? [英] How can I run a specific function of thread asynchronously in c/c++?

查看:173
本文介绍了我如何运行的线程异步的C / C特异功能++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

性能调整:将数据写入多个管道

Performance tuning: writing data to multiple pipes

现在我做它在单个线程:

Now I'm doing it in a single thread:

for(unsigned int i = 0; i < myvector.size();)
{
    tmp_pipe = myvector[i];
    fSuccess = WriteFile( tmp_pipe, &Time, sizeof(double), &dwWritten, NULL );
    if(!fSuccess)
    {
        myvector.erase(myvector.begin()+i);
        printf("Client pipe closed\r\n");
        continue;
    }
    fSuccess = WriteFile( tmp_pipe, &BufferLen, sizeof(long), &dwWritten, NULL );
    if(!fSuccess)
    {
        myvector.erase(myvector.begin()+i);
        printf("Client pipe closed\r\n");
        continue;
    }
    fSuccess = WriteFile( tmp_pipe, pBuffer, BufferLen, &dwWritten, NULL );
    if(!fSuccess)
    {
        myvector.erase(myvector.begin()+i);
        printf("Client pipe closed\r\n");
        continue;
    }
    i++;
}

而结果是,第一个管道获取数据速度最快,最后管道最慢的。

And the result is that the first pipe gets data fastest ,and the last pipe slowest.

我想在单独的线程所以每个管道被同样处理,这样做的。

I'm thinking of doing it in separate threads so each pipe is equally processed.

但我怎么可以运行的线程的特定功能异步在C(主线程应该得到立即返回)/ C ++?

But how can I run a specific function of thread asynchronously(the main thread should get return immediately) in c/c++?

推荐答案

您可以使用的 的CreateThread 函数来创建一个新的线程并通过管道句柄作为参数传递给线程函数:

You can use the CreateThread function to create a new thread and pass the pipe handle as a parameter to the thread function:

DWORD PipeThread(LPVOID param) {
  HANDLE hPipe = (HANDLE)param;
  // Do the WriteFile operations here
  return 0;
}

for(unsigned int i = 0; i < myvector.size(); i++)
  CreateThread(NULL, 0, PipeThread, myvector[i], 0, NULL);

请注意,该向量类不是线程安全的,所以你会面对 myvector.erase 的问题,如果你不同步访问它们,例如, 。使用一个关键部分。

Note that the vector class isn't thread-safe, so you'll face problems with myvector.erase if you don't synchronize access to them, for eg. using a critical section.

更新:既然你提到频率高,你可以使用的 I / O完成端口而不是对每个管一个单独的线程。那么你可以使用重叠I / O与 WriteFile的来异步执行写,你可以只有一个额外的线程监听写操作完成:

Update: Since you mentioned high frequency, you could use I/O completion ports instead of a separate thread for each pipe. You can then use overlapped I/O with WriteFile to perform the write asynchronously and you could have just one extra thread that listens for completion of writes:

// Initial setup: add pipe handles to a completion port
HANDLE hPort = CreateCompletionPort(myvector[0], NULL, 0, 1);
for (unsigned int i = 1; i < myvector.size(); i++)
  CreateCompletionPort(myvector[i], hPort, 0, 0);

// Start thread
CreateThread(NULL, 0, PipeThread, hPort, 0, NULL);

// Do this as many times as you want
for(unsigned int i = 0; i < myvector.size(); i++) {
  OVERLAPPED *ov = new OVERLAPPED;
  ZeroMemory(ov, sizeof ov);
  WriteFile(myvector[i], buffer, size, NULL, ov);
  // If pipe handle was closed, WriteFile will fail immediately
  // Otherwise I/O is performed asynchronously
}

// Close the completion port at the end
// This should automatically free the thread
CloseHandle(hPort);

---

DWRD PipeThread(LPVOID param) {
  HANDLE hPort = (HANDLE)param;
  DWORD nBytes;
  ULONG_PTR key;
  LPOVERLAPPED ov;

  // Continuously loop for I/O completion
  while (GetQueuedCompletionStatus(hPort, &nBytes, &key, &ov, INFINITE)) {
    if (ov != NULL) {
      delete ov;
      // Do anything else you may want to do here
    }
  }

  return 0;
}

这篇关于我如何运行的线程异步的C / C特异功能++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆