Bri Bri
Bri Bri

Reputation: 2270

Objective-C: use NSThread with a function or block, without need for an additional object

I have an Objective-C app where I need to run a function in a separate thread. This function will run continuously with a run loop and will need to be canceled at a later date, so I think the right thing to use is NSThread. I don't think this is an appropriate time to use Grand Central Dispatch. (Do correct me if I'm wrong.)

NSThread requires me to allocate and maintain a separate object with a method that will be executed in the new thread, e.g. [[NSThread alloc] initWithTarget:someObject selector:@selector(runThread) object:nil]

Granted this may seem like a trifling complaint, but is there any way for me to start a thread like this without needing this separate object? I'd like to just use a C-style function or a block.

Upvotes: 0

Views: 1016

Answers (1)

Ken Thomases
Ken Thomases

Reputation: 90531

The documentation is organized a bit oddly, but NSThread has -initWithBlock: and +detachNewThreadWithBlock:.

Upvotes: 1

Related Questions