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. how to lock function?

how to lock function?

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
5 Posts 5 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.
  • J Offline
    J Offline
    JoneLe86
    wrote on last edited by
    #1

    i can lock a thread, but how about single function? int Func(int par) just want to lock it so other function do not call it until it finish, is that possible?

    L C F 3 Replies Last reply
    0
    • J JoneLe86

      i can lock a thread, but how about single function? int Func(int par) just want to lock it so other function do not call it until it finish, is that possible?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      What do you mean by "lock a function"? By definition you can only lock it if it is running in a separate thread.

      Veni, vidi, abiit domum

      1 Reply Last reply
      0
      • J JoneLe86

        i can lock a thread, but how about single function? int Func(int par) just want to lock it so other function do not call it until it finish, is that possible?

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

        Have a look at Critical Section[^].

        Veni, vidi, vici.

        1 Reply Last reply
        0
        • J JoneLe86

          i can lock a thread, but how about single function? int Func(int par) just want to lock it so other function do not call it until it finish, is that possible?

          F Offline
          F Offline
          Freak30
          wrote on last edited by
          #4

          If you want something inside the function protected from parallel/recursive calls, you can do that by placing a criitical section and a class member variable inside it. If don't want the function to be called at all, you could make it private and add a public function as the only one that calls it. It could look something like that:

          void somePublicFunc()
          {
          EnterCriticalSection(); // keep other treads out
          try
          {
          if (!alreadyRunning) // static private member variable to prevent recursive calls
          {
          alreadyRunning = true;
          privateFunction();
          alreadyRunning = false;
          }
          }
          catch (...)
          {
          alreadyRunning = false; // important to do that before leaving the critical section
          LeaveCriticalSection(); // prevent a dead lock
          throw; // let the caller decide what to do with the initial exception
          }
          LeaveCriticalSection();
          }

          So privateFunction() will only be called once at a time no matter from which thread (provided you don't add another member function that calls it or introduce any friend classes).

          The good thing about pessimism is, that you are always either right or pleasently surprised.

          P 1 Reply Last reply
          0
          • F Freak30

            If you want something inside the function protected from parallel/recursive calls, you can do that by placing a criitical section and a class member variable inside it. If don't want the function to be called at all, you could make it private and add a public function as the only one that calls it. It could look something like that:

            void somePublicFunc()
            {
            EnterCriticalSection(); // keep other treads out
            try
            {
            if (!alreadyRunning) // static private member variable to prevent recursive calls
            {
            alreadyRunning = true;
            privateFunction();
            alreadyRunning = false;
            }
            }
            catch (...)
            {
            alreadyRunning = false; // important to do that before leaving the critical section
            LeaveCriticalSection(); // prevent a dead lock
            throw; // let the caller decide what to do with the initial exception
            }
            LeaveCriticalSection();
            }

            So privateFunction() will only be called once at a time no matter from which thread (provided you don't add another member function that calls it or introduce any friend classes).

            The good thing about pessimism is, that you are always either right or pleasently surprised.

            P Offline
            P Offline
            pasztorpisti
            wrote on last edited by
            #5

            Not bad but in C++ you should use RAII for this, use the destructor of an auto (stack) object as your finally block.

            class CAutoLock
            {
            public:
            CAutoLock(CRITICAL_SECTION& cs)
            : m_CS(cs)
            {
            EnterCriticalSection(&cs);
            }
            ~CAutoLock()
            {
            LeaveCriticalSection(&m_CS);
            }

            private:
            CRITICAL_SECTION& m_CS;
            };

            void func()
            {
            CAutoLock auto_lock(cs);
            // do work here, cs will be automatically released when leaving the function

            {
                CAutoLock auto\_lock2(cs2);
                // the cs2 criticalsection will be left automatically at the end of this scope
            }
            
            // do work
            

            }

            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