NSThread running a class

Posted by Thomas Joulin on Stack Overflow See other posts from Stack Overflow or by Thomas Joulin
Published on 2010-05-04T09:56:09Z Indexed on 2010/05/04 10:08 UTC
Read the original article Hit count: 299

Filed under:
|

Hi,

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

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about multithreading