GilAwareMutex
-
class GilAwareMutex
A mutex class that handles the GIL (Global Interpreter Lock) automatically.
The GilAwareMutex class provides a mutex that automatically releases and re-acquires the GIL when locking and unlocking. This is useful in multi-threaded applications that interact with the Python interpreter, as it allows other Python threads to continue executing while the lock is held.
Public Functions
-
inline void lock()
Locks the GilAwareMutex.
This function acquires the GilAwareMutex lock. If the lock is not available within 100 milliseconds, it throws a std::runtime_error with a potential deadlock message.
- Throws:
std::runtime_error – If the GilAwareMutex is unable to acquire the lock within 100ms.
-
inline void unlock()
Unlocks the GilAwareMutex.
This function unlocks the GilAwareMutex by releasing the lock. It ensures that the GIL (Global Interpreter Lock) is re-acquired after unlocking. This is useful in multi-threaded applications that interact with the Python interpreter, as it allows other Python threads to continue executing while the lock is released.
Note
This function should only be called after acquiring the lock using the
lock()function.
-
inline bool try_lock()
Tries to lock the GilAwareMutex.
This function attempts to acquire the GilAwareMutex lock. If the lock is available, it will be acquired and the function will return true. If the lock is not available, the function will return false.
- Returns:
True if the lock was acquired successfully, false otherwise.
-
inline void lock()