How to implement own mutex in C++
Hi,
I am in process of implementing my own blocking queue class. So requirement is when I add a element to a queue and if no space available caller should be blocked until space is available. Similarly for delete. I am trying to do it in C++. Please help me in implementing own blocking queue class, I mean own implementation of mutex with conditional variable. Thanks, Sindy |
This has been done in the ACE-Project and you can use different aproaches to that queue- put/get methods with semaphores. Best is to use premade classes, when you insist on creating them yourself:
template <class T> class Blocking_Queue { public: Blocking_Queue() { pthread_mutex_init(&_lock, NULL); pthread_cond_init(&_cond, NULL); } ~Blocking_Queue() { pthread_mutex_destroy(&_lock); pthread_cond_destroy(&_cond); } void put(T t) { pthread_mutex_lock(&_lock); _queue.push(t); pthread_cond_signal(&_cond); pthread_mutex_unlock(&_lock); } T pull() { pthread_mutex_lock(&_lock); while(_queue.empty()) { pthread_cond_wait(&_cond, &_lock); } T t = _queue.front(); _queue.pop(); pthread_mutex_unlock(&_lock); return t; } private: std::queue<T> _queue; pthread_cond_t _cond; pthread_mutex_t _lock; } ----- (^^ This is an example of something I wrote at another place, feel free to use it) Now you can extend this by making put-method check for std::queue<T>::size and return a fail when the queue is too big (make the caller wait in a way he wants) or block until the size is as small as you like, this of course also blocks the execution of the calling code. |
Thanks for the quick response Auge.
But actually I am trying to implemented own mutex instead of using a standard defined mutex. Is it possible to get mutex implementation? Thanks, Sindy |
My impression is that mutexes are typically implemented using assembly language to take advantage of the relevant hardware instructions.
The wikipedia article on mutexes lists several algorithms for implementing them in software. https://en.wikipedia.org/wiki/Mutual_exclusion |
The mutex (mutual exclusion of access to a resource) is part of the kernel. You need to use functionality of the operating-system to have an atomic and static area to store your exclusion information.
Of course you can create a "mutex" of your own by means of a C++-class that locks, based on a variable and then some loop waits until it is unset, but it will be shady work and your OS has it "in the mix" already. For linux ... look in linux/mutex.h My advice: Never create thread-synchronization from scratch by means of your programming-language alone. There is an OS-layer for that for a good reason. |
@Dugan... Yes verified those algorithms... I am trying to make it as conditional wait like
thread 1: pushQ() { if (Q(full)) { wait(condition1); } add(); release(condition2); } thread 2: popQ() { if (Q(empty)) { wait(condition2); } delete(); release(condition1); } It is just a prototype, I am struck in completing this. @Auge: Yes that is true.. but I am trying to explore myself :) |
All times are GMT -5. The time now is 02:49 AM. |