2

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;
      | ^~~~~~~~~

loaded_dypper
  • 262
  • 3
  • 12
  • A quick check of the boost documentation and I can see no type called `MutexType` what makes you think it exists? – john Aug 13 '22 at 14:53
  • 3
    If you are going by this [documentation](https://www.boost.org/doc/libs/1_80_0/doc/html/interprocess/synchronization_mechanisms.html#interprocess.synchronization_mechanisms.mutexes) then you will see that `MutexType` is a type **you** are supposed to define, not one that boost defines for you. – john Aug 13 '22 at 14:55
  • @john i am following the docs check the link that i provided – loaded_dypper Aug 13 '22 at 14:55
  • Yes, please see my previous comment. – john Aug 13 '22 at 14:55
  • @john could you explain how to define a `MutexType` – loaded_dypper Aug 13 '22 at 14:56
  • scoped_lock is just a wrapper around a mutex. So pick one of the other mutex types (eg. `interprocess_mutex` or `named_mutex`). – john Aug 13 '22 at 14:58
  • @john like so? `std::mutex mtx;` `scoped_lock lock(mtx);` – loaded_dypper Aug 13 '22 at 15:01
  • Yes, I think so, not 100% sure if boost mutex is compatible with the mutexes in the standard library. Maybe you would know that better than me. – john Aug 13 '22 at 15:02

1 Answers1

1

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.

sehe
  • 374,641
  • 47
  • 450
  • 633