ThinSQLite++
A thin, safe and convenient modern C++ wrapper for SQLite API.
Loading...
Searching...
No Matches
mutex Class Referencefinal

SQLite Mutex. More...

Inheritance diagram for mutex:
handle< sqlite3_mutex, mutex >

Public Types

enum  type { fast = SQLITE_MUTEX_FAST , recursive = SQLITE_MUTEX_RECURSIVE }
 Type of mutex to allocate for alloc()
 

Public Member Functions

void lock () noexcept
 Lock the mutex.
 
bool try_lock () noexcept
 Try to lock the mutex.
 
void unlock () noexcept
 Unlock the mutex.
 
void operator delete (void *) noexcept
 Operator delete for a fake pointer is no-op.
 
sqlite3_mutexc_ptr () const noexcept
 Access the real underlying SQLite type.
 

Static Public Member Functions

static std::unique_ptr< mutexalloc (type t) noexcept
 Allocate a new mutex.
 
static mutexfrom (sqlite3_mutex *obj) noexcept
 Create fake pointer from the underlying SQLite one.
 

Detailed Description

SQLite Mutex.

This is a fake wrapper class for sqlite3_mutex.

Unlike other wrappers in this library the interface of this class does not match the names of SQLite C interface. Instead is made to conform to Lockable C++ standard library concept. This allows you to use it in standard lock related machinery like std::unique_lock or std::lock_guard

In many cases SQLite can return nullptr mutexes due to compile-time or runtime disabling of synchronization. While calling lock(), try_lock() and unlock() on a nullptr this pointer should work fine on all compilers (underlying SQLite functions all support null pointers) it is technically an undefined behavior in C++ (sigh). To avoid this and deal with null mutex pointers safely see lock_adapter class

#include <thinsqlitepp/mutex.hpp>

See also
lock_adapter

Member Function Documentation

◆ alloc()

static std::unique_ptr< mutex > alloc ( type t)
inlinestaticnoexcept

Allocate a new mutex.

Equivalent to sqlite3_mutex_alloc

Note that the interface to this function deliberately disallows access to internal static SQLite mutexes. Accroding to SQLite docs "[s]tatic mutexes are for internal use by SQLite only".

If you do require access to one of the static mutexes you can use from(sqlite3_mutex_alloc(SQLITE_MUTEX_XXX)) manually.

Parameters
ttype of the mutex to allocate
Returns
Newly allocated mutex of nullptr if the SQLite implementation is unable to allocate a mutex (e.g. if it does not support mutexes).

◆ lock()

void lock ( )
inlinenoexcept

Lock the mutex.

Equivalent to sqlite3_mutex_enter

◆ try_lock()

bool try_lock ( )
inlinenoexcept

Try to lock the mutex.

Equivalent to sqlite3_mutex_try

◆ unlock()

void unlock ( )
inlinenoexcept

Unlock the mutex.

Equivalent to sqlite3_mutex_leave