Using a semaphore

Hi,
I have two threads that read from the same resource. To see that two contemporary readings don't provoke an error, I split the reading, putting "red light" when a thread start reading, and "green light" when it stop reading. I used a semaphore in the following way:
Semaphore fileAvailable;
File myfile;
public class MyClass {
//Some code
//First thread:
try {
        fileAvailable.acquire();
        } catch (InterruptedException ex) {
                     ex.printStackTrace();
                   method1ReadingMyfile();
                   fileAvailable.release();
//Other code
//Second thread:
try {
        fileAvailable.acquire();
        } catch (InterruptedException ex) {
                     ex.printStackTrace();
                   method2ReadingMyfile();
                   fileAvailable.release();
}Is it correct?

Is it correct?I don't think so: if something has failed (the 'catch' clause) it's certainly
not safe to manipulate the shared resource. Also, the name of your
semaphore 'fileAvailable' suggests that a file can not be available
during the entire process.
The moment a file becomes available shouldn't a 'producer' thread
notify the 'consumers'? Synchronizing and waiting can do the job.
kind regards,
Jos

Similar Messages

  • Do I need to use a semaphore when reading/writing a functional global from reentrant VIs?

    I have a program that spawns multiple reentrant VIs running simultaneously.  These VIs write their status to a functional global.  The VI that monitors them periodically polls this global to check out how they're doing and takes action based on their collective status.  (BTW, I'll mention that this monitoring VI is NOT the parent that spawned the reentrants, just in case this might affect memory management as it pertains to my question.)
    Anyway, 90% of the time, stuff goes off without a hitch.  However, once in a while the whole thing hangs.  I'm wondering if there's any chance that I've overlooked something and that some kind of collision is occurring at the global.  If that's the case, then should I be setting a semaphore for the global read/writes?
    And, if this is a problem, then there is something deep about functional globals that I don't yet understand.  My notion of them is that they should negate the need for a semaphore, since there is only one global instance, which cannot be simultaneously called by the various reentrants.  Indeed, this is arguably THE WHOLE POINT about functional globals, is it not?  Or am I missing something?
    Thanks,
    Nick 
    "You keep using that word. I do not think it means what you think it means." - Inigo Montoya

    Thanks Uwe,
    This is a good hunch.  However, functional globals typically run at "subroutine" priority.  With this priority, it is not possible to select a specific execution system; it is always "same as caller."
    I will try your suggestion by switching to "time-critical" priority.  However, I do not know if this could lead to a different set of issues (non-determinism?).  It will probably take a little while to hear back from my guys on whether this makes a difference or not, because the error is sporadic, and sometimes doesn't come along for quite a while.
    While probing all of this, I looked at the execution settings for my reentrant VI.  It has standard settings: "normal" priority, running in the "same as caller" execution system.  My impression has always been that LV creates the clones with unique names.  This allows the clones to be in the same execution system with no problem, and the fact that the execution dialog allows me to choose "same as caller" for a reentrant VI supports this assertion.  This is logical, since there could potentially be many more clones than available execution systems.  "Preallocate clone for each instance" is selected, which is what I want, I think, though I don't know if it matters in my application.
    In summary, I am trying out your suggestion, but with skepticism.  Any other suggestions from anyone out there?  Any misunderstandings on my part that need clarification?
    Thanks,
    Nick 
    "You keep using that word. I do not think it means what you think it means." - Inigo Montoya

  • Use of Semaphore in addition to db connection pool

    Hi,
    I have a multi-threaded app, about 4 threads, reading from a local DB then pushing to a web service,
    then writing a response back to the local DB. I'm using Commons DBCP with a size of 20.
    Do you think there is any added benefit to using semaphore to safe-guard access to the DB pool?
    Will this prevent any deadlocks in a more thorough manner?

