|
cppthread 1.1.16
C++ Thread Library
|
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>
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 | |
| mutex * | cppthread::g_system_mutex = nullptr |
| The system mutex. | |
This file includes the implementation used by the cppthread environment.
Definition in file mutex.cpp.
|
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.
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.
You could even create a function which returns a reference to that local mutex (just make sure you don't copy that mutex).
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().


| 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.
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.