简体   繁体   中英

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. So I need to know how do I:

1) Create a thread in Win 32 API

2) Pass input parameters for it

3) Get return value.

Thanks in advance!

Edit: "Process" changed for "Thread"

Well, the answer to your question is CreateProcess and GetExitCodeProcess .

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.

If you have to use the Win32 API directly for threads, the process is something like:

  • 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.
  • 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.

For even greater efficiency, you can use the Windows thread pool and call 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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