cppthread 1.1.16
C++ Thread Library
Classes | Functions | Variables
mutex.cpp File Reference

Implementation of the Thread Runner and Managers. More...

#include "cppthread/mutex.h"
#include "cppthread/exception.h"
#include "cppthread/guard.h"
#include "cppthread/log.h"
#include <snapdev/timespec_ex.h>
#include <string.h>
#include <sys/time.h>
#include <snapdev/poison.h>
Include dependency graph for mutex.cpp:

Go to the source code of this file.

Classes

class  cppthread::detail::mutex_impl
 The implementation of the mutex. More...
 

Functions

void cppthread::create_system_mutex ()
 Function used to initialize the system mutex.
 

Variables

mutexcppthread::g_system_mutex = nullptr
 The system mutex.
 

Detailed Description

This file includes the implementation used by the cppthread environment.

Definition in file mutex.cpp.

Function Documentation

◆ create_system_mutex()

void cppthread::create_system_mutex ( )
extern

This is an internal function called by the logger constructor.

This is unfortunate, but the system mutex create relies on the log object to exist and be properly initialized before it gets created. So we call this function from the constructor of the logger.

Note
This means it's not exact properly initialized since the constructor did not yet return. However, it is guaranteed to work properly since we do not allow derivations or virtual tables.

The logger constructor is called whenever the cppthread library is loaded and that happens in your main thread at initialization time. This means any of your code can access the g_system_mutex as required.

This function is not defined externally so that other users can't call it from the outside (there would be no need anyway). Just in case, if called a second time, the function writes an error message and fails with a call to std::terminate().

Another way to safely create a new mutex is to use a static variable in a function. The initialization of that static variable will always be safe so that mutex will be available to all your threads even if you already had multiple threads the first time that function was called.

void myfunc()
{
// here "m" is a safe to use mutex
...do atomic work here...
}
Lock a mutex in an RAII manner.
Definition guard.h:42
A mutex object to ensures atomicity.
Definition mutex.h:55

You could even create a function which returns a reference to that local mutex (just make sure you don't copy that mutex).

Note
Although this function gets called from the logger contructor, it is not used by the logger at all.

Definition at line 926 of file mutex.cpp.

References cppthread::create_system_mutex(), and cppthread::g_system_mutex.

Referenced by cppthread::logger::logger(), and cppthread::create_system_mutex().

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ g_system_mutex

mutex * cppthread::g_system_mutex = nullptr

This mutex is created and initialized whenever this library is loaded. This means it will always be ready for any library that depends on the cppthread library.

It should be used for things that may happen at any time and require to be done by one thread at a time. For example, a class with a get_instance() may want to lock this mutex, do it's instance creation and then unlock the mutex.

The mutex locking should be done using the guard class.

ptr * get_instance()
{
cppthread::guard lock(*g_system_mutex);
...proceed with your instance allocation...
}

Because of the order in which things get initialized under Linux, you can be sure that this mutex will be ready for use as soon as the cppthread is loaded. So you can even use it in your own C++ initialization (i.e. your global objects.)

Obviously, if you also have your own mutex, you need to initialize it and therefore you may have problems in your initialization process (i.e. your C++ globals may not get initialized in the correct order–the mutex may get initialized after your other parameters...)

Definition at line 888 of file mutex.cpp.

Referenced by cppthread::create_system_mutex().

This document is part of the Snap! Websites Project.

Copyright by Made to Order Software Corp.