Using multithreading to log...
-
I am creating a logging .dll utility. Which shud support multithreading. I am a bit confused about how shud I be handling multithreading using MUTEX. From the application(using my dll) I can call initializelog() function which get's the necessary data from properties file etc, and logmin(),logmid() and logmax() functions which passes the messages to the dll to log. Now if I have many threads running in my application and each thread calls any of the above functions. It should pass the message and the thread ID to dll to log. I know the Mutex function which synhronises the threads. But am confused WERE and HOW to use this in my DLL to synchronise the threads one at a time. Please help me out to solve this.. THANKS
-
I am creating a logging .dll utility. Which shud support multithreading. I am a bit confused about how shud I be handling multithreading using MUTEX. From the application(using my dll) I can call initializelog() function which get's the necessary data from properties file etc, and logmin(),logmid() and logmax() functions which passes the messages to the dll to log. Now if I have many threads running in my application and each thread calls any of the above functions. It should pass the message and the thread ID to dll to log. I know the Mutex function which synhronises the threads. But am confused WERE and HOW to use this in my DLL to synchronise the threads one at a time. Please help me out to solve this.. THANKS
I think the easiest way to go is to use a critical section. Are you using MFC ? If yes, you can use a CCriticalSection object, otherwise you can go with the plain CRITICAL_SETION (win32). Basically what you would is the following: in your function that logs text in your file, you wrap your logging code with an access to a critical section. This will ensure that only one thread will be able to log data in the file at a certain point of time:
void CLogger::Log(....)
{
// Enter the critical section
m_CriticalSection.Lock();// Do the logging here
// ....// Leave the critical section
m_CriticalSection.Unlock();
}m_CriticalSection is an object of CCriticalSection and has been instancied previously.
Cédric Moonen Software developer
Charting control [v1.4] -
I think the easiest way to go is to use a critical section. Are you using MFC ? If yes, you can use a CCriticalSection object, otherwise you can go with the plain CRITICAL_SETION (win32). Basically what you would is the following: in your function that logs text in your file, you wrap your logging code with an access to a critical section. This will ensure that only one thread will be able to log data in the file at a certain point of time:
void CLogger::Log(....)
{
// Enter the critical section
m_CriticalSection.Lock();// Do the logging here
// ....// Leave the critical section
m_CriticalSection.Unlock();
}m_CriticalSection is an object of CCriticalSection and has been instancied previously.
Cédric Moonen Software developer
Charting control [v1.4] -
I am creating a logging .dll utility. Which shud support multithreading. I am a bit confused about how shud I be handling multithreading using MUTEX. From the application(using my dll) I can call initializelog() function which get's the necessary data from properties file etc, and logmin(),logmid() and logmax() functions which passes the messages to the dll to log. Now if I have many threads running in my application and each thread calls any of the above functions. It should pass the message and the thread ID to dll to log. I know the Mutex function which synhronises the threads. But am confused WERE and HOW to use this in my DLL to synchronise the threads one at a time. Please help me out to solve this.. THANKS
-
I am creating a logging .dll utility. Which shud support multithreading. I am a bit confused about how shud I be handling multithreading using MUTEX. From the application(using my dll) I can call initializelog() function which get's the necessary data from properties file etc, and logmin(),logmid() and logmax() functions which passes the messages to the dll to log. Now if I have many threads running in my application and each thread calls any of the above functions. It should pass the message and the thread ID to dll to log. I know the Mutex function which synhronises the threads. But am confused WERE and HOW to use this in my DLL to synchronise the threads one at a time. Please help me out to solve this.. THANKS
Haven't Googled yet [^], have you?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke -
Why, you need Mutex and don't want to use critical section as suggested earlier. [Using Mutex Objects (WIN32)^] [CMutex (MFC)^] To access or release a CMutex object, create a CMultiLock or CSingleLock object and call its Lock and Unlock member functions