Click to See Complete Forum and Search --> : thread critical object problem


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

mgopshtein
February 25th, 2006, 12:48 AM
What is the "param" that is passed to 2nd thread function? Is it possible that it is a pointer to some object on the stack of 1st thread/on the heap, that is alsready released?

zerver
February 27th, 2006, 12:48 PM
A debugger can be very useful !

If you tell us on what line of code it crashes, I'm pretty sure someone can help you.

Regards / Z

MikeAThon
February 27th, 2006, 02:20 PM
Did you initialize the critical section before using it (and delete it before exiting the program)?

InitializeCriticalSection() http://msdn.microsoft.com/library/en-us/dllproc/base/initializecriticalsection.asp
DeleteCriticalSection() http://msdn.microsoft.com/library/en-us/dllproc/base/deletecriticalsection.asp

Mike