Search code examples
c++boostboost-mutex

Why is identifier "MutexType" is undefined?


Attempting to use the boost library to create a system wide mutex from the docs

#include <boost/interprocess/sync/interprocess_mutex.hpp>
#include <boost/interprocess/sync/scoped_lock.hpp>
#include <boost/interprocess/sync/named_mutex.hpp>

using namespace boost::interprocess;

MutexType  mtx;


int main()
{
    return 0;
}

I am running visual studio code and compiling my code using msys2-MINGW64 environment like so g++ mutexes.cpp -lboost_system but this does not seem to work and I am getting this error in the bash console

mutexes.cpp:8:1: error: 'MutexType' does not name a type
    8 | MutexType  mtx;
      | ^~~~~~~~~


Solution

  • The linked documentation specifically means "any mutex type":

    //Let's create any mutex type:
    MutexType mutex;
    

    It follows it up with a concrete, more elaborate Anonymous mutex example and Named mutex example.

    Which types are eligible is documented at scoped_lock:

    scoped_lock is meant to carry out the tasks for locking, unlocking, try-locking and timed-locking (recursive or not) for the Mutex. The Mutex need not supply all of this functionality. If the client of scoped_lock does not use functionality which the Mutex does not supply, no harm is done

    In practice the minimal useful interface required will be that of the standard library BasicLockable concept although many mutex implementations also model Lockable and Mutex concepts.