LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-07-2015, 10:33 AM   #1
sindy07
LQ Newbie
 
Registered: Mar 2012
Posts: 8

Rep: Reputation: Disabled
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
 
Old 08-07-2015, 10:42 AM   #2
auge
Member
 
Registered: May 2002
Location: Germany
Distribution: CentOS, Debian, LFS
Posts: 100
Blog Entries: 1

Rep: Reputation: Disabled
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.
 
Old 08-07-2015, 11:18 AM   #3
sindy07
LQ Newbie
 
Registered: Mar 2012
Posts: 8

Original Poster
Rep: Reputation: Disabled
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
 
Old 08-07-2015, 11:26 AM   #4
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,198

Rep: Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307
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

Last edited by dugan; 08-07-2015 at 11:28 AM.
 
Old 08-07-2015, 11:30 AM   #5
auge
Member
 
Registered: May 2002
Location: Germany
Distribution: CentOS, Debian, LFS
Posts: 100
Blog Entries: 1

Rep: Reputation: Disabled
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.
 
Old 08-07-2015, 11:36 AM   #6
sindy07
LQ Newbie
 
Registered: Mar 2012
Posts: 8

Original Poster
Rep: Reputation: Disabled
@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

Last edited by sindy07; 08-07-2015 at 11:38 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Mutex Unlock pratham29 Linux - Newbie 1 11-19-2013 01:46 PM
[SOLVED] Is it necessary to mutex lock here?(C++) vendtagain Programming 2 08-14-2011 12:20 AM
Priority in mutex manohar Programming 2 02-04-2011 03:21 PM
Why does pthread_cond_wait need a mutex? fuzzyBuzz Programming 4 06-01-2009 02:16 PM
Mutex new2lunix Programming 1 12-02-2008 08:12 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration