Click to See Complete Forum and Search --> : Critical Section / Mutex Help need - where to place them in my dll
nkapachi
March 4th, 2006, 02:55 AM
Hi folks,
I have searched the entire web for a simple example (step by step) on how the critical section works, however the results were negative.
The problem is i have a dll which i am trying to thread safe using critical section or mutex ( I know c++ pretty well but not as good as coding critical section). long story short see my pseudo code below and please tell me how can i create a critical section and place them as required.
The code below is where all the threads enter the dll and make the call simultaneously to "case 2" (log_info).
EXTERN_C HRESULT Case(DWORD case_no, info*data)
{
HRESULT hr = S_OK;
switch(case_no)
{
case 2:
log_info("Nk', data);
break;
case 1:
log_data("Pk", data);
break;
}
return hr;
}
HRESULT log_info(LPCSTR ID, info*data)
{
HRESULT hr = S_OK;
CString curTime1;
CString formatstr1 = _T("%Y%m%d%H%M%S");
curTime1 = CTime::GetCurrentTime().Format(formatstr1);
return hr;
}
Could anyone please tell me where/how to place critical section for this code???
Thanks a lot for your help, you would be life save if we solve this
Thanks again
Siddhartha
March 4th, 2006, 04:24 AM
Work with Critical Sections in Windows using APIs -
InitializeCriticalSection (http://msdn.microsoft.com/library/en-us/dllproc/base/initializecriticalsection.asp)
EnterCriticalSection (http://msdn.microsoft.com/library/en-us/dllproc/base/entercriticalsection.asp)
LeaveCriticalSection (http://msdn.microsoft.com/library/en-us/dllproc/base/leavecriticalsection.asp)
DeleteCriticalSection (http://msdn.microsoft.com/library/en-us/dllproc/base/deletecriticalsection.asp)
In this post (http://www.codeguru.com/forum/showthread.php?p=1252692#post1252692) you will find details on Critical Section and a usage explanation with sample.
Coming to your problem at hand - in the code you have presented, there seems to be no shared data, and no I/O activity that needs to be completed one thread at a time. So, I don't see a need for any Synchronization here unless you haven't posted complete code.
Lets assume that log_info function actually wrote to a file - which would mean that this activity would need to be done a thread at a time.
If so, you would guard this method (or actually only the invisible file-write part) with the following -
HRESULT log_info(LPCSTR ID, info*data)
{
HRESULT hr = S_OK;
CString curTime1;
CString formatstr1 = _T("%Y%m%d%H%M%S");
curTime1 = CTime::GetCurrentTime().Format(formatstr1);
::EnterCriticalSection (&cs);
// Some file logging code here that needs to be entered a thread at a time
::LeaveCriticalSection (&cs);
return hr;
} cs in the code above is an object of type CRITICAL_SECTION that is visible within log_info, and has been initialized before it has been used, like this -
CRITICAL_SECTION cs;
::InitializeCriticalSection (&cs); Remember to release the Critical Section Resource once all multithreaded activity is done using DeleteCriticalSection, like this - DeleteCriticalSection (&cs);I hope this makes things clear.
Siddhartha
March 4th, 2006, 04:25 AM
[ redirected ]
Regards,
Siddhartha
nkapachi
March 4th, 2006, 02:42 PM
Thanks a lot for detailed info
Only question i have is - where should i Initialized the object inside the function or outside?
Siddhartha
March 4th, 2006, 05:24 PM
Only question i have is - where should i Initialized the object inside the function or outside? Outside it... And the cs object itself should be shared between threads.
If you create the object inside the function, a new object will be created for every thread that enters it - this will not server your purpose.
Similarly, if you create the object outside the function, but initialize it inside, then every thread that enters it will re-intialize this object - resulting in undefined behaviour.
So an example for correct usage would be: If your function log_info was a public method of a Singleton Class (http://www.codeguru.com/forum/showpost.php?p=1232679&postcount=5), then the Critical Section could have been a private member that is initialized in the constructor, and deleted in the destructor.
nkapachi
March 6th, 2006, 06:44 PM
Hi,
Thanks again for the detailed info - However I just coded up what you suggested but now it doesnt enter the function (log_info) at all.
any idea why?
FYI: I dont have any constructor or destructor since the functions are called independantly from an application...
Thanks for your help
Ninad
Siddhartha
March 7th, 2006, 11:26 AM
However I just coded up what you suggested but now it doesnt enter the function (log_info) at all.Obviously, this problem has nothing to do with Critical Sections.
If your function log_info is not being executed... It implies that this is not being called. ;)
(So, review the logic that calls this function... And if problem persists, post both here.)
nkapachi
March 7th, 2006, 10:50 PM
thanks bro,
will doo
Siddhartha
March 8th, 2006, 02:49 AM
You are welcome... ;)
nkapachi
March 8th, 2006, 02:23 PM
ok this is how i have implemented in my dll..
//Global Variable outside of any functions
CRITICAL_SECTION cs;
//One of the function in DLL which will be only called one in an entire
// process so i have initialize the cs object in there..
HRESULT Startlog(LPCSTR ID, info*data)
{
HRESULT hr = S_OK;
::InitializeCriticalSection (&cs);
//Some code
return hr;
}
//the log_info will be called from an application simultaneosly and thats why
//i need some kind of protection. This function will be called from an application many times (unlikly from start log which only one time) through
//out the process
HRESULT log_info(LPCSTR ID, info*data)
{
HRESULT hr = S_OK;
CString curTime1;
CString formatstr1 = _T("%Y%m%d%H%M%S");
curTime1 = CTime::GetCurrentTime().Format(formatstr1);
::EnterCriticalSection (&cs);
// Some file logging code here that needs to be entered a thread at a time
::LeaveCriticalSection (&cs);
return hr;
}
//Again this function will be called only once after collecting data from function log_info
HRESULT Endlog(LPCSTR ID, info*data)
{
HRESULT hr = S_OK;
::DeleteCriticalSection (&cs);
//Some code
return hr;
}
Now the problem is would this critical section work for what i need (the way i have placed them)?
Please let me konw
Thanks!
Siddhartha
March 8th, 2006, 05:37 PM
Now the problem is would this critical section work for what i need (the way i have placed them)?Well... You need to make sure that the Critical Section object is valid (i.e. Initialized and not Deleted, and not not-initialized) when log_info is called by multiple threads.
So, if the sequence of events is -
1. StartLog () called by main thread before any thread activity begins.
2. log_info called by thread 1
3. log_info called by thread 2
4. log_info called by thread ... N
5. EndLog () called by main thread after all thread activity has ended....Then, things are fine.
nkapachi
March 11th, 2006, 12:12 AM
i got a better idea after searching little more..
logfile()
{
static CCriticalSection c_cs;
c_cs.lock()
//write data
c_cs.unlock();
}
would this work??
thanks again for you inputs..
Siddhartha
March 12th, 2006, 04:28 PM
This is OK.
A general heads-up follows... :)
would this work??This is something that would take you little effort to implement and verify for yourself. People like me can post replies that solve your question pronto - nevertheless, it is important to you for your own good that you run and test a piece of code you have questions about yourself first.
If something doesn't work, ask.
But, it is not in your interests to ask before testing.
CCriticalSection is a wrapper over the thread synchronization WinAPIs.
One can actually single-step into it to see how it makes things work.
Like here - _AFXMT_INLINE CCriticalSection::CCriticalSection() : CSyncObject(NULL)
{
BOOL bSuccess;
bSuccess = Init();
if (!bSuccess)
AfxThrowMemoryException();
} ...Which is defined as -
_AFXMT_INLINE BOOL CCriticalSection::Init()
{
__try
{
::InitializeCriticalSection(&m_sect);
}
__except(STATUS_NO_MEMORY == GetExceptionCode())
{
return FALSE;
}
return TRUE;
}...In the end not any different from the APIs recommended above.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.