问题描述
我已经成功地创建使用的CreateThread()。单线程
I have successfully created a single thread using CreateThread().
现在我想创建'N'的线程数量,但每一个不同的ThreadProc的()。
Now I want to create 'n' number of threads but each with a different ThreadProc().
我曾尝试以下code,但使用它,N被创建的线程数都执行相同的任务(因为ThreadProc的()函数自动对焦的所有线程是一样的。)
I have tried the following code but using it, 'n' number of threads are created all performing the same task (since Threadproc() function af all threads is same.)
//Start the threads
for (int i=1; i<= max_number; i++)
{
CreateThread( NULL, //Choose default security
0, //Default stack size
(LPTHREAD_START_ROUTINE)&ThreadProc,
//Routine to execute. I want this routine to be different each time as I want each thread to perform a different functionality.
(LPVOID) &i, //Thread parameter
0, //Immediately run the thread
&dwThreadId //Thread Id
)
}
有什么办法,我可以创造'N'数量的线程每一个不同的线程程序?
Is there any way I can create 'n' number of Threads each with a different Thread procedure?
推荐答案
试试这个:
DWORD WINAPI ThreadProc1( LPVOID lpParameter)
{
...
return 0 ;
}
DWORD WINAPI ThreadProc2( LPVOID lpParameter)
{
...
return 0 ;
}
...
typedef DWORD (WINAPI * THREADPROCFN)(LPVOID lpParameter);
THREADPROCFN fntable[4] = {ThreadProc1, ThreadProc2, ...} ;
//Start the threads
for (int i = 0; i < max_number; i++)
{
DWORD ThreadId ;
CreateThread( NULL,
0,
(LPTHREAD_START_ROUTINE)fntable[i],
(LPVOID) i,
0,
&ThreadId
) ;
}
这将启动MAX_NUMBER线程与在fntable定义不同的线程程序(TreadProc1,ThreadProc2等)。
This will start max_number threads with different thread procedures (TreadProc1, ThreadProc2, etc.) as defined in fntable.
这篇关于如何创建multiplethreads每个使用的CreateThread不同ThreadProc的()()的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!