3

Referring to RAII

I can use static mutex for a critical section as:

#include <string>
#include <mutex>
#include <iostream>
#include <fstream>
#include <stdexcept>

void write_to_file (const std::string & message) {
    // mutex to protect file access
    static std::mutex mutex;

    // lock mutex before accessing file
    std::lock_guard<std::mutex> lock(mutex);

    // try to open file
    std::ofstream file("example.txt");
    if (!file.is_open())
        throw std::runtime_error("unable to open file");

    // write message to file
    file << message << std::endl;

    // file will be closed 1st when leaving scope (regardless of exception)
    // mutex will be unlocked 2nd (from lock destructor) when leaving
    // scope (regardless of exception)
}

If use the same approach for a class member function such as:

class Test{
    public:
        void setI(int k)
        {
            static std::mutex mutex;
            std::lock_guard<std::mutex> lock(mutex);
            i=k;
        }

    private:
        int i;    
};

What are the cons and pros of above approach?

Is it more advisable to use the approach as below:

class Test
{
public:
    void setI(int k)
    {
        std::lock_guard<std::mutex> lock(mutex);
        i = k;
    }

private:
    int i;
    std::mutex mutex;
};

which method to ensure thread safety is better?

OdinX
  • 4,135
  • 1
  • 24
  • 33
Gaurav K
  • 2,864
  • 9
  • 39
  • 68

3 Answers3

7

reference 1

"Static variables declared in member functions will keep their value between function calls. There will be only one copy over all instances"

Both of your solutions is "valid", it really depends on what you want to accomplish...

static mutex variable inside member function

This solution gives you one mutex for all instances of the class. It will be effective in providing thread-safety but might not be optimal for performance if you have many objects you spread across different threads. The mutex is also limited to only that single function so usually this makes the implementation impractical. A static private variable is usually better therefore.

private mutex variable inside class

With this solution you get one mutex per instance of your class. It will be effective to provide thread safety for your class so that multiple threads can access it. This is usually the more preferred solution as it allows different threads to access different objects at the same time. However this will not be sufficient to protect access to static members of your class.

Most of the time you want to have a private non-static mutex inside your class.

Community
  • 1
  • 1
0

The static mutex inside function is seems to be useful only (and only) if you're trying to access some resource only (and only) in this function.

If you'll need to add some read function to class Test (like int getI() const;) tomorrow, then you should to refactor and setI function and class Test. If you'll use mutex as a class member, then you need just to write getI's code, using mutex similar as in setI

Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
borisbn
  • 4,988
  • 25
  • 42
-1

I guess static mutex is good. because there is one resource you want to protect. Rather than multiple instances of class having each mutex individually, it's better to have one mutex for all since there is only one resource.

I myself just imagining as a resource like a house. we will have one locker to lock right. Please correct me if I am wrong anywhere :)

Logesh G
  • 1
  • 2