• 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: Re: Thread and sleep
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: Re: Thread and sleep


  • Subject: RE: Re: Thread and sleep
  • From: "Peter Karlsson" <email@hidden>
  • Date: Tue, 18 Jan 2005 19:00:31 +0100

Hi!

I can't get this to work. The thread will not sleep at all. Must I
initialize the lock before every time I sleep?

Peter

Ursprungligt meddelande

> Yes, of course there's a better way.  Polling the state of a button
> (especially from a different thread, remember the AppKit is not
> thread-safe) is ridiculously wasteful.  Set the action of the button to
> be -(void)wakeThread:(id)sender and code it like this:
>
> NSConditionLock*	lock; // lock is an instance variable
>
> - (void)wakeThread:(id)sender {
> 	[lock lock];
> 	[lock unlockWithCondition:[sender state]];
> }
>
> and in your worker thread, do this:
>
> // These two calls replace the while() loop below
> [lock lockWhenCondition:1];
> [lock unlockWithCondition:0];
>
> // Do stuff here
>
> Brendan Younger
>
> On Jan 18, 2005, at 10:39 AM, Peter Karlsson wrote:
>
> > Dear list!
> >
> > I'm running a second thread and I want the thread to sleep until a
> > NSButton
> > is pressed. Right now I'm checking the state of the button and if it's
> > not
> > pressed I sleep for a while and repeat everything until the button is
> > pressed. Then the thread continue. Code fragment:
> >
> > while ([myButtonOutlet state] != 1)
> > {
> > usleep(100000);
> > }
> >
> > But it seems that this will not work if I do it more then one time in a
> > row. I must click the button twice for some reason sometimes to make
> > the
> > thread continue. I don't know why.
> >
> > Is there another and better way to do this?
>
>

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Prev by Date: Re: dismissing one sheet for another problem
  • Next by Date: Re: dismissing one sheet for another problem
  • Previous by thread: Re: Thread and sleep
  • Next by thread: RE: Thread and sleep
  • Index(es):
    • Date
    • Thread