如何使用CreateThread()创建具有不同ThreadProc()函数的多线程? [英] How to create multiplethreads each with different ThreadProc() function using CreateThread()?
问题描述
我已经使用CreateThread()成功创建了一个线程。
现在我想创建''n''个线程但每个线程都有不同的ThreadProc ()。
我尝试过以下代码但使用它,''''创建的线程数都执行相同的任务(因为Threadproc()函数af所有线程是相同的。)
//启动线程
for(int i = 1; i< = max_number; i ++)
{
CreateThread(NULL,//选择默认安全性
0,//默认堆栈大小
(LPTHREAD_START_ROUTINE)& ThreadProc,
//例程到我希望这个例程每次都不同,因为我希望每个线程执行不同的功能。
(LPVOID)& i,//线程参数
0,//立即运行线程
& dwThreadId //主题ID
)
}
是有什么方法我可以用不同的Thread程序创建''n''个线程数?
怎么样的东西:
typedef std :: vector< LPTHREAD_START_ROUTINE> ThreadProcList;
typedef std :: vector< DWORD> ThreadIdList;
ThreadProcList threadProcs;
ThreadIdList threadIds;
// 将所有线程过程添加到threadProcs:
// threadProcs.pushBack((LPTHREAD_START_ROUTINE)& ThreadProc);
ThreadProcList :: iterator end = threadProcs.end();
for (ThreadProcList :: iterator it = threadProcs.begin();
it!= end; ++ it)
{
DWORD threadId;
:: CreateThread(NULL, 0 ,
* it,
NULL, 0 ,& threadId);
threadIds.push_back(threadId);
}
// 做任何你想做的线程
您可以创建一个包含线程程序地址的数组:
LPTHREAD_START_ROUTINE pThreadProcs [] = {ThreadProc1,ThreadProc2 / * ,... * / };
for ( int i = 0 ; i< sizeof (pThreadProcs)/ sizeof (pThreadProcs [ 0 ]; i ++)
{
CreateThread(NULL, 0 ,pThreadProcs [i],(LPVOID)( i + 1), 0 NULL);
}
另请注意,我已通过了线程参数通过将值+ 1转换为LPVOID
。循环变量i
是堆栈上的局部变量在创建所有线程后不再有效。在线程内使用
int i =( int )lpParameter
获取值。如果传递指向参数变量的指针,则必须确保当线程访问该变量时,该变量仍然存在。
创建一个线程程序数组,例如:
DWORD WINAPI myThreadProcA(LPVOID p)
{
DWORD dw =(DWORD)p;
return dw;
}
DWORD WINAPI myThreadProcB(LPVOID p)
{
DWORD dw =(DWORD)p;
return dw * dw;
}
VOID CreateMyThreads(LPTHREAD_START_ROUTINE myProc [], int max_number,HANDLE hThread [])
{
DWORD dwThreadId;
for ( int i = 0 ; i< max_number; i ++)
{
hThread [i] = CreateThread(NULL, // 选择默认安全性
0 , // 默认堆栈大小
(LPTHREAD_START_ROUTINE)myProc [i],
// < span class =code-comment>执行例程。
(LPVOID)(i + 1), // 线程参数
0 , // 立即运行线程
& dwThreadId // 主题Id
);
}
}
那么你可以这样称呼它:
const DWORD N = 2 跨度>;
HANDLE hThread [N];
DWORD dwExit [N];
LPTHREAD_START_ROUTINE myProc [N] = {myThreadProcA,myThreadProcB};
CreateMyThreads(myProc,N,hThread);
for ( int i = 0 ; i< N; i ++)
{
do
{
GetExitCodeThread(hThread [i],& dwExit [i]);
} while (dwExit [i] == STILL_ACTIVE);
}
I have successfully created a single thread using CreateThread().
Now I want to create ''n'' number of threads but each with a different 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 ) }
Is there any way I can create ''n'' number of Threads each with a different Thread procedure?解决方案How about something along the lines of:
typedef std::vector<LPTHREAD_START_ROUTINE> ThreadProcList; typedef std::vector<DWORD> ThreadIdList; ThreadProcList threadProcs; ThreadIdList threadIds; // add all the thread procs to threadProcs: // threadProcs.pushBack((LPTHREAD_START_ROUTINE)&ThreadProc); ThreadProcList::iterator end = threadProcs.end(); for(ThreadProcList::iterator it = threadProcs.begin(); it != end; ++it) { DWORD threadId; ::CreateThread( NULL, 0, *it, NULL, 0, &threadId); threadIds.push_back(threadId); } // Do whatever you want to do with threads
You may create an array holding the addresses of your thread procedures:
LPTHREAD_START_ROUTINE pThreadProcs[] = { ThreadProc1, ThreadProc2 /*, ...*/}; for (int i = 0; i < sizeof(pThreadProcs) / sizeof(pThreadProcs[0]; i++) { CreateThread(NULL, 0, pThreadProcs[i], (LPVOID)(i+1), 0 NULL); }
Note also that I have passed the thread parameter by casting the value + 1 toLPVOID
. The loop variablei
is a local variable on the stack which address is no longer valid when all threads has been created. From within the threads use
int i = (int)lpParameter
to get the value. If you pass a pointer to the parameter variable, you must ensure that the variable still exists when it is accessed by the thread.
Create an array of thread procedures, for instance:
DWORD WINAPI myThreadProcA(LPVOID p) { DWORD dw = (DWORD) p; return dw; } DWORD WINAPI myThreadProcB(LPVOID p) { DWORD dw = (DWORD)p; return dw*dw; } VOID CreateMyThreads(LPTHREAD_START_ROUTINE myProc[], int max_number, HANDLE hThread[]) { DWORD dwThreadId; for (int i=0; i< max_number; i++) { hThread[i] = CreateThread( NULL, //Choose default security 0, //Default stack size (LPTHREAD_START_ROUTINE)myProc[i], //Routine to execute. (LPVOID) (i+1), //Thread parameter 0, //Immediately run the thread &dwThreadId //Thread Id ); } }
Then you might call itm, for instance, this way:
const DWORD N=2; HANDLE hThread[N]; DWORD dwExit[N]; LPTHREAD_START_ROUTINE myProc[N] = {myThreadProcA, myThreadProcB}; CreateMyThreads(myProc, N, hThread); for (int i=0; i<N; i++) { do { GetExitCodeThread(hThread[i], &dwExit[i]); } while (dwExit[i] == STILL_ACTIVE); }
这篇关于如何使用CreateThread()创建具有不同ThreadProc()函数的多线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!