vlc_mutex_locker: automatic mutex locking for C++
authorRémi Denis-Courmont <rdenis@simphalempin.com>
Mon, 20 Oct 2008 19:45:16 +0000 (22:45 +0300)
committerRémi Denis-Courmont <rdenis@simphalempin.com>
Mon, 20 Oct 2008 20:15:51 +0000 (23:15 +0300)
include/vlc_threads.h

index 4ab92c2..7b66e5b 100644 (file)
@@ -448,4 +448,27 @@ static inline void barrier (void)
 #define vlc_thread_join( P_THIS )                                           \
     __vlc_thread_join( VLC_OBJECT(P_THIS) )
 
+#ifdef __cplusplus
+/**
+ * Helper C++ class to lock a mutex.
+ * The mutex is locked when the object is created, and unlocked when the object
+ * is destroyed.
+ */
+class vlc_mutex_locker
+{
+    private:
+        vlc_mutex_t *lock;
+    public:
+        vlc_mutex_locker (vlc_mutex_t *m) : lock (m)
+        {
+            vlc_mutex_lock (lock);
+        }
+
+        ~vlc_mutex_locker (void)
+        {
+            vlc_mutex_unlock (lock);
+        }
+};
+#endif
+
 #endif /* !_VLC_THREADS_H */