LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to implement own mutex in C++ (https://www.linuxquestions.org/questions/programming-9/how-to-implement-own-mutex-in-c-4175550140/)

sindy07 08-07-2015 10:33 AM

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

auge 08-07-2015 10:42 AM

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.

sindy07 08-07-2015 11:18 AM

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

dugan 08-07-2015 11:26 AM

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

auge 08-07-2015 11:30 AM

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.

sindy07 08-07-2015 11:36 AM

@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.