Critical section coding with OSX 10.6

Discussion in 'Mac Programming' started by multinode, Aug 29, 2013.

  1. multinode, Aug 29, 2013
    Last edited: Aug 29, 2013

    macrumors regular

    Joined:
    Feb 4, 2011
    #1
    I have N SDCards connected to my MacBook via USB. This is an OSX 10.6 MacBook business app with as many as N customers randomly inserting SDCards. My app recognizes each insertion with a Disk Arbitration DiskAppearedCallback and that callback processes the information in a file on the card and determines the USB locationID to identify which customer did the mount.

    My question is how to have the multiple callbacks be processed SEQUENTIALLY, i.e. I don't want any callback to be interrupted by another mount callback. I think that this is a critical section problem ... locks, semaphores, etc.

    Does anybody here have any suggestions?
     
  2. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #2
    Enter the debugger. Set a breakpoint on the callback. Is it called on the main thread or on a background thread? Better yet, check the documentation. What does it say? Main thread is single threaded.
     
  3. thread starter macrumors regular

    Joined:
    Feb 4, 2011
    #3
    Thanks GNASHER. I'm not certain that this is a multithreading issue??? I usually think of multithreading as the processor giving each thread a quantum of time one after the other. In my case, I'm thinking that ONE thread is doing all the mount processing, BUT while one mount callback is executing the callback code block, another mount callback cannot interrupt to enter that same code block. I think this is a matter of locking the code block to prevent non lock holders from entering.

    I don't understand your statement "main thread is single threaded" ... please elaborate. Your statement seems to imply that a DA notification spawns a new thread and that execution in the main thread doesn't allow that. If that is what you mean, then that begs the question of how any DA notification can execute. Again, I'm not sure that my issue is about multi threading.
     
  4. thread starter macrumors regular

    Joined:
    Feb 4, 2011
    #4
    Hello GNASHER ...

    I have an idea ... maybe I do want multiple threads. Maybe each time a DA Disk Appears notification happens the callback should explicitly spawn a new thread and then call the actual mount processing code to execute in the new (child) thread (HOW?). The beginning of the actual mount processing code could spin on a lock which would get released whenever a player completes/exits the execution of the mount processing code.

    Does this make sense? Did you understand me?
     
  5. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #5
    Sounds like a job for Grand Central Dispatch. If you want to ensure that the dispatched events (mounts) are handled one at a time in the order they happened dispatch against your own queue that you create with
    Code:
    DISPATCH_QUEUE_SERIAL
     
  6. thread starter macrumors regular

    Joined:
    Feb 4, 2011
    #6
    Yeah ... I was thinking about queues and GCD too. In other words, the DA callback should go to some intermediate code ... queues, locks, etc. ... which would then go to the actual mount processing code.

    Thanx.
     

Share This Page