aphelix
February 24th, 2006, 02:27 AM
Hi!
i have a problem with thread syncronization (critical object).
accept function accept a new connection and launch new thread.Then it set some value a global variables.Other part thread still running and enter critical object for set some value a global variable.But program is not running and crash.Why?
Thanks!
Accept
CRITICAL_SECTION cs2;
void Accept(void* param)
{
...........................
while(!stopConcurrentAcceptor){
................
if((threadId = _beginthread((void(*)(void*))PrcMT, 0, (void*)slave)) < 0){
Sleep(MAX_ACCEPTOR_TIME);
continue;
}
EnterCriticalSection(&cs2);
aaa = 9;
//wait
int i = 0;
while(i<100000)
i++;
LeaveCriticalSection(&cs2);
Sleep(MAX_ACCEPTOR_TIME);
}
return;
}
[code]
Thread
[code]
extern CRITICAL_SECTION cs2;
void PrcMT (void* param)
{
printf("%d\n", aaa);
EnterCriticalSection(&cs2);
aaa = 999;
LeaveCriticalSection(&cs2);
printf("%d\n", aaa);
_endthread();
}
Not: 2 function exist in separate cpp file . So i use extern
i have a problem with thread syncronization (critical object).
accept function accept a new connection and launch new thread.Then it set some value a global variables.Other part thread still running and enter critical object for set some value a global variable.But program is not running and crash.Why?
Thanks!
Accept
CRITICAL_SECTION cs2;
void Accept(void* param)
{
...........................
while(!stopConcurrentAcceptor){
................
if((threadId = _beginthread((void(*)(void*))PrcMT, 0, (void*)slave)) < 0){
Sleep(MAX_ACCEPTOR_TIME);
continue;
}
EnterCriticalSection(&cs2);
aaa = 9;
//wait
int i = 0;
while(i<100000)
i++;
LeaveCriticalSection(&cs2);
Sleep(MAX_ACCEPTOR_TIME);
}
return;
}
[code]
Thread
[code]
extern CRITICAL_SECTION cs2;
void PrcMT (void* param)
{
printf("%d\n", aaa);
EnterCriticalSection(&cs2);
aaa = 999;
LeaveCriticalSection(&cs2);
printf("%d\n", aaa);
_endthread();
}
Not: 2 function exist in separate cpp file . So i use extern