    iketurner wrote:
    Yes,
    after I am done with the connection I explicitly close it:
    conn.closeThis should handle things for me.
    I will add in the use of finally
    Will this make a difference you think, even though I am using conn.close()?whenever you use a "resource" you should close it in a finally block. otherwise you can leak resources if you encounter problems (i.e. throw exception):
    Connection c = null;
    try {
      c = ...;
      // do something with c
    } finally {
      if(c != null) {
        c.close();
    }this is a must if you are using db connections, otherwise an exception thrown during the "do something with c" part will cause your connection to not be closed. then you would encounter problems later as all you run out of connections in your connection pool and your application hangs.

  • Using the same object in two actors at the same time

    Hello,
    I'm in doubt.  In my application, for example i have three Hardware classes - Database, DAQ1, DAQ2.
    Database class has one main method (except initialization methods) - Log Measure.
    DAQ1 class also has one method - Get Voltage, and DAQ2 class has one method - Get Current.
    Then i define two HAL (Hardware Abstraction Layer) classes - Measurement1 and Measurement2.
    Each of those classes have two main methods, respectively - Measure Voltage and Measure Curent. They also have some initilziation methods.
    In code, i create three objects of Hardware classes - Database, DAQ1 and DAQ2 and initiaze it. For example in Database private cluster i have DB RefNum, in DAQ1&2 i have device addresses etc.
    Next I initialzie Measurement1 with two hardware objects - Database and DAQ1, and in the same way - Measurement2 with Database, and DAQ2 objects.
    What Measure Voltage and Measure Curent (methods of Measurement1 and Measurement1 objects) do?
    Measure Voltage method call DAQ1::Get Voltage method, then Database::Log Measure.
    Similarly, Measure Current method call DAQ2::Get Voltage method, then Database::Log Measure.
    Till now it's clear i think. It should work properly.
    But now i incorporate Actor Framework, and define Measurement1 and Measurement2 as an actors.
    Now i can run Measurement1 and Measurement2 at the same time, simultaneously, but this two actors share one object - Database. So my question - what is when actors want to use Database::Log measure method at the same time and this method is time cosuming (for example large pack of data).
    Is one actor waits for second to stop executing this method of one shared object? I think yes because reentrancy setting (http://zone.ni.com/reference/en-XX/help/371361J-01/lvconcepts/reentrancy/)
    But what, for example if i share one Hardware object of device (ADC converter) beetwen two HAL classes (Measurement1 and Measurement2). This Hardware object has two methods, for ex. Get Voltage At channel 1, and Get Voltage At Channel 2. Both of this methods cannot by call at the same time, because in physical device i can measure only at one channel in the same time.
    Let the Get Voltage At channel X meausere take 10 seconds. Now i have huge chance, that Measurement 1 actor call Get Voltage At channel 1 method, and Measurement 2 wants to call Get Voltage At channel 2, when Get Voltage At channel 1 is in process. How to inform actor to wait till second actor release device resources?
    Regards,
    Kacper

    Dear Kacper,
    the way I understood, there are two separate questions in this post, namely:
    I'm using a shared resource in multiple actors, and the code accessing the resource can take quite some time to execute. How can can I ensure this does not mess up the timing of one or all accessors?
    I'm using a shared resource in multiple actors, and the code accessing the resource can generate errors if I try to access it from multiple places. How can I ensure synchronization between all accessors?
    As for the first question, the optimal solution to separate the resource prone to timing issues in a different thread/actor. Put anything that is not strictly timed (file access, network comunication) in a separate loop. For each actor, you will get queue references to communicate with, so instead of logging the measurements in the actors where they are made, just queue them up for a different loop to process.
    For the second part, the same logic still applies. Ideally, every singular resource should be handled by its own thread and nowhere else, so if anything else needs data from/to said resource, it can send a request to the dedicated thread or actor.
    If, for some reason, this is not sufficient, you have to handle synchronization in some other way. There are a lot of techniques here, for example:
    Create a named semaphore or lock as a part of the class. Have class functions use the semaphore before accessing the resource.
    Use actor messages. Have a "resource in use" or "resource released" messages sent to all users whenever obtaining or releasing said resource.
    Have a separate actor handle all resources, awarding them to threads needing them. This method also allows setting priorities between requests.
    These are just a few examples, there are many other options.
    Please let me know if this was helpful. 
    Kind regards:
    Andrew Valko
    National Instruments Hungary

  • Lock and semaphore, what's better method for my case

    Hi all,
    I'm implementing a classic case: a consumption queue with infinite capacity. That's say I have a queue of infinity capacity, a thread to put objects into the queue, another thread take it out. Pseudo code is smth like below:
    void put(Object o) {
    put message into the queue
    if (consume thread is waiting) {
    lock();
    signal();
    unlock();
    void eat() {
    if (queue not empty)
    take object out;
    else
    lock;
    wait for signal from producer;
    wake up and take object out;
    unlock;
    I don't know if I should use semaphore or Lock interface to get the job done. Do you know which one is better in my case? I'm writing an apps which is very sensitive in latency so basically I want the eater to get the object as soon as possible. Any advices?
    Message was edited by:
    principles

    Blocking queue doesn't work for me as it is too slow.
    I don't need to lock the data because one thread
    adds too the tail, one thread consumes the head, so
    why bother?LinkedBlockingQueue allows concurrent access to the head and tail ie it uses two different locks.
    A Lock is a mechanism for mutual exclusion. It generally has a notion of ownership and so the thread that locks must be the thread that unlocks.
    A Semaphore is a resource counter. There are no constraints on which thread signals() after await(). It is only a protocol that you establish in your code that allows it to be used for exclusion.
    A bounded-buffer generally needs two synchronization aids:
    a) an exclusion control to ensure the data structure is not corrupted by concurrent access
    b) a coordination control to allow consumers to block when the buffer is empty, or producers to block when the buffer is full.
    These two can be combined by using "synchronized" methods and wait/notify. Or by using Lock and associated Condition objects. Or you can use "synchronized" blocks or Lock for exclusion, and handle the coordination using a seperate semaphore (which must use some form of internal synchronization too - but perhaps more efficient.)
    If you have a lock-free data structure, such as ConcurrentLinkedQueue then you don't need anything for (a) and so you only need coordination. So try using a Semaphore: put() increments it and take() decrements it.
    But the Semaphore still becomes a serialization point in your code.

  • What happens to AM instances used by asynchronous threads

    Hi,
    We have a long running process inside an AM, that calls pl/sql packages. Sometimes, this is causing http connection timeout depending on the volume of data it is processing. While we can increase the timeout, we are exploring other options. One that we're planning to spawn a thread in the backing bean, and invoke the AM method binding inside the thread. And have a polling component on the UI that keeps checking the status of the process. If you guys have done it before, please share your experiences and insights. Please help us with the following questions:
    1) When we spawn a thread that uses an AM instance - will it still be managed by ADF or completely moves out of ADF control ?
    2) In our case we spawn a thread and return control back to the UI. So obviously the http connection / HttpServletResponse will be closed immediately. Does this also trigger ADF to forcefully reclaim the AM ? or ADF considers it as busy and not-passivate/release until thread completes ?
    3) Precisely at what point in time, AM gets released? is it upon closure of http/servlet connection ? or upon completion of am method calls ?
    4) Upon thread completion, do we need to take care of releasing it back to pool or ADF will do it automatically ?
    5) Does ADF transaction management work fine with thread scenarios ?
    4) What happens, If another request comes from the same user? does it get a different am instance to cater that request ? Even if it gets a different am instance, it might represent an invalid state, because the previously initiated process could still be running.
    Your help is greatly appreciated.
    Have a good weekend.
    Thank you
    Srini

    If you spawn a new thread to do work, you should use another application module than the one you use for the UI. The way I do this is to call a method in the am which then spanws the new thread which then gets a new root application module and the thread uses this ma exclusivly. All changes done in the thread are part of the transaction of the am used in the thread. There can't be any communication with the transaction from the am used in the ui. The ui only can react on changes done in tables in the db. The spawned thread uses a semaphore which only allows one execution of the thread until the thread has done its work. Here you have to be careful if your app runs in a cluster environment. In this case the semaphore has to be stored in the db or an a shared file system.
    One other way to do this (more easily) is to implement hte log running process as an asynchronous web service. then you call this service from your UI and let the service do the work. Via the callback you get notified once the service has finished.
    Timo

  • Configuring Semaphores for OEL 5.2 and ASM Instance

    Under Document ID 15654.1, the 'processes' parameters of database instances are used to determine the proper sizing of Kernel parameters specific to semaphores. Do we include ASM instance 'processes' in the sizing?

    Individual System V semaphores are small, so one can be generous when
    determining their limits. Having too few is bad, but having lots of unused
    semaphore capability is practically inconsequential.
    To better size your actual usage, use the command:
    # /usr/bin/ipcs -s
    ------ Semaphore Arrays --------
    key        semid      owner      perms      nsems
    0x000000a7 0          root      600        1
    # /usr/bin/ipcs -s -i 0
    Semaphore Array semid=0
    uid=0    gid=0   cuid=0  cgid=0
    mode=0600, access_perms=0600
    nsems = 1
    otime = Wed Apr 28 08:03:25 2010
    ctime = Wed Apr 28 08:03:24 2010
    semnum     value      ncount     zcount     pid
    0          1          0          0          2010will let you determine which processs are using which semaphores.

  • Acquire semaphore gives error Code = 0

    I have incorporated a semaphore into our software.  The semaphore protection works fine for a little while then gives a mysterious error code = 0.  From that point on, no semaphore operations will work correctly.  Has anyone ever seen this error?  Any clue what it might mean.  There is no message included with the error.
    During Debugging I incorporated a mechanism to destroy that semaphore and get a new one.  I found that I can create a new semaphore but when the new semaphore is used, the reference is invalid.  I am using unique named semaphores.  I'm thinking that once the error occurs, the semaphore core is somehow corrupted.
    This is occurring in built code running on windows server 2003.
    Jim West
    Summitek Instruments

    I'm not following the reasoning that there is a race condition. It seems to me that if the semaphore works as advertised... meaning that the "Test and Set" action is correct.  There can never be a race condition of the kind that multiple processes lock the same semaphore.
    In my webserver, there are two pieces to the puzzle.  The server that creates/destroys the semaphore and the processes that acquire/release the semaphore.
    The main server, which is always running, creates a named semaphore.  Places that reference in a global.  Let's called it REF.  On shutdown, the semaphore is destroyed.
    When running, a user requests a web page which then spawns off another handler process using a VI template (VIT).  Within this vi instance, the REF is used to Acquire the semaphore, do the protected action, and release the semaphore.  This will happen hundreds of times with no error.  Then, at some point an acquire is called and the acquire semaphore times out.  You can be sure that I have instrumented the code in such a way to tell me if a release failed or some other error.
    One possibility is that the VIT instance spontaneously exits after the semaphore is acquired.  I don't believe that this is happening but there would not be any way to 'log' that behaviour.
    It is possible that multiple VITs will attempt to acquire the semaphore using REF.  But that is why I am using the semaphore, to stack up the acquires in a methodology similar to stacking on non-reentrant vis.
    Jim West

  • Semaphores

    Have a problem doing assignment in college on threads,
    Have to create Producer(extends thread) and Consumer(extends thread) classes simular to those on the java tutorial on threads , also created another class that contains two variables. The producer updates one variable and the consumer updates the other variable the update methods are in the third class and are synchronized the object of the assignment is to print the difference of these two varibles every two seconds and the result should always be zero. We were told about semaphores and reading through tutorials and websites i got a simple semaphore class.
    class Semaphore
         private int value;
         public Semaphore()
              value = 0;
         public Semaphore(int value)
              this.value = value;
         public synchronized void acquire()
              while (value == 0)
              try {
                   wait();
              catch (InterruptedException ie) { }
              value--;
         public synchronized void release()
              ++value;
              notify();
    The problem is i have no idea how to make sure that the difference is always zero using this class. Any help appreciated, prefer suggestions rather than code. Thanks.

    i changed the semaphore methods to wait() and signal()
    and i tried it a new way, probably well wrong!!I created three semaphore objects and passed them to the producer and Consumer classes.
    i thought i'd use the semaphores within the producer and consumer classes
    * The Counters class, shared by Producer and Consumer
    class Counters {
    /* Variables */
    public int count, outs, ins;
         boolean available;
    /* Constructor */
    public Counters() {
         count = ins = outs = 0;
         public synchronized void OutUp()
    while (available == true)
    try
                        wait();
                   catch (InterruptedException e) { }
                   outs++;
                   available=true;//incrementing one variable
                   notifyAll();
    public synchronized void InsUp()
              while (available == false)
    try
                             wait();
                   catch (InterruptedException e) { }
              ins++;//incrementing one variable
              available=false;
              notifyAll();
    * The Producer class extends Thread. Its run method does nothing
    * more than continuously increment the shared count variable and
    * the shared ins variable
    class Producer extends Thread {
    /* Our thread works on this */
    private Counters ctr;
         public Semaphore mut;
         public Semaphore emp;
         public Semaphore ful;
    /* Constructor */
    public Producer(Counters ctr,Semaphore full,Semaphore mutex,Semaphore empty) {
         this.ctr = ctr;
         this.ful=full;
         this.emp=empty;
         this.mut=mutex;
    /* What our thread does */
    public void run() {
         while (true) {
              mut.WAIT();
              ctr.ins++;
         ctr.InsUp();
              ctr.count++;
              mut.signal();
    * The Consumer class extends Thread. Its run method does nothing
    * more than continuously decrement the shared count variable and
    * increment the shared outs variable
    class Consumer extends Thread {
    /* Our thread works on this */
    public Counters ctr;
         public Semaphore mut;
         public Semaphore emp;
         public Semaphore ful;
    /* Constructor */
    public Consumer(Counters ctr,Semaphore ful,Semaphore mute,Semaphore empt) {
         this.ful=ful;
         this.emp=empt;
         this.mut=mute;
         this.ctr = ctr;
    /* What our thread does */
    public void run() {
         while (true) {
              mut.WAIT();
              ctr.outs++;
              ctr.count--;
         ctr.OutUp();
              mut.signal();
    class RunMe {
    public static void main(String[] args) {
         /* Create our counters */
         Counters ctr = new Counters();
         Semaphore mutex = new Semaphore(1);//created three semaphore objects here
         Semaphore full = new Semaphore(0);
         Semaphore empty = new Semaphore(2);
         /* Create our Consumer thread */
         Consumer outthread = new Consumer(ctr,full,mutex,empty);//passed them to constructers
         /* Create our Producer thread */
         Producer inthread = new Producer(ctr,full,mutex,empty);
         /* Fire up the threads */
         inthread.start();
         outthread.start();
         /* Keep track */
         while (true) {
         /* Sleep for two seconds */
         try {
              Thread.sleep(2000);
         } catch (InterruptedException e) { }
         /* Display the current state of affairs */
         System.out.println("Diff = " + (ctr.ins - ctr.outs) +
                   " Count = " + ctr.count);
    class Semaphore {
              private int count;
              public Semaphore(int n) {
                   this.count = n;
              public synchronized void WAIT() {
                   while(count == 0) {
                        try {
                        wait();
                        } catch (InterruptedException e) {
                        //keep trying
                   count--;
              public synchronized void signal() {
                   count++;
                   notify(); //alert a thread that's blocking on this semaphore
         }

  • Locking Periods using the Second Hierarchy in the Time Dimension

    Hi,
    They are not usiing work status for this...
    I am at a client, and they used the second hierarchy in the time dimension to lock periods.  If they left the second hierarchy blank, no data can be written to it.  To open the open the period, they code the Second Heirarchy with CONS_OPEN.  Just wondering if anyone has information on this? 
    It was working fine, then we needed to run a transport to move BPC to a new machine, and it stopped working.  Not sure if anyone has done it this way before?
    Thanks,
    Chandra

    Dear Kacper,
    the way I understood, there are two separate questions in this post, namely:
    I'm using a shared resource in multiple actors, and the code accessing the resource can take quite some time to execute. How can can I ensure this does not mess up the timing of one or all accessors?
    I'm using a shared resource in multiple actors, and the code accessing the resource can generate errors if I try to access it from multiple places. How can I ensure synchronization between all accessors?
    As for the first question, the optimal solution to separate the resource prone to timing issues in a different thread/actor. Put anything that is not strictly timed (file access, network comunication) in a separate loop. For each actor, you will get queue references to communicate with, so instead of logging the measurements in the actors where they are made, just queue them up for a different loop to process.
    For the second part, the same logic still applies. Ideally, every singular resource should be handled by its own thread and nowhere else, so if anything else needs data from/to said resource, it can send a request to the dedicated thread or actor.
    If, for some reason, this is not sufficient, you have to handle synchronization in some other way. There are a lot of techniques here, for example:
    Create a named semaphore or lock as a part of the class. Have class functions use the semaphore before accessing the resource.
    Use actor messages. Have a "resource in use" or "resource released" messages sent to all users whenever obtaining or releasing said resource.
    Have a separate actor handle all resources, awarding them to threads needing them. This method also allows setting priorities between requests.
    These are just a few examples, there are many other options.
    Please let me know if this was helpful. 
    Kind regards:
    Andrew Valko
    National Instruments Hungary

  • Ipcs for semaphores empty?!?!

    Hi all,
    I'm trying to do up an Operating Systems class assignment which involves the use of semaphores (the Sys V type) using semget(). My code is definitely correct for the creation of a semaphore (in fact I get the -1 error if I try to create the same semaphore number twice).
    issuing ipcs in Terminal gives me no info about the SEMAPHORES.
    Yes, I read the thread about using sudo ipcs -a. It still doesn't show me the semaphores in existence. Shared memory stuff, yes, semaphores, no.
    I'm really hoping to use Xcode/OS X for this... I don't want to have to do this project in Linux.

    FWIW, I run ipcs -ma and ipcs -sa regularly as part of a little check script that I wrote, and I've never seen any semaphores listed.
    Roger

  • Help needed on Logging Servlet Implementation using high traffic HTTP Post

    I have a servlet called LogServlet, which accepts the post HTTP request Data from another application and logs it into a file. The key thing here is the logs should get logged in a squence it is received.
    Since the frequency of the data being sent is very high the LogServlet is not able to keep it up with the sequence and messing up the sequence.
    Are there any suggestions on how to improve this and make sure the servlet logs it in the way it receives it?
    Can I replace the Servlet with JSP to do the logging and will that solve my sequence issue?
    Or there are any other techniques.
    Thanking you in advance.

    You could use the log4j logging tool, it uses a static Logger for the class and you can run it in synchronized mode to ensure that your log messages don't clobber each other. Hmm, you're testing my knowledge of how synchronization is implemented in Java. It uses a semaphore-style approach under the covers, but is it a fair-style (first-come, first-served) implementation?
    After a quick Google search I just learned that in the normal VM fairness is not guaranteed, but if you use the Sun Real-Time VM synchronized will guarantee FIFO style access to synchronized resources. The Real-Time VM is not free, so if cost is an issue that might not be an option for you. That's going to make things more complicated for you since ordering sounds important.
    You can probably write something fairly easily to do what you want. Basically write a queue processing class that uses either static methods/variables or a Singleton approach to ensure that your messages get processed in a FIFO order. Then just use your existing logging logic or use log4j to write your messages to a log file. There are several pitfalls to writing multi-threaded components like this, so be careful that you've synchronized the right methods and, depending on how you decide to implement it, use the right Queue implementation (hint: you may need to look in the java.util.concurrent package for your queue implementation, depending on the approach you take.)
    One thing to note, this last approach may be overkill depending on what you mean by "very high frequency." If you're getting 10-20 requests a second, I think you still might be able to get away with log4j in synchronized mode. It also depends on how important the ordering really is. If one request arrives 500 microseconds after the other, is it okay to reverse the ordering? If so then syncrhonized log4j may still be okay. If not, then you're going to have to write something a little more sophisticated or take a closer look at the capabilities of the real-time VM.
    Alternatively, someone who's more knowledgeable than me on the subject might be able to suggest a better approach using JMS.

  • Don't understand Semaphores

    I have been given this code:
    class Semaphore {
       private int count;
       public Semaphore(int n) {
          this.count = n;
       public synchronized void acquire() {
          while(count == 0) {
             try {
                wait();
             } catch (InterruptedException e) {
                //keep trying
          count--;
       public synchronized void release() {
          count++;
          notify(); //alert a thread that's blocking on this semaphore
       }Which is an implementation of a semaphore.
    I don't understand how it would be used - could somebody give me an example please?
    Also, I understand that a binary semaphore can only have a value of 0 or 1. Could I create a binary semaphore class by extending this one?

    As i understand semaphores, they are used to limit control over a shared resource (like a shared buffer). Sometimes you may have a shared resource that you dont want to be altered during a critical operation. Using a semaphore around this critical code keeps the resource from being altered until the semaphore is released. While the value of the semaphore is 0, the shared resource is blocked from being accessed by other functions, after it becomes non-zero other threads are notified that the resource is available again. I recently learned about semaphores in one of my computer architecture classes, so this is how i understand them....but there is plenty of better documentation online :). Also, this class looks easily extendable into a binary semaphore subclass, just limit the value that the semaphore can accept. Hope this helps.
    ~Nick

  • Semaphores: sem_getvalue not implemented: why? when? workaround?

    It seems sem_getvalue is not implemented on Darwin.
    Does anyone know why?
    Does anyone know if/when it will be implemented?
    Does anyone a workaround? (i.e. any way to get the current value of a named semaphore?)
    I need to share a value between two or more processes and I need to be able to atomically increase it. Using MACH semaphores does not help (no getvalue there either). One option I could see would be to use shared memory, but then I'd also have to use a named semaphore to atomically increase the value in there, but this all is starting to get quite convoluted for what is a basic functionality of named semaphores. So I'm open to any suggestion, surely someone else must have run into this problem before (like in porting Unix code to Darwin for example).

    semctl is for System V semaphores, you can't use it with a pthread semaphore. I would have to re-write everything using System V semaphores rather than named pthread semaphores, which is what I am trying to avoid in the first place.
    I actually find it a bit strange that System V and pthread semaphore calls are making "see also" reference to each other in the man pages while they are obviously completely different implementations (of the same semaphore concept)...

  • Semaphore GOOP Help

    My Quandry:
    I am dynamically launching multiple VIs, but I am interested in them sharing a single resource (say, data stored in a GOOP-style/Old-style global.). So I want to use a semaphore to coordinate which VI has access to the resource.
    I have a VI that calls the object VI, which creates the semaphore and stores it in its shift register. Then I dynamically launch the resource(s) that use the object VI.
    What I find is that the semaphore becomes invalid the minute that the top-level VI (which set everything up) is done.
    This makes no sense to me, as the refnum is stored in a shift register in a subVI that is contained by an executing VI.
    It is also causing me a big problem in my application.
    Who can explain this?
    SEE EXAMPLE.
    Note: the example was written in 7.1, but also evidences the behavior in 8.0
    Attachments:
    DEBUGSemaphore.llb ‏85 KB

    Lets see if I can add some value without getting stuck!
    There is a size input fo rthe create semaphore.
    That determines how many enities can get the semaphore.
    When a semaphore is requested and obtained, that value gets decremented. When the value is zero, nobody else get the semaphore. When the semaphore is released tha value is incremented.
    This how LV tracks if the semhpre is in use.
    When a VI evaluated to determine if it can be unloaded the resource taht were allocated by that VI are checked to see if they are in use. That is done by looking a the semaphore count and the max size.
    So...
    1) When the semaphore is created set the size = 2
    2) Imediately after creating it, acquire the semaphore. (that should leave one for everyone else).
    3) Everything else codes the same until the end.
    4) At shutdown time, the entity that will destroy the semaphore should do a "release" prior to the destruction.
    I have not tested this but this is how I understand it and what I would try if I was faced with this challenge.
    Ben
    Ben Rayner
    I am currently active on.. MainStream Preppers
    Rayner's Ridge is under construction

Maybe you are looking for

  • Email issue in ECC 6.0

    Hi, I have written a program to send emails as shown in SAP example programs, I am not able to send the emails. I have ran some of the SAP example programs BCS_EXAMPLE_1 BCS_EXAMPLE_2 BCS_EXAMPLE_3 BCS_EXAMPLE_4 BCS_EXAMPLE_5 BCS_EXAMPLE_6 I still do

  • Configure InstanceName

    hi Recently Steve Muench posted this discussion message at http://groups.google.com/group/adf-methodology/msg/5b8bd1437d87b4e5 An ActionBinding can call backing bean method by using an EL expression in the action binding's "InstanceName" attribute in

  • Trouble deleting text messages from Extravert 2

    My daughter and I both recently got the LG Extravert 2 and have both had the same problem. I was the first to have the problem with not being able to delete messages, but I was able to solve it after turning my phone off and removing the battery for

  • Slow going to sleep

    Does anyone have any ideas how to speed my imac up when going to sleep , for the past few days it seems to take ages shutting down where as before it was quite quick i haven't downloaded anything or altered any settings . Im running Snow Leopard on a

  • How to retrieve image in BLOB in oracle database using JSP?

    do any one the method to view image in homepage using jsp from BLOB field in oracle database ? thx