Thomas Joulin
Thomas Joulin

Reputation: 6650

NSThread running a class

Every the example I find on the internet (including Apple doc) sets the target to self like this :

[NSThread detachNewThreadSelector:@selector(threadedTask) toTarget:self withObject:nil];

Since i would like to do a more complex task in background (involving multiple methods), I thought of creating a class Task called like this :

Task *task = [[Task alloc] init];
[NSThread detachNewThreadSelector:@selector(main) toTarget:task withObject:nil];

but I wonder, how will be handled the instance variables of my class Task ? in my method main can I safely call methods of my class Task using [self myMethod] and modifying members variables assuming I'm the only thread accessing it ?

This way of handling threads seems weird to me, sorry if my question is stupid ^^

Thanks in advance

Upvotes: 0

Views: 238

Answers (1)

T .
T .

Reputation: 4944

If there's only one NSThread (including the main thread) for each Task then yes, you can do whatever you want. As soon as more than one thread can be accessing certain data simultaniously, you'll have to provide synchronization.

Upvotes: 1

Related Questions