简体   繁体   English

使用Win 32 API创建并行线程

[英]Creating parallel threads using Win 32 API

Here is the problem: I have two sparse matrices described as vector of triplets. 问题是:我有两个稀疏矩阵,被描述为三元组的向量。 The task is to write multiplication function for them using parallel processing with Win 32 API. 任务是使用Win 32 API的并行处理为其编写乘法函数。 So I need to know how do I: 所以我需要知道如何:

1) Create a thread in Win 32 API 1)在Win 32 API中创建线程

2) Pass input parameters for it 2)传递输入参数

3) Get return value. 3)获取返回值。

Thanks in advance! 提前致谢!

Edit: "Process" changed for "Thread" 编辑:“进程”更改为“线程”

Well, the answer to your question is CreateProcess and GetExitCodeProcess . 好吧,您问题的答案是CreateProcessGetExitCodeProcess

But the solution to your problem isn't another process at all, it's more threads. 但是解决您的问题的方法根本不是另一个过程,而是更多的线程。 And probably OpenMP is a much more suitable mechanism than creating your own threads. 可能OpenMP是比创建自己的线程更合适的机制。

If you have to use the Win32 API directly for threads, the process is something like: 如果必须将Win32 API直接用于线程,则过程类似于:

  • Build a work item descriptor by allocating some memory, storing pointers to the real data, indexes for what this thread is going to work on, etc. Use a structure to keep this organized. 通过分配一些内存,存储指向实际数据的指针,该线程将要处理的内容的索引等来构建工作项描述符。使用一种结构来保持其组织性。
  • Call CreateThread and pass the address of the work item descriptor. 调用CreateThread并传递工作项描述符的地址。
  • In your thread procedure, cast the pointer back to a structure pointer, access your work item descriptor, and process the data. 在线程过程中,将指针转换回结构指针,访问工作项描述符,然后处理数据。
  • In your main thread, call WaitForMultipleObjects to join with the worker threads. 在主线程中,调用WaitForMultipleObjects以与辅助线程一起加入。

For even greater efficiency, you can use the Windows thread pool and call QueueUserWorkItem . 为了获得更高的效率,可以使用Windows线程池并调用QueueUserWorkItem But while you won't have to create threads yourself, you'd then need event handles to join tasks back to the main thread. 但是,尽管您不必自己创建线程,但是您将需要事件句柄将任务连接回主线程。 It's about the same amount of code I suspect. 这与我怀疑的代码量差不多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM