Hello Arno, Per your advice, I coded the following but in the first time there needs to be a wait for other thread, it times out! I wonder why?
void inline __fastcall lockCriticalSection(TCriticalSection *criticalSection) { HANDLE hMutex; // Create a mutex with no initial owner. hMutex = CreateMutex( NULL, // no security attributes true, // initially owned String((int)(void*)criticalSection).c_str()); // name of mutex DWORD dwWaitResult; // Request ownership of mutex. if(hMutex) { dwWaitResult = WaitForSingleObject( hMutex, // handle to mutex 5000L); // five-second time-out interval switch(dwWaitResult) { // Cannot get mutex ownership due to time-out. case WAIT_TIMEOUT: throw Exception("BUG FOUND, CHECK CALL STACK!"); // Got ownership of the abandoned mutex object. case WAIT_ABANDONED: break; } } criticalSection->Acquire(); }; void inline __fastcall releaseCriticalSection(TCriticalSection *criticalSection) { criticalSection->Release(); HANDLE hMutex; // Create a mutex with no initial owner. hMutex = OpenMutex( MUTEX_ALL_ACCESS, false, String((int)(void*)criticalSection).c_str() ); // name of mutex if(hMutex) { DWORD dwWaitResult; // Request ownership of mutex. dwWaitResult = WaitForSingleObject( hMutex, // handle to mutex 5000L); // five-second time-out interval switch(dwWaitResult) { // The thread got mutex ownership. case WAIT_OBJECT_0: // Release ownership of the mutex object. if(!ReleaseMutex(hMutex)) { // Deal with error. } break; // Cannot get mutex ownership due to time-out. case WAIT_TIMEOUT: break; // Got ownership of the abandoned mutex object. case WAIT_ABANDONED: break; } } }; PLEASE HELP! The technique I used is called named mutex. What I am trying to do is to complete the job in these two functions. Best Regards, SZ On 4/28/07, Arno Garrels <[EMAIL PROTECTED]> wrote: > Fastream Technologies wrote: > > For example, perhaps we can implement a single global mutex for the > > lock... function. > > No good idea. That would change a lot. > > > This would slow down the execution for sure but > > would it create the exception? > > You have to raise the exception if the wait returns WAIT_TIMEOUT. > > > Can you provide some source code > > snippet for this please? > > Please search the internet there should be tons of snippets > around, or ask your question in newsgroup: > borland.public.delphi.nativeapi.win32. > > -- > Arno Garrels [TeamICS] > http://www.overbyte.be/eng/overbyte/teamics.html > > > > > Best Regards, > > > > SZ > > > > On 4/28/07, Fastream Technologies <[EMAIL PROTECTED]> wrote: > >> Arno, > >> > >> I have THOUSANDS of critical sections passed to this function as > >> parameter! Isn't there a simpler way to implement?? > >> > >> Regards, > >> > >> SZ > >> > >> On 4/28/07, Arno Garrels <[EMAIL PROTECTED]> wrote: > >>> Fastream Technologies wrote: > >>>> This sounds reasonable. Can you give an example? Since all my cs > >>>> are of TCriticalSection type, can I do it in this function: > >>> > >>> I thought of using mutexes. That's not very difficult to change. > >>> Have an array of mutex handles, then pass the index to your > >>> function. Search for CreateMutex etc. in the SDK. When the wait > >>> returned WAIT_TIMEOUT and you raise an exception MadExcept will > >>> report the callstacks of each thread. > >>> I don't have any idea how one can raise an exception in a thread > >>> or get to thread's callstack when it hangs around in > >>> CriticalSection->Acquire. > >>> > >>> -- > >>> Arno Garrels [TeamICS] > >>> http://www.overbyte.be/eng/overbyte/teamics.html > >>> > >>>> > >>>> void __fastcall lockCriticalSection(TCriticalSection *CS) > >>>> { > >>>> // currently it is: > >>>> CS->Acquire(); > >>>> > >>>> // what should it be? > >>>> } > >>>> > >>>> Best Regards, > >>>> > >>>> SZ > >>>> > >>>> On 4/28/07, Arno Garrels <[EMAIL PROTECTED]> wrote: > >>>>> Fastream Technologies wrote: > >>>>>> Hello, > >>>>>> > >>>>>> I have a very difficult bug: the program includes lots of > >>>>>> critical sections and it suddenly stops responding! > >>>>> > >>>>> Sounds like deadlock. > >>>>> > >>>>>> I have a wrapper global > >>>>>> function called lockCriticalSection(TCriticalSection *cs) which > >>>>>> calls acquire. I want to log inside this but need to record the > >>>>>> call stack so that I can see which call is the lastest. Any idea? > >>>>> > >>>>> AFAIK Jedi gives you the callstack only upon an exception. > >>>>> My idea is to use a different synchronization method with a > >>>>> timeout parameter, when the wait timed out you can raise an > >>>>> exception. > >>>>> > >>>>> Would that help? > >>>>> > >>>>> -- > >>>>> Arno Garrels [TeamICS] > >>>>> http://www.overbyte.be/eng/overbyte/teamics.html > >>>>> > >>>>>> > >>>>>> Best Regards, > >>>>>> > >>>>>> SZ > >>>>> -- > >>>>> To unsubscribe or change your settings for TWSocket mailing list > >>>>> please goto http://www.elists.org/mailman/listinfo/twsocket > >>>>> Visit our website at http://www.overbyte.be > >>>>> > >>>> > >>>> > >>>> -- > >>>> CSA, Fastream Technologies > >>>> Software IQ: Innovation & Quality > >>>> www.fastream.com | Email: [EMAIL PROTECTED] | Tel: +90-312-223- > >>>> 2830 Join IQWF Server Yahoo group at > >>>> http://groups.yahoo.com/group/IQWFServer Join IQ Reverse Proxy > >>>> Yahoo group at http://groups.yahoo.com/group/IQReverseProxy > >>> -- > >>> To unsubscribe or change your settings for TWSocket mailing list > >>> please goto http://www.elists.org/mailman/listinfo/twsocket > >>> Visit our website at http://www.overbyte.be > -- > To unsubscribe or change your settings for TWSocket mailing list > please goto http://www.elists.org/mailman/listinfo/twsocket > Visit our website at http://www.overbyte.be > -- To unsubscribe or change your settings for TWSocket mailing list please goto http://www.elists.org/mailman/listinfo/twsocket Visit our website at http://www.overbyte.be