12-28-2009
Mutexes, semaphores, and atomic operations have been around for decades, have been used by untold numbers of programmers, and have been examined, re-examined, and otherwise worked on by probably thousands of computer scientists and programmers of which probably more than a few have done Nobel-prize level work in science and/or mathematics.
They're mature, stable, and work without fail.
They're also just about as fast as possible.
If you think you can do better, go ahead. You may very well come up with something that's better. But be sure when you're done developing your alternative, you compare the performance you get to the OS-supplied mutexes, semaphores and atomic operations, because coming up with something better is going to be very hard to do.
But if you can do it, it would be great.
10 More Discussions You Might Find Interesting
1. Programming
Hi all,
I am working in a UNIX/C environment. I would like to understand more about MUTEX and Threads. Can someone explain me these
concepts and how they are related.
Vijay (2 Replies)
Discussion started by: vthasan
2 Replies
2. UNIX for Dummies Questions & Answers
Can anyone explain me what mutexes are in
multithreading environment? (2 Replies)
Discussion started by: sagar
2 Replies
3. Shell Programming and Scripting
A shell in crontab per 5 min write a file
B shell in crontab per 6 min read a file
how to lock the share file a ;avioid confilict in write and read?
Thx : -) (1 Reply)
Discussion started by: zz_xm
1 Replies
4. Programming
Hi, I have two tasks 'Read' and 'Write' which reads and writes on a file "abc.txt" respectively.
Now I need to restrict the Write operation on the file while Read is going on, But can allow two Reads at a time.
ie. two Reads can happen simultaneously, but Write can't happen at Read is going on.
... (3 Replies)
Discussion started by: satheeshalle
3 Replies
5. Programming
Hi,
can someone explain me the difference between mutex and semaphores?
Thanks (1 Reply)
Discussion started by: rvan
1 Replies
6. UNIX for Dummies Questions & Answers
can u tell me what is the exact difference b/w mutex and semaphore
and what is the diff b/w counting semaphore and binary semaphore
amit (1 Reply)
Discussion started by: amitpansuria
1 Replies
7. Shell Programming and Scripting
Hello Everyone,
I just joined this forum and this is my first post.
I would like to know how can I impliment basic read/write locks in perl. I have a database (file) which can be accessed simultaneously but has to be locked while writing.
If there is no such support in perl, my next... (6 Replies)
Discussion started by: superuser84
6 Replies
8. Programming
I am trying to use mutex in my multi-tread project, but they don't seem to work.
I have created a simple demonstration of the problem. This is NOT how I would use a mutex, only a demonstration of the problem:
#include <stdio.h>
#include <pthread.h>
int main()
{
int val;
... (3 Replies)
Discussion started by: ChrisWilliams
3 Replies
9. Programming
Hi all,
I have a scenario where I need to use the mutex locks. The mutex locks are working fine, but sometimes I am getting into the dead lock situation.
Below is the summary of my code :
MUTEX LOCK
performTask();
MUTEX UNLOCK.
In some cases I get into the situation where... (2 Replies)
Discussion started by: cjjoy
2 Replies
10. Programming
Hello,
I have got some issue with the struct variable with passed arguments
the variable in the sturct is only recognize the last value their assigned to
I'm pretty confused why the mutex didn't work out
here is my program:
#include<stdio.h>
#include<pthread.h>
pthread_mutex_t lock... (3 Replies)
Discussion started by: michael23
3 Replies
LEARN ABOUT DEBIAN
dispatch_semaphore_create
dispatch_semaphore_create(3) BSD Library Functions Manual dispatch_semaphore_create(3)
NAME
dispatch_semaphore_create, dispatch_semaphore_signal, dispatch_semaphore_wait -- synchronized counting semaphore
SYNOPSIS
#include <dispatch/dispatch.h>
dispatch_semaphore_t
dispatch_semaphore_create(long count);
long
dispatch_semaphore_signal(dispatch_semaphore_t semaphore);
long
dispatch_semaphore_wait(dispatch_semaphore_t semaphore, dispatch_time_t timeout);
DESCRIPTION
Dispatch semaphores are used to synchronize threads. The timeout parameter is creatable with the dispatch_time(3) or dispatch_walltime(3)
functions.
COMPLETION SYNCHRONIZATION
If the count parameter is equal to zero, then the semaphore is useful for synchronizing completion of work. For example:
sema = dispatch_semaphore_create(0);
dispatch_async(queue, ^{
foo();
dispatch_semaphore_signal(sema);
});
bar();
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
FINITE RESOURCE POOL
If the count parameter is greater than zero, then the semaphore is useful for managing a finite pool of resources. For example, a library
that wants to limit Unix descriptor usage:
sema = dispatch_semaphore_create(getdtablesize() / 4);
At each Unix FD allocation:
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
fd = open("/etc/services", O_RDONLY);
When each FD is closed:
close(fd);
dispatch_semaphore_signal(sema);
RETURN VALUES
The dispatch_semaphore_create() function returns NULL if no memory is available or if the count parameter is less than zero.
The dispatch_semaphore_signal() function returns non-zero when a thread is woken. Otherwise, zero is returned.
The dispatch_semaphore_wait() function returns zero upon success and non-zero after the timeout expires. If the timeout is DISPATCH_TIME_FOR-
EVER, then dispatch_semaphore_wait() waits forever and always returns zero.
MEMORY MODEL
Dispatch semaphores are retained and released via calls to dispatch_retain() and dispatch_release().
CAVEATS
Dispatch semaphores are strict counting semaphores. In other words, dispatch semaphores do not saturate at any particular value. Saturation
can be achieved through atomic compare-and-swap logic. What follows is a saturating binary semaphore:
void
saturating_semaphore_signal(dispatch_semaphore_t dsema, int *sent)
{
if (__sync_bool_compare_and_swap(sent, 0, 1)) {
dispatch_semaphore_signal(dsema);
}
}
void
saturating_semaphore_wait(dispatch_semaphore_t dsema, int *sent)
{
*sent = 0;
dispatch_semaphore_wait(dsema, DISPATCH_TIME_FOREVER);
}
SEE ALSO
dispatch(3), dispatch_object(3)
Darwin May 1, 2009 Darwin