1
1
Fork 0
mirror of https://github.com/QB64-Phoenix-Edition/QB64pe.git synced 2024-08-30 23:30:16 +00:00
QB64-PE/internal/c/libqb/include/mutex.h
Matthew Kilgore 7ef15653fd Add generic thread, mutex, and condvar API
This adds generic APIs to libqb for handling thread's, mutex's, and
condition variables. On Linux and OSX these are implemented via the ones
provided by pthreads. On Windows they're implemented via the ones
provided by the Win32 API.

For compiling, the code itself is not conditional, but the Makefile
includes logic to decide which implementation to pick.

Note that it would have been nice to simply use std::thread and friends
from C++11, however using them on MinGW appears to be a bit messy. Since
using the Windows ones directly isn't that hard this was an easy compromise.
2022-06-11 22:47:06 -04:00

29 lines
680 B
C++

#ifndef INCLUDE_LIBQB_MUTEX_H
#define INCLUDE_LIBQB_MUTEX_H
struct libqb_mutex;
// Allocates and frees a Mutex. Mutex is created unlocked.
struct libqb_mutex *libqb_mutex_new();
void libqb_mutex_free(struct libqb_mutex *);
// Lock and unlock the Mutex
void libqb_mutex_lock(struct libqb_mutex *);
void libqb_mutex_unlock(struct libqb_mutex *);
// Locks a mutex when created, and unlocks when the guard goes out of scope
class libqb_mutex_guard {
public:
libqb_mutex_guard(struct libqb_mutex *mtx) : lock(mtx) {
libqb_mutex_lock(lock);
}
~libqb_mutex_guard() {
libqb_mutex_unlock(lock);
}
private:
struct libqb_mutex *lock;
};
#endif