开发者

Multithreading in c++

I want to run one function with different parameters on different threads:

int threads = 3;
int par1[] = {1, 2, 3};
int par2[] = {4, 5, 6};
for (int i=0; i<threads; i++){
  //new_thread function(par1[i], par2[i]);
}
开发者_如何转开发

I know nothing about threads. I tried to do something the windows API (can't use other libraries), but it doesn't work. How should I implement this? And it is possible to start an unknown number of threads of the time of programming (dynamically creating threads)?


One sample example for Windows,

#include <Windows.h>

struct thread_data
{
 int m_id;
 thread_data(int id) : m_id(id) {}
};
DWORD WINAPI thread_func(LPVOID lpParameter)
{
 thread_data *td = (thread_data*)lpParameter;
 cout << "thread with id = " << td->m_id << endl;
 return 0;
}
int main()
{
 for (int i=0; i< 10; i++)
 {
  CreateThread(NULL, 0, thread_func, new thread_data(i) , 0, 0);
 }
}

In this example, each thread gets different data, namely of type thread_data, which is passed as argument to the thread function thread_func().

Read these to know how to create thread on windows:

http://msdn.microsoft.com/en-us/library/ms682516(VS.85).aspx
http://msdn.microsoft.com/en-us/library/ms682453(v=vs.85).aspx


Also, you may like this suggestion as well:

Better design : define a reusable class!


I know nothing about threads.

This is a problem. Threads is one of the area where experimenting without at least some theoretical background usually ends in a catastrophe. There’s so much that can go wrong with threading, but that doesn’t occur until much, much later.

I would suggest either getting a book about multithreading or reading a few articles online before starting.

If you are interested in using multithreading for for efficiency then Herb Sutter’s column may be for you. And then there’s the excellent Introduction to Parallel Computing by Blaise Barney.


You need to do a couple of things to get this to work. First of all the windows thread function takes this signature:

DWORD WINAPI ThreadFunction(LPVOID args)

The thread startup routine is this function:

HANDLE WINAPI CreateThread(
  __in_opt   LPSECURITY_ATTRIBUTES lpThreadAttributes,
  __in       SIZE_T dwStackSize,
  __in       LPTHREAD_START_ROUTINE lpStartAddress,
  __in_opt   LPVOID lpParameter,
  __in       DWORD dwCreationFlags,
  __out_opt  LPDWORD lpThreadId
);

Note that the way to pass arguments to your thread function is through an LPVOID. That means you need to create a structure to hold your par1 and par2. You would then pass in a pointer to this structure and the pull the contents out in your thread routine.


Since OpenMP is supported in Visual studio why not use that? It's far simpler than managing your own threads manually, is very portable and the example you gave is almost perfect for it. Wikipedia has a reasonble introduction to the concepts of OpenMP.

In your example the simple (and slightly naive - normally you don't explicitly say the number of threads, you say the number of work units) code is:

int threads = 3;
int par1[] = {1, 2, 3};
int par2[] = {4, 5, 6};
#pragma omp parallel for
for (int i=0; i<threads; i++){
  function(par1[i], par2[i]);
}

With an appropriate option passed to the compiler to enable OpenMP. Without this option it still compiles and runs as a serial program too.


May i suggest the use of boost::thread_group ?

boost::thread_group g;
int threads = 3;
int par1[] = {1, 2, 3};
int par2[] = {4, 5, 6};
for (int i=0; i<threads; i++){
  g.create_thread(/*function*/);
  //new_thread function(par1[i], par2[i]);
}
g.join_all();


You create a thread in Windows by calling CreateThread. There's an example here.

Yes, you can dynamically create threads.

Now, when your "thread function" is getting called (which means the thread started) you use the passed parameter which you've feeded when calling CreateThread.
At the "thread-function" it's "LPVOID lpParam". You should cast it to your type. If it's two objects you need to pass to the thread then create your own custom struct/class with the appropriate memebers, and pass an instance of it when calling CreateThread so you can use it later. Very straight-forward.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