Suppose I have two thread state variables and one C ++ 11 common variable. What happens if thread1 calls a notification and after that call thread2 to wait? will thread2 block forever or will it continue to work due to a notification call on thread1?
Edit:
enum bcLockOperation { bcLockOperation_Light = -1, bcLockOperation_Medium = 50, bcLockOperation_Heavy = 1 } class BC_COREDLL_EXP bcCustomMutex { private: bcCustomMutex(const bcCustomMutex&); bcCustomMutex& operator=(const bcCustomMutex&); protected: bcAtomic<int> mFlag; bcMutex mMutex; bcConditionVariable mCond; public: bcCustomMutex() { bcAtomicOperation::bcAtomicInit(mFlag, 0); }; ~bcCustomMutex() {}; bcInline void lock(bcLockOperation pLockOperation = bcLockOperation_Medium) { bcINT32 lNewLoopCount = static_cast<bcINT32>(pLockOperation); bcINT32 lLoopCounter = 0; bcINT32 lExpected = 0; bcINT32 lLoopCount = bcAtomicOperation::bcAtomicLoad(mFlag, bcMemoryOrder_Relaxed); while (true) { while(bcAtomicOperation::bcAtomicLoad(mFlag, bcMemoryOrder_Relaxed) != 0 && lLoopCounter != lLoopCount) ++lLoopCounter; bcAtomicOperation::bcAtomicCompareExchangeStrong( mFlag, &lExpected, lNewLoopCount, bcMemoryOrder_Acquire, bcMemoryOrder_Relaxed); if(lExpected == 0) {
I want to write my own mutex so that each thread can provide an argument representing the complexity of the operations that the current thread wants to perform. If the complexity of the operation is low, other threads will be in a loop, such as spin lock, but if the complexity of the operation is medium, each thread will iterate 50 times and then sleep on the condition variable, and if the operation is very complex, other threads will go straight ahead .
Now suppose thread1 blocks this mutex, and thread2 goes on hold because its loopCounter reaches its end and to the right before locking the mutex state variable, thread1 requests notify the condition variable. Now thread2 will sleep until another thread locks the user mutex and then unlocks it.
I am new to multithreading and I want to learn. I know that my class may contain errors or it may be completely wrong, but is there a way to fix this problem or a good algorithm for writing such a mutex.
Another question: is the ordering of atoms correctly ordered?
source share