locking

Proper C++ way of implementing a lock_guard for custom library

一曲冷凌霜 提交于 2020-08-26 10:44:05
问题 O wise interwebs We have an impasse between two colleagues that we could use your help resolving in the proper C++ way. Basically we have a set of utility classes, two of which are a Mutex and SpinLock class which both have the following abridged interface: class Mutex { public: Mutex(); ~Mutex(); void Lock(); void Unlock(); // ... }; Obviously this is similar to, but differently-cased than the BasicLockable concept used by std::lock_guard, so we want something similar (assume that the Mutex

Why can't await and signal methods be called directly on object of ReentrantLock. Why do I need Condition?

非 Y 不嫁゛ 提交于 2020-08-26 10:34:46
问题 In old synchronized block, we used same object to synchronize on, also used wait and notify methods. So they can all refer to same lock. Makes sense. So when I use class ReentrantLock, why can't I also use same variable to call lock , unlock as well as await and signal ? Why do I need to make additional Condition variable? That is, why I need to do this: Lock lock = new ReentrantLock(); Condition condition = lock.newCondition(); void doSomething() { lock.lock(); //some code condition.await();

LockService lock does not persist after a prompt is shown

送分小仙女□ 提交于 2020-08-20 05:58:16
问题 I have a script in Google Sheets, which runs a function when a user clicks on an image. The function modifies content in cells and in order to avoid simultaneous modifications I need to use lock for this function. I cannot get, why this doesn't work (I still can invoke same function several times from different clients): function placeBidMP1() { var lock = LockService.getScriptLock(); lock.waitLock(10000) placeBid('MP1', 'I21:J25'); lock.releaseLock(); } placeBid() function is below: function

Atomic struct containing pointer

隐身守侯 提交于 2020-08-19 12:20:52
问题 #include <atomic> #include <iostream> using namespace std; struct Simple{ int a = 0; int b = 0; }; struct WithPointer{ int *a = nullptr; int b = 0; }; int main(int argc, char const *argv[]) { atomic<Simple> simple; cout<<simple.is_lock_free()<<"\n"; atomic<Simple*> simple_p; cout<<simple_p.is_lock_free()<<"\n"; atomic<WithPointer> with_pointer; cout<<with_pointer.is_lock_free()<<"\n"; return 0; } This example works fine for the Simple struct but not for the WithPointer struct. I get the