From 5bee45756d5e8c66c5e93e31686f2ba38b255cf8 Mon Sep 17 00:00:00 2001 From: Denes Matetelki Date: Sat, 26 Feb 2011 18:35:23 +0100 Subject: [PATCH] Initial commit --- include/Mutex.hpp | 22 ++++++++++++++++++++++ src/Mutex.cpp | 26 ++++++++++++++++++++++++++ test/main_Mutex.cpp | 31 +++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 include/Mutex.hpp create mode 100644 src/Mutex.cpp create mode 100644 test/main_Mutex.cpp diff --git a/include/Mutex.hpp b/include/Mutex.hpp new file mode 100644 index 0000000..e15dd39 --- /dev/null +++ b/include/Mutex.hpp @@ -0,0 +1,22 @@ +#ifndef MUTEX_HPP +#define MUTEX_HPP + +#include + +class Mutex +{ +public: + Mutex(); + ~Mutex(); + + void lock(); + void unlock(); + +private: + + pthread_mutex_t m_mutex; + +}; + +#endif // MUTEX_HPP + diff --git a/src/Mutex.cpp b/src/Mutex.cpp new file mode 100644 index 0000000..9b051b7 --- /dev/null +++ b/src/Mutex.cpp @@ -0,0 +1,26 @@ +#include "Mutex.hpp" + + +Mutex::Mutex() +{ + pthread_mutex_init( &m_mutex, 0 ); +} + + +Mutex::~Mutex() +{ + pthread_mutex_destroy ( &m_mutex ); +} + + +void Mutex::lock() +{ + pthread_mutex_lock( &m_mutex ); +} + + +void Mutex::unlock() +{ + pthread_mutex_unlock ( &m_mutex ); +} + diff --git a/test/main_Mutex.cpp b/test/main_Mutex.cpp new file mode 100644 index 0000000..2130d4e --- /dev/null +++ b/test/main_Mutex.cpp @@ -0,0 +1,31 @@ +#include +#include + +#include "Mutex.hpp" + +Mutex m; +int counter = 0; + +void *functionC( void* params ) +{ + m.lock(); + counter++; + std::cout << "Counter value: " << counter << std::endl; + m.unlock(); + return 0; +} + + +int main() +{ + pthread_t thread1, thread2; + + pthread_create( &thread1, 0, &functionC, 0 ); + pthread_create( &thread2, 0, &functionC, 0 ); + + pthread_join( thread1, 0 ); + pthread_join( thread2, 0 ); + + return 0; +} +