Is it necessary to create a mutex for a read-only stream and for a write-only stream?

There are 2 threads, one reads only signal , the other installs only signal .

Do I need to create mutexes for signal and reason?

UPDATE

I don't care if the crash happens if two threads read / set the same time

+4
source share
4 answers

You probably want to use atomic variables for this, although the mutex will work.

The problem is that there is no guarantee that data will be synchronized between threads, but using atomic variables ensures that as soon as one thread updates this variable, other threads will immediately read the updated value.

A problem may occur if one thread updates the variable in the cache and the second thread reads the variable from memory. This second thread would read the stale value for the variable if the cache had not yet been flushed to memory. Atomic variables ensure that the value of a variable is consistent between threads.

If you are not interested in timely updates of variables, you can leave with one mutable variable.

+2
source

It depends. If the records are atomic, you do not need to block mutual exclusion. If the records are not atomic, you need a lock.

There is also a problem of caching compilers in the CPU cache, which may lead to the fact that the copy in the main memory will not be updated every time it is written. Some languages ​​have ways to tell the compiler not to cache a variable in the processor like this (volatile keyword in Java), or tell the compiler to synchronize any cached values ​​with main memory (synchronized keyword in Java). But, mutex does not solve this problem at all.

+1
source

If you only need synchronization between threads (one thread must do something before the other can start something else), then mutual exclusion should not be necessary.

Mutual exclusion is only necessary when the threads share some resource where the resource may be damaged if they both go through the critical section at about the same time. Think of two people who share a bank account and work at two different ATMs at the same time.

Depending on your language / stream library, you can use the same synchronization mechanism as for mutual exclusion - a semaphore or a monitor. So, if you use Pthreads, someone here can post an example of synchronization, and another one for mutual exclusion. If its java, there would be another example. Perhaps you can tell us which language / library you are using.

0
source

If, as you said in your editing, you only want to convince you of the collapse, then you do not need to do anything (at least, as a rule). If you encounter conflicts between threads, the worst thing that happens is that the data will be damaged - for example, the reader may receive a value that was partially updated and does not directly correspond to any value that the thread wrote. A classic example would be multibyte the number you added something to and was carried over (for example), the old value was 0x3f ffff, which was increasing. Perhaps the read stream can see 0x3f 0000, where the lower 16 bits were increased, but the transfer to the upper 16 bits has not yet occurred.

On a modern machine, the increment on this small data element will usually be atomic, but there will be some size (and alignment) where it is absent - usually if part of the variable is on one line of the cache and partially on another, it will no longer be atomic. The exact size and alignment for this varies somewhat, but the basic idea remains the same - basically it's a matter of quantity having enough numbers to make this happen.

Of course, if you are not careful, something like this can block your code or something in that order - it's impossible to guess what could happen without knowing anything about how you plan to use the data.

0
source

Source: https://habr.com/ru/post/1309259/


All Articles