Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more: , +
I am trying to create 3 threads using a for loop. Following is my code snippet:

DWORD WINAPI ThreadProc0(LPVOID param)
{
   return 0; 
}
DWORD WINAPI ThreadProc1(LPVOID param)
{
    return 0;
}
DWORD WINAPI ThreadProc2(LPVOID param)
{
    return 0;
}

int WINAPI WinMain( HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{
   DWORD threadId = 0;
   int max_number=1;
   //Start the threads
   typedef DWORD (WINAPI * THREADPROCFN)(LPVOID lpParameter);
   THREADPROCFN function[3] = {ThreadProc0,ThreadProc1,ThreadProc2} ;
   for (int i = 0; i < max_number; i++) 
   {
      CreateThread( NULL,
                    0,
                    (LPTHREAD_START_ROUTINE)&function[i],
                    (LPVOID) i,
                    0,
                    NULL
                   );
   }
}


The code is compiled successfully but when executed, the error is solution.exe has stopped working. When I Debug the code, I get the following error:

Unhandled exception at 0x0034fd00 in Solution.exe: 0xC0000005: Access violation.

Waiting for help.
Posted
Updated 6-May-13 21:50pm
v2

Minor error:
Quote:
int max_number=1;
This should be int max_number=3; right?


The problem:
Quote:
(LPTHREAD_START_ROUTINE)&function[i],
This should be
(LPTHREAD_START_ROUTINE)function[i],


(without & operator).
 
Share this answer
 
v2
Comments
ayesha hassan 7-May-13 4:13am    
Thank you so much. It works now :)
CPallini 7-May-13 4:29am    
You are welcome.
This code works without errors and creates 3 worker threads, keep in mind that you need a

return value as int and

int max_number = 3 and

function[i]:

C++
DWORD WINAPI ThreadProc0(LPVOID param)
{
   return 0; 
}
DWORD WINAPI ThreadProc1(LPVOID param)
{
    return 0;
}
DWORD WINAPI ThreadProc2(LPVOID param)
{
    return 0;
}
 
int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPTSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
	DWORD threadId = 0;
	int max_number=3;
	//Start the threads
	typedef DWORD (WINAPI * THREADPROCFN)(LPVOID lpParameter);
	THREADPROCFN function[3] = {ThreadProc0,ThreadProc1,ThreadProc2} ;
	for (int i = 0; i < max_number; i++) 
	{
		CreateThread( NULL,
					0,
					function[i],
					(LPVOID) i,
					0,
					NULL
					);
	}

	
	return 0;
}


Output:

The thread 0x2c4 has exited with code 0 (0x0).
The thread 0x1160 has exited with code 0 (0x0).
The thread 0xc54 has exited with code 0 (0x0).
The program '[3520] Win32Project1.exe' has exited with code 0 (0x0).
 
Share this answer
 
v5
Comments
ayesha hassan 7-May-13 4:13am    
+1 for the help :) Thanks a lot
The code is compiling because you are casting incompatible types. So the compiler can not check the types. Remove the unnecessary typedef and the casting, and pass the correct parameter (the element of the function array, not the address to it):
C++
LPTHREAD_START_ROUTINE function[3] = {ThreadProc0,ThreadProc1,ThreadProc2} ;
for (int i = 0; i < max_number; i++)
{
   CreateThread( NULL,
                 0,
                 function[i],
                 (LPVOID) i,
                 0,
                 NULL
                );
}
 
Share this answer
 
Comments
ayesha hassan 7-May-13 4:13am    
+1 for the help :) Thanks a lot

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