• 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: Simple message passing between threads
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Simple message passing between threads


  • Subject: Re: Simple message passing between threads
  • From: Bill Bumgarner <email@hidden>
  • Date: Wed, 13 Jun 2001 09:14:13 -0400

Another possibility is to create an Application Defined event via NSEvent:

+ (NSEvent *)otherEventWithType:(NSEventType)type location:(NSPoint)location modifierFlags:(unsigned int)flags timestamp:(NSTimeInterval)time windowNumber:(int)wNum context:(NSGraphicsContext*)context subtype:(short)subtype data1:(int)d1 data2:(int)d2;

(Pass NSApplicationDefined as the event type)

Use the subType field to enumerate the different types of events you need to send. I.e. you could define something like:

typedef enum {
CalculationThreadStarted = 0,
CalculationThreadStopped = 1,
CalculationThreadUpdate = 2,
CalculationThreadError = 3
} CalcThreadEventSubTypes;

And you can use d1 and d2 to pass along additional information. A very annoying anti-feature of the NSEvent is that it doesn't have the equivalent of the userInfo field on, say, NSNotification. As such, you can't pass objects along with the event; you'll have to store 'em someplace thread safe and retrieve them from the main event loop manually.

You can use NSApp's -postEvent:atStart: method to cause the event to be posted into the main event loop thread. -postEvent:atStart: takes care of passing the event from the thread into the MEL, hence it is useful for slave->master thread communications.

To handle the event, override -sendEvent: on a NSWindow or NSApplication subclass. If you are subclassing NSWindow, make sure that the NSEvent contains the target window's windowNumber -- it is a parameter to the event factory method shown above. This will ensure that the event is actually dispatched to the appropriate window.

This will take care of notification from the thread back to the main event loop without locking up the main event loop. It also works fine if programming Cocoa using Java. Cocoa/Java was actually the context within which I originally figured this out-- I was used to a postEvent from older versions of OpenStep that did not correctly pass the event to the main event loop.

--

Notification from the main event loop back to your thread is actually quite a bit easier in that you are no longer worried about interrupting the main event loop and can use standard intra-thread communication patterns.

Keep in mind that the slave thread will either need to run some kind of an event loop-- an NSRunLoop works well-- or will have to periodically poll a condition to see if the MEL has sent it some kind of an event.

b.bum

On Wednesday, June 13, 2001, at 03:42 AM, cocoa-dev-
email@hidden wrote:

Message: 6
Date: Tue, 12 Jun 2001 18:43:22 -0700
From: Richard Schreyer <email@hidden>
To: email@hidden
Subject: Simple message passing between threads

I have a secondary thread I use to do calculations, and I need to send a
message to the main thread when the second thread completes it's job,
and terminates.

Distributed Objects seems like a little much for this single use. There
have been several times has come up previously on the list, but looking
over those has still left me clueless how to do this.

Thanks again,
Richard Schreyer


  • Prev by Date: Re: Checkboxes and NSBox titles
  • Next by Date: Re: Bug In Project Builder
  • Previous by thread: Re: Simple message passing between threads
  • Next by thread: Displaying HTML
  • Index(es):
    • Date
    • Thread