Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can mutex implementations be interchanged (independently of the thread implementation)

Do all mutex implementations ultimately call the same basic system/hardware calls - meaning that they can be interchanged?

Specifically, if I'm using __gnu_parallel algorithms (that uses openmp) and I want to make the classes they call threadsafe may I use boost::mutex for the locking? or must I write my own mutex such as the one described here

//An openmp mutex.  Can this be replaced with boost::mutex? 
class Mutex {  
public:
    Mutex() { omp_init_lock(&_mutex); }
    ~Mutex() { omp_destroy_lock(&_mutex); }
    void lock() { omp_set_lock(&_mutex); }
    void unlock() { omp_unset_lock(&_mutex); }
private:
    omp_lock_t _mutex;
};

Edit, the link above to the openmp mutex seems to be broken, for anyone interested, the lock that goes with this mutex is along these lines

class Lock
{
public:
    Lock(Mutex& mutex) 
        : m_mutex(mutex), 
    m_release(false) 
    { 
        m_mutex.lock();
    }

    ~Lock() 
    {
        if (!m_release) 
            m_mutex.unlock(); 
    }

    bool operator() const 
    {
        return !m_release; 
    }

    void release()
    {
        if (!m_release)
        {
            m_release = true;
            m_mutex.unlock();
        }
    }

private:
    Mutex& m_mutex;
    bool m_release;
};
like image 442
Tom Avatar asked May 15 '11 10:05

Tom


People also ask

How mutex is implemented?

A mutex is the starting point for a critical section, which uses a mutex internally to see if it can enter a section of code. If the mutex is free, it sets the mutex and executes the code, only to release the mutex when done.

How mutex works internally?

The idea behind mutexes is to only allow one thread access to a section of memory at any one time. If one thread locks the mutex, any other lock attempts will block until the first one unlocks. However, how is this implemented? To lock itself, the mutex has to set a bit somewhere that says that it is locked.


2 Answers

This link provides a useful discussion:

http://groups.google.com/group/comp.programming.threads/browse_thread/thread/67e7b9b9d6a4b7df?pli=1

Paraphrasing, (at least on Linux) Boost::Thread and OpenMP both an interface to pthread and so in principle should be able to be mixed (as Anders says +1), but mixing threading technologies in this way is generally a bad idea (as Andy says, +1).

like image 101
Tom Avatar answered Oct 10 '22 03:10

Tom


You should not mix synchronization mechanisms. E.g. current pthreads mutex implementation is based on futex and it is different from previous pthreads implementations (see man 7 pthreads). If you create your own level of abstraction, you should use it. It should be considered what is your need - inter-thread or inter-process synchronization? If you need cooperation with code that uses boost::mutex, you should use boost::mutex in place of open mp. Additionally IMHO it is quite strange to use open mp library functions to realize mutex.

like image 45
Andy Avatar answered Oct 10 '22 04:10

Andy