📄 multiprocdiv.h
字号:
}
#define BEGIN_CTRL_THREAD_FUNCTION(mac_functionName,mac_inputStructType,mac_inputStructTypeForChild,\
mac_eraseFunction,mac_beginFunction,mac_endFunction) \
DWORD mac_functionName##_ctrl( LPVOID param ) \
{ \
mac_inputStructType* params = (mac_inputStructType*)param; \
SYSTEM_INFO sysInfo; \
int numProc; \
DWORD dwThread[32]; \
HANDLE hThread[32]; \
mac_inputStructTypeForChild thrParams[32]; \
DWORD affMask = 0; \
CRITICAL_SECTION* criticalSect = &mac_functionName##CritSect; \
HANDLE* event = &mac_functionName##Event; \
HANDLE* thisThreadHandle = &mac_functionName##ThreadHandle; \
int tasksNum; \
LPTHREAD_START_ROUTINE childFunction = (LPTHREAD_START_ROUTINE)mac_functionName##_child; \
int thrTaskNum; \
int* thrMessage = (int*)&mac_functionName##Message; \
bool (*eraseFunction)( void* ) = ( bool (*)( void* ) )mac_eraseFunction; \
bool (*beginFunction)( void* ) = ( bool (*)( void* ) )mac_beginFunction; \
bool (*endFunction)( void* ) = ( bool (*)( void* ) )mac_endFunction; \
int i; \
thrWriteToTrace0( _CRT_WARN, "Control thread begins\n" )
#define BEGIN_CTRL_THREAD_CORE(mac_tasksNum) \
tasksNum = mac_tasksNum; \
InitializeCriticalSection( criticalSect ); \
*event = CreateEvent( 0, true, false, NULL ); \
GetSystemInfo( &sysInfo ); \
numProc = sysInfo.dwNumberOfProcessors; \
if( numProc > 32 ) { \
EXIT_CTRL_THREAD( *thisThreadHandle ); \
} \
if( beginFunction ) { \
beginFunction( params ); \
} \
*thrMessage = NONE_FINISH_WORK; \
for( i = 0; i <= tasksNum; i ++ ) \
{ \
if( i > 0 ) { \
i --; \
thrWriteToTrace2( _CRT_WARN, "Control thread gives task #%d to" \
" thread #%d\n", i, thrTaskNum )
// givingTask
#define END_CTRL_THREAD_CORE() \
thrParams[thrTaskNum].doNext = true; \
thrWriteToTrace1( _CRT_WARN, "Control thread resumes thread #%d\n", \
thrTaskNum ); \
while( !ResumeThread( hThread[thrTaskNum] ) ) Sleep(0); \
i ++; \
} \
if( i < tasksNum ) { \
if( i < numProc ) { \
thrParams[i].threadNum = i; \
thrParams[i].working = true; \
thrWriteToTrace0( _CRT_WARN, "Control thread creates child" \
" thread\n" ); \
hThread[i] = CreateThread( NULL, 0, childFunction, \
&(thrParams[i]), CREATE_SUSPENDED, &(dwThread[i]) ); \
thrParams[i].hThread = hThread[i]; \
thrParams[i].doNext = false; \
SET_CHILD_THREAD_PRIORITY( hThread[i] ); \
SET_CHILD_THREAD_AFFINITY_MASK( hThread[i], affMask ); \
SET_CHILD_THREAD_IDEAL_PROCESSOR( hThread[i], (DWORD)i ); \
thrTaskNum = i; \
} \
else { \
thrWriteToTrace0( _CRT_WARN, "Control thread waits for a message\n" ); \
WAIT_SINGLE_OBJECT_CTRL(); \
EnterCriticalSection( criticalSect ); \
ResetEvent( *event ); \
thrTaskNum = *thrMessage; \
thrWriteToTrace1( _CRT_WARN, "Control thread got a message" \
" from thread #%d\n", thrTaskNum ); \
thrParams[thrTaskNum].doNext = true; \
*thrMessage = NONE_FINISH_WORK; \
LeaveCriticalSection( criticalSect ); \
} \
} \
} \
thrWriteToTrace0( _CRT_WARN, "Control thread waits for finishing all" \
" threads\n" ); \
for( i = tasksNum; i < numProc; i ++ ) { \
thrParams[i].doNext = false; \
ResumeThread( hThread[i] ); \
WaitForSingleObject( hThread[i], INFINITE ); \
} \
for( i = 0; i < numProc; i ++ ) \
{ \
thrWriteToTrace0( _CRT_WARN, "Control thread waits for message\n" ); \
WAIT_SINGLE_OBJECT_CTRL(); \
EnterCriticalSection( criticalSect ); \
ResetEvent( *event ); \
thrTaskNum = *thrMessage; \
thrWriteToTrace1( _CRT_WARN, "Control thread got last message from" \
" thread #%d\n", thrTaskNum ); \
thrParams[thrTaskNum].doNext = false; \
*thrMessage = NONE_FINISH_WORK; \
while( !ResumeThread( hThread[thrTaskNum] ) ) Sleep(0); \
LeaveCriticalSection( criticalSect ); \
WaitForSingleObject( hThread[thrTaskNum], INFINITE ); \
}
#define END_CTRL_THREAD_FUNCTION() \
thrWriteToTrace0( _CRT_WARN, "Control thread is getting normal finishing\n" ); \
if( endFunction ) { \
endFunction( params ); \
} \
CloseHandle( *event ); \
DeleteCriticalSection( criticalSect ); \
*thisThreadHandle = 0; \
return 0; \
}
#define DEFINE_START_THREAD_FUNCTION(mac_functionName,mac_inputParams) \
extern HANDLE mac_functionName##ThreadHandle; \
bool mac_functionName##mac_inputParams
#define BEGIN_START_THREAD_FUNCTION(mac_functionName,mac_inputParams, \
mac_inputStructType) \
bool mac_functionName##mac_inputParams \
{ \
static bool mac_functionName##StartsFirstTime = true; \
bool anotherThreadBlocksVar; \
mac_inputStructType* params = &mac_functionName##InputStruct; \
HANDLE* thrHandle = &mac_functionName##ThreadHandle; \
LPTHREAD_START_ROUTINE threadCtrlFunction = \
(LPTHREAD_START_ROUTINE)mac_functionName##_ctrl; \
thrWriteToTrace0( _CRT_WARN, "Start function begins\n" ); \
if( mac_functionName##StartsFirstTime ) { \
mac_functionName##StartsFirstTime = false; \
mac_functionName##ThreadHandle = 0; \
} \
if( mac_functionName##ThreadHandle ) { \
EnterCriticalSection( &mac_functionName##CritSect ); \
anotherThreadBlocksVar = true; \
while( anotherThreadBlocksVar ) \
{ \
if( mac_functionName##Message == NONE_FINISH_WORK ) { \
mac_functionName##Message = BRAKE_WORK; \
anotherThreadBlocksVar = false; \
} \
else { \
LeaveCriticalSection( &mac_functionName##CritSect ); \
Sleep(0); \
EnterCriticalSection( &mac_functionName##CritSect ); \
} \
} \
thrWriteToTrace0( _CRT_WARN, "Main thread sets break event\n" ); \
SetEvent( mac_functionName##Event ); \
LeaveCriticalSection( &mac_functionName##CritSect ); \
} \
else {
#define END_START_THREAD_FUNCTION() \
*thrHandle = CreateThread( NULL, 0, \
(LPTHREAD_START_ROUTINE)threadCtrlFunction, params, 0, 0 ); \
SET_CHILD_THREAD_PRIORITY( *thrHandle ); \
} \
thrWriteToTrace0( _CRT_WARN, "Start function ends\n" ); \
return true; \
}
#endif // if !defined __MULTYPROCDIV_H__
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -