mutex

What happens when reading or writing concurrently without a mutex

倖福魔咒の 提交于 2020-06-11 15:56:11
问题 In Go, a sync.Mutex or chan is used to prevent concurrent access of shared objects. However, in some cases I am just interested in the latest value of a variable or field of an object. Or I like to write a value and do not care if another go-routine overwrites it later or has just overwritten it before. Update: TLDR; Just don't do this. It is not safe. Read the answers, comments, and linked documents! Here are two variants good and bad of an example program, where both seem to produce

Unlock mutex on exception

馋奶兔 提交于 2020-05-29 01:03:58
问题 mutex.lock(); try { foo(); // can throw exception } catch (...) { mutex.unlock(); throw; } mutex.unlock(); To guaranty the unlock i must call mutex.unlock() in catch block and in normal case. Is there any option to avoid duplication? Thank You 回答1: What you are looking for is a mutex wrapper like std::lock_guard: #include <mutex> std::mutex _mutex; void call_foo() { std::lock_guard<std::mutex> lock(_mutex); try { foo(); // can throw exception } catch (...) { // the mutex is unlocked here...

Handing over locked std::unique_lock to new threads

谁都会走 提交于 2020-05-27 07:26:10
问题 Consider the following example where I create a std::mutex , lock it and then hand the lock over to another thread : #include <future> #include <mutex> int main() { // Create and lock a mutex std::mutex mutex; std::unique_lock<decltype(mutex)> lock(mutex); // Hand off the lock to another thread auto promise = std::async(std::launch::async, [lock{ std::move(lock) }]() mutable { // Unlock the mutex lock.unlock(); }); promise.get(); return 0; } The example seems to run fine with gcc 6.3 but

Handing over locked std::unique_lock to new threads

丶灬走出姿态 提交于 2020-05-27 07:24:40
问题 Consider the following example where I create a std::mutex , lock it and then hand the lock over to another thread : #include <future> #include <mutex> int main() { // Create and lock a mutex std::mutex mutex; std::unique_lock<decltype(mutex)> lock(mutex); // Hand off the lock to another thread auto promise = std::async(std::launch::async, [lock{ std::move(lock) }]() mutable { // Unlock the mutex lock.unlock(); }); promise.get(); return 0; } The example seems to run fine with gcc 6.3 but

How does a mutex.Lock() know which variables to lock?

牧云@^-^@ 提交于 2020-05-24 07:59:00
问题 I'm a go-newbie, so please be gentle. So I've been using mutexes in some of my code for a couple weeks now. I understand the concept behind it: lock access to a certain resource, interact with it (read or write), and then unlock it for others again. The mutex code I use is mostly copy-paste-adjust. The code runs, but I'm still trying to wrap my head around it's internal working. Until now I've always used a mutex within a struct to lock the struct. Today I found this example though, which

Semaphore vs Mutex in Producer/Consumer

☆樱花仙子☆ 提交于 2020-03-22 09:22:28
问题 In the Producer-Consumer problem, why are we often suggested use to semaphores instead of using a lock/mutex? I don't see a valid reason to use a semaphore because we only have 2 threads coordinating. In this case a lock seems much easier to code and reason about because a thread will lock the buffer then free it so the other thread can do the same. There are only 2 threads so I don't see the use of signaling. Can anyone say why it is suggested to use semaphores usually for producer-consumer?

Should a return statement be inside or outside a lock?

一笑奈何 提交于 2020-03-12 07:05:56
问题 I just realized that in some place in my code I have the return statement inside the lock and sometime outside. Which one is the best? 1) void example() { lock (mutex) { //... } return myData; } 2) void example() { lock (mutex) { //... return myData; } } Which one should I use? 回答1: Essentially, which-ever makes the code simpler. Single point of exit is a nice ideal, but I wouldn't bend the code out of shape just to achieve it... And if the alternative is declaring a local variable (outside

How to communicate between threads with wait and notify?

懵懂的女人 提交于 2020-03-04 15:34:13
问题 I have 2 threads running in the same function. I want to edit the data structures later in the code, but I want to make sure that both the threads have read the data and any future changes in the dict_list and ans_list will not be read by these threads. I was thinking of making use of commands such as wait() and notify() just before mutex.acquire() but since both the threads are using the same function, the second thread will have to wait for a notify that will never come. How can I approach

Using std::mutex as member variable in a class

为君一笑 提交于 2020-03-03 08:47:05
问题 I have defined a class that has std::mutex my_mutex as its private member variable. But when I try to use it using lock_guard in a member function which is called from different threads, the compiler throws up a lots of errors. If I keep this mutex outside the class it works. The code is as follows class ThreadClass { std::mutex my_mutex; public: void addToList(int max, int interval) { std::lock_guard<std::mutex> guard(my_mutex); for (int i = 0; i < max; i++) { // Some operation } } }; int

How to make a synchronization mutex with access by every process?

寵の児 提交于 2020-01-25 03:33:07
问题 I need to use a global mutex to synchronize access to a mutually shared file by several processes. I create the mutex as such: HANDLE hMutex = ::CreateMutex(NULL, FALSE, L"Global\\MySpecialName"); And then use it in: //Entering critical section VERIFY(::WaitForSingleObject(hMutex, INFINITE) == WAIT_OBJECT_0); and then: //Leave critical section VERIFY(::ReleaseMutex(hMutex)); The issue arises from the fact that the processes sharing this mutex are a local-system service and several user-mode