• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Interrupting sleep call
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Interrupting sleep call


  • Subject: Re: Interrupting sleep call
  • From: Raphael Sebbe <email@hidden>
  • Date: Thu, 6 Dec 2001 10:14:53 +0100

I would not use sleep + signal for thread synchronization purposes. That clearly relies on sleep implementation details.

There are some primitives for doing exactly that, those are mutex and semaphore.

If you are using pthreads directly, have a look at:

man pthread
man pthread_mutex_init
man pthread_cond_init

If using Cocoa:

NSThread, NSLock, NSConditionLock


In your case, I would use a condition lock (or mutex)

thread 1:
init condition to 0
launch thread 2
do some stuff...
wait for condition to be 1 (no loop, just a "passive, to be awaken" wait)
do some other stuff...

thread 2:
compute...
set condition to 1

Raphael

On Thursday, December 6, 2001, at 09:46 AM, Tim McLaren wrote:

I wonder if anyone can help me with a problem I am having. I am writing some code which is multi-threaded which one of the threads performs a sleep within a loop while waiting for some data from the other thread to process. The idea of using the sleep is to stop it from free wheeling and using unnecessary cpu cycles.

What I am trying to do is to be able to wake up the sleeping thread before the sleep has completed by sending a signal to the program from another thread.

According to the man page for sleep this should work which it did when I wrote a simple c program to test it. Unfortunately, when called from a cocoa app the sleep does not exit prematurely, even though the signal is caught by the signal handler.

Does anybody know of a way to make this work within a cocoa app.

Thanks
Tim.

-- _______________________________________________
cocoa-dev mailing list
email@hidden
http://www.lists.apple.com/mailman/listinfo/cocoa-dev


References: 
 >Interrupting sleep call (From: Tim McLaren <email@hidden>)

  • Prev by Date: The sick power!
  • Next by Date: NSNotificationCoalescingOnName | NSNotificationCoalescingOnSender
  • Previous by thread: Interrupting sleep call
  • Next by thread: NSNotificationCoalescingOnName | NSNotificationCoalescingOnSender
  • Index(es):
    • Date
    • Thread