Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Using multithreading to log...

Using multithreading to log...

Scheduled Pinned Locked Moved C / C++ / MFC
toolshelptutorial
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pl_kode
    wrote on last edited by
    #1

    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

    C P C 3 Replies Last reply
    0
    • P pl_kode

      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

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      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]

      P 1 Reply Last reply
      0
      • C Cedric Moonen

        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]

        P Offline
        P Offline
        pl_kode
        wrote on last edited by
        #3

        I will try to do this. But I would also request you to please suggest me using MUTEX. THANKS

        1 Reply Last reply
        0
        • P pl_kode

          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

          P Offline
          P Offline
          pl_kode
          wrote on last edited by
          #4

          Please suggest me reagrding this using mutex. THANKS

          R 1 Reply Last reply
          0
          • P pl_kode

            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

            C Offline
            C Offline
            CPallini
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • P pl_kode

              Please suggest me reagrding this using mutex. THANKS

              R Offline
              R Offline
              Rajkumar R
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups