Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multithreading: do I need protect my variable in read-only method?

I have few questions about using lock to protect my shared data structure. I am using C/C++/ObjC/Objc++

For example I have a counter class that used in multi-thread environment

class MyCounter {
private:
    int counter;
    std::mutex m;

public:
    int getCount() const {
        return counter;
    }
    void increase() {
        std::lock_guard<std::mutex> lk(m);
        counter++;
    }
};
  1. Do I need to use std::lock_guard<std::mutex> lk(m); in getCount() method to make it thread-safe?

  2. What happen if there is only two threads: a reader thread and a writer thread then do I have to protect it at all? Because there is only one thread is modifying the variable so I think no lost update will happen.

  3. If there are multiple writer/reader for a shared primitive type variable (e.g. int) what disaster may happen if I only lock in write method but not read method? Will 8bits type make any difference compare to 64bits type?

  4. Is any primitive type are atomic by default? For example write to a char is always atomic? (I know this is true in Java but don't know about c++ and I am using llvm compiler on Mac if platform matters)

like image 508
Bryan Chen Avatar asked Mar 23 '12 05:03

Bryan Chen


People also ask

Can two threads read the same variable at the same time?

A race condition occurs when two threads access a shared variable at the same time. The first thread reads the variable, and the second thread reads the same value from the variable.

Do I need mutex for reading?

Unless you use a mutex or another form of memory barrier. So if you want correct behavior, you don't need a mutex as such, and it's no problem if another thread writes to the variable while you're reading it. It'll be atomic unless you're working on a very unusual CPU.

Can multiple threads read the same memory?

Not only are different cores allowed to read from the same block of memory, they're allowed to write at the same time too.

How can you share data between multiple threads?

All static and controlled data is shared between threads. All other data can also be shared through arguments/parameters and through based references, as long as the data is allocated and is not freed until all of the threads have finished using the data.


2 Answers

Yes, unless you can guarantee that changes to the underlying variable counter are atomic, you need the mutex.

Classic example, say counter is a two-byte value that's incremented in (non-atomic) stages:

(a) add 1 to lower byte
    if lower byte is 0:
(b)     add 1 to upper byte

and the initial value is 255.

If another thread comes in anywhere between the lower byte change a and the upper byte change b, it will read 0 rather than the correct 255 (pre-increment) or 256 (post-increment).

In terms of what data types are atomic, the latest C++ standard defines them in the <atomic> header.

If you don't have C++11 capabilities, then it's down to the implementation what types are atomic.

like image 95
paxdiablo Avatar answered Sep 17 '22 21:09

paxdiablo


Yes, you would need to lock the read as well in this case.

There are several alternatives -- a lock is quite heavy here. Atomic operations are the most obvious (lock-free). There are also other approaches to locking in this design -- the read write lock is one example.

like image 24
justin Avatar answered Sep 18 '22 21:09

justin