1 #include "thread_group.h"
  2 
  3 DWORD WINAPI Thread_Group_Thread( LPVOID lpParam ) 
  4 { 
  5 	Thread_Group* thread_group = (Thread_Group*)lpParam;
  6 
  7 	int threadIndex = -1;
  8 
  9 	DWORD currentThread = GetCurrentThreadId();
 10 	for( int i = 0; i < thread_group->thread_no; ++i )
 11 	{
 12 		if( currentThread == thread_group->dwThreadId[i] )
 13 		{
 14 			threadIndex = i;
 15 			break;
 16 		}		
 17 	}
 18 
 19 	if( threadIndex < 0 )
 20 	{
 21 		return 10;
 22 	}
 23 	if( thread_group->init )
 24 	{
 25 		thread_group->init();
 26 	}
 27 
 28 	while( TRUE )
 29 	{
 30 		
 31 		WaitForSingleObject( thread_group->hStartEvents[threadIndex], INFINITE );
 32 		ResetEvent( thread_group->hStartEvents[threadIndex] );
 33 		
 34 		if( thread_group->end )
 35 		{
 36 			break;
 37 		}
 38 		
 39 		if( thread_group->thread_run )
 40 		{
 41 			thread_group->thread_run( threadIndex, thread_group );
 42 		}						
 43 		SetEvent( thread_group->hStopEvents[threadIndex] );
 44 	}
 45     return 0; 
 46 } 
 47 int Thread_Group_create( Thread_Group* thread_group, Thread_Run thread_run, int thread_no )
 48 {
 49 	return Thread_Group_create_init( thread_group, thread_run, thread_no, NULL );
 50 }
 51 
 52 
 53 int  Thread_Group_create_init( Thread_Group* thread_group, Thread_Run thread_run, int thread_no, Init init )
 54 {
 55 	thread_group->thread_no	   = thread_no;
 56 	thread_group->end		   = FALSE;	
 57 	thread_group->thread_run   = thread_run;
 58 	thread_group->init		   = init;
 59 	thread_group->dwThreadId   =  (DWORD*)malloc( sizeof( DWORD  ) * thread_no );
 60 	thread_group->hStartEvents = (HANDLE*)malloc( sizeof( HANDLE ) * thread_no );
 61 	thread_group->hStopEvents  = (HANDLE*)malloc( sizeof( HANDLE ) * thread_no );
 62 	thread_group->hThread	   = (HANDLE*)malloc( sizeof( HANDLE ) * thread_no );
 63 
 64 	for( int i = 0; i < thread_no; i++ )
 65 	{
 66 		thread_group->hStartEvents[i] = CreateEvent( 0, TRUE, FALSE, 0 );
 67 		thread_group->hStopEvents[i]  = CreateEvent( 0, TRUE, TRUE,  0 );
 68 	}
 69 	 
 70 	for( int i = 0; i < thread_no; i++ )
 71     {
 72         thread_group->hThread[i] = CreateThread( 
 73             NULL,
 74             0,
 75             Thread_Group_Thread,   
 76             thread_group, 
 77             0,                
 78             &thread_group->dwThreadId[i]);
 79  
 80         if (thread_group->hThread[i] == NULL) 
 81         {
 82             return 0;
 83         }
 84     }
 85 
 86 	return 1;
 87 }
 88 void Thread_Group_execute( Thread_Group* thread_group )
 89 {
 90 	WaitForMultipleObjects( thread_group->thread_no,
 91 		thread_group->hStopEvents,
 92 		TRUE, INFINITE);
 93 
 94 	for( int i = 0; i < thread_group->thread_no; i++ )
 95 	{
 96 		ResetEvent( thread_group->hStopEvents[i] );
 97 		SetEvent( thread_group->hStartEvents[i] );				
 98 	}
 99 }
100 
101 void Thread_Group_wait( Thread_Group* thread_group )
102 {
103 	WaitForMultipleObjects( thread_group->thread_no,
104 		thread_group->hStopEvents,
105 		TRUE, INFINITE);
106 }
107 
108 void Thread_Group_release( Thread_Group* thread_group )
109 {
110 	Thread_Group_wait( thread_group );
111 	
112 	thread_group->end = TRUE;
113 	int i;
114 	for( i = 0; i < thread_group->thread_no; i++ )
115 	{		
116 		if( !SetEvent( thread_group->hStartEvents[i] ) )
117 		{
118 			return;
119 		}
120 	}
121 	//std::cout << i ;
122 	
123 	Thread_Group_wait( thread_group );
124 	WaitForMultipleObjects(thread_group->thread_no,
125 		thread_group->hThread, 
126 		TRUE, INFINITE);
127 
128 	for(int i = 0; i < thread_group->thread_no; i++ )
129     {
130         CloseHandle( thread_group->hThread[i]      );
131         CloseHandle( thread_group->hStartEvents[i] );
132         CloseHandle( thread_group->hStopEvents[i]  );
133     }
134 
135 	free( thread_group->dwThreadId );
136 	free( thread_group->hStartEvents );
137 	free( thread_group->hStopEvents );
138 	free( thread_group->hThread );
139 }


syntax highlighted by Code2HTML, v. 0.9.1