Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
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?
Posted
Updated 1-May-13 20:51pm
v2

How about something along the lines of:

C++
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
 
Share this answer
 
You may create an array holding the addresses of your thread procedures:
C++
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 to LPVOID. The loop variable i is a local variable on the stack which address is no longer valid when all threads has been created. From within the threads use
C++
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.
 
Share this answer
 
Create an array of thread procedures, for instance:
C
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:

C
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);

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900