Posix Semaphore Use - Releasing all resources?


 
Thread Tools Search this Thread
Top Forums Programming Posix Semaphore Use - Releasing all resources?
# 1  
Old 02-10-2010
Posix Semaphore Use - Releasing all resources?

I am attempting to write a program with multiple POSIX threads. I want to ensure all threads are released at the same time. I am (trying to) use a semaphore to accomplish this.

Without too much irrelevant information, I declare my semaphore with 0 of 25 resources available. Is there a way to instantly make all resources available? It seems the only way to make a resource available is with sem_post(&semaphore_name), but that only makes one available.

I would like to make all 25 available at once so my threads all become unblocked at once and run concurrently.

Any suggestions?

Thank you!
# 2  
Old 02-10-2010
Unfortunately sem_post doesn't support multiple posts at once. SYSV IPC sems do, I believe, but they may have higher overhead. You might also check out 'man pthread_cond_init'. A condition variable isn't quite a semaphore, but does support launching multiple threads with one signal.
# 3  
Old 02-10-2010
Hello Snowbarr,

Well, it's a tradition in POSIX threads to avoid semaphores whenever possible ... Corona688 idea to use condition variables (with a broadcast) is a good, see man pthread_cond_broadcast() and pthread_cond_wait() among others,

Another possibility, that may even better fit, could be using a barrier: see man pthread_barrier_init() and pthread_barrier_wait().

HTH,
Loïc
# 4  
Old 02-10-2010
Quote:
but does support launching multiple threads with one signal.
Humm, not quite so. Remember condition variables have an associated mutex. Only one thread is actually unblocked. If pthread_cond_signal() is used and several threads are blocked the thread that is actually signaled and acquires the associated mutex is determined by the scheduling policy. If pthread_cond_broadcast() is used all threads that are blocked are notified but still, at the end of the day, only one thread can acquire the mutex and proceed.

A better approach might be to use Pthread barriers.
# 5  
Old 02-10-2010
Quote:
Originally Posted by fpmurphy
Humm, not quite so. Remember condition variables have an associated mutex. Only one thread is actually unblocked.
I stand corrected, thank you.
Quote:
Originally Posted by Loic Domaigne
Well, it's a tradition in POSIX threads to avoid semaphores whenever possible
How come?
# 6  
Old 02-10-2010
The pthread_cond_broadcast() function shall unblock all threads currently blocked on the specified condition variable cond.

opengroup /functions/pthread_cond_signal.html

yes, we don't use semaphores.
# 7  
Old 02-19-2010
Quote:
Originally Posted by Corona688
Quote:
Originally Posted by Loic Domaigne
Well, it's a tradition in POSIX threads to avoid semaphores whenever possible
How come?
Well, this is a joke coming from the comp.programming.threads NG.

The reason behind this joke is: in most of the cases, POSIX threads offers synchronization mechanisms that surpass what semaphores can offer. For instance, a mutex could be seen as a binary semaphore. But mutex is more efficient in the uncontended case (indeed, there is no kernel involvement. This is not the case for semaphore). Beside that, mutex can offer priority inheritance, something impossible with semaphore.

From my own experience, most of the time where semaphores are used in Pthreads program often results from a lack of Pthreads understanding. There are only very few specific cases where semaphores make more sense.

Cheers,
Loïc.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

PLINK from Windows not releasing job

Hi all, I would greatly appreciate some help with this. I may not even be on the right path but I think I am close. I have a Synology NAS that I am running rsync jobs on to offload to USB3. These work fine. I have backup software on our Windows Server 2008R2 that I have now realized can... (2 Replies)
Discussion started by: stefshuuj
2 Replies

2. AIX

AIX 6.1: Releasing Memory and Page Space

Hi everyone, i have a question about the Memory Management in AIX 6.1. I have - 128 GB RAM and - 70 GB Page Space. The application i am running on this machine is doing some operations in perl. These are done only once a day and uses both memory and paging space. My problem... (1 Reply)
Discussion started by: Haichao
1 Replies

3. AIX

Releasing a Port

Hey Guys, I need a help, After I upgraded my server to AIX 6.1.4 the port 80 is occupied by a Java process bash-3.00# netstat -Aan | grep "*.80" f1000700007d7bb0 tcp 0 0 *.80 *.* LISTEN bash-3.00# rmsock f1000700007d7bb0 tcp usage: rmsock Addr... (2 Replies)
Discussion started by: kkeng808
2 Replies

4. Solaris

Releasing the swap space Solaris

Hi Guys ! I have found this problem many times that the swap space (/tmp space more precisely )of my Server becomes full specially when i run a complete back up of the server . once the /tmp space is full I am not able to run simple commands as i get error like : fork no space on... (4 Replies)
Discussion started by: Paarth
4 Replies

5. Solaris

Releasing the swap space Solaris

Hi Guys ! I have found this problem many times that the swap space (/tmp space more precisely )of my Server becomes full specially when i run a complete back up of the server . once the /tmp space is full I am not able to run simple commands as i get error like : fork no space on... (6 Replies)
Discussion started by: Paarth
6 Replies

6. Filesystems, Disks and Memory

Veritas HSM not releasing tapes

I have a volume managed by Veritas Storage Migrator 6.0 running on Solaris 10. It has one managed volume and one tape robot it shares with netpackup (separate volume pools). When I check the process tree I am not seeing any migration processes running, but when I check the panel on the tape robot... (0 Replies)
Discussion started by: ilikecows
0 Replies

7. Linux

ext3 file system not releasing space

Hi all i am facing a problem with ext3 file system df -h is showing 19gb is used even if there is not a single file on the mount point /dev/mapper/vg01-archive 55G 19G 33G 36% /archive_log OS == Linux cdrsvr 2.6.9-55.ELsmp #1 SMP Fri Apr 20 17:03:35 EDT 2007... (1 Reply)
Discussion started by: ajays
1 Replies

8. Solaris

Checking/ Releasing File Locks in Solaris

Hi, One of my prod jobs using Ab Initio (which is Solaris Based) returned this error: ========= Error from Reformat_3.000 on f7j21-01.xxxx3.com ========= Failed opening file for flow: Resource temporarily unavailable Flow = "Flow_11.000" connected to output port "out0" of "Reformat_3.000"... (0 Replies)
Discussion started by: teenu18
0 Replies

9. Programming

replacing of the mutex with POSIX semaphore

Hi, does anybody know, if it is possible to replace mutex with POSIX semaphore? I'm gonna to rewrite old thread's program with processes and I don't want rewrite most of the code...So if I replace the threads with processes and mutexes with semaphores WILL IT WORK? Before, the critical... (1 Reply)
Discussion started by: michael26100
1 Replies

10. Programming

Problem with releasing semaphore lock

Hi, I am trying to write stuff to a shared memory using a writer, and reading the corresponding stuff using a reader. I am facing problems while releasing the lock, as a result of which I am having segmentation faults. The code is as follows... /********** writer.c ***********/ ... (1 Reply)
Discussion started by: jacques83
1 Replies
Login or Register to Ask a Question