12-28-2009
Can Mutex be replaced with anything?
Hi All,
To avoid race condition, instead of using mutex, semaphore, spinlock etc.... Is there any other mechanism by which we can avoid race condition in an multi-threading environment.
-Thanks
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 FREEBSD
sema_value
SEMA(9) BSD Kernel Developer's Manual SEMA(9)
NAME
sema, sema_init, sema_destroy, sema_post, sema_wait, sema_timedwait, sema_trywait, sema_value -- kernel counting semaphore
SYNOPSIS
#include <sys/types.h>
#include <sys/lock.h>
#include <sys/sema.h>
void
sema_init(struct sema *sema, int value, const char *description);
void
sema_destroy(struct sema *sema);
void
sema_post(struct sema *sema);
void
sema_wait(struct sema *sema);
int
sema_timedwait(struct sema *sema, int timo);
int
sema_trywait(struct sema *sema);
int
sema_value(struct sema *sema);
DESCRIPTION
Counting semaphores provide a mechanism for synchronizing access to a pool of resources. Unlike mutexes, semaphores do not have the concept
of an owner, so they can also be useful in situations where one thread needs to acquire a resource, and another thread needs to release it.
Each semaphore has an integer value associated with it. Posting (incrementing) always succeeds, but waiting (decrementing) can only success-
fully complete if the resulting value of the semaphore is greater than or equal to zero.
Semaphores should not be used where mutexes and condition variables will suffice. Semaphores are a more complex synchronization mechanism
than mutexes and condition variables, and are not as efficient.
Semaphores are created with sema_init(), where sema is a pointer to space for a struct sema, value is the initial value of the semaphore, and
description is a pointer to a null-terminated character string that describes the semaphore. Semaphores are destroyed with sema_destroy().
A semaphore is posted (incremented) with sema_post(). A semaphore is waited on (decremented) with sema_wait(), sema_timedwait(), or
sema_trywait(). The timo argument to sema_timedwait() specifies the minimum time in ticks to wait before returning with failure.
sema_value() is used to read the current value of the semaphore.
RETURN VALUES
The sema_value() function returns the current value of the semaphore.
If decrementing the semaphore would result in its value being negative, sema_trywait() returns 0 to indicate failure. Otherwise, a non-zero
value is returned to indicate success.
The sema_timedwait() function returns 0 if waiting on the semaphore succeeded; otherwise a non-zero error code is returned.
ERRORS
The sema_timedwait() function will fail if:
[EWOULDBLOCK] Timeout expired.
SEE ALSO
condvar(9), locking(9), mtx_pool(9), mutex(9), rwlock(9), sx(9)
BSD
February 1, 2006 BSD