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