★ TouchArcade needs your help. Click here to support us on Patreon.

How do I create a new thread?

08-19-2009, 08:43 PM
#1
Joined: Mar 2009
Location: Singapore
Posts: 1,770
Send a message via AIM to kohjingyu
How do I create a new thread?

Hey guys,
I'm not sure about how to create a new thread in the game. I think it's something like:

Code:
[detach newThread ....]
Can anyone help?

Also can the function called by the thread be also called by a timer?

Thanks.
08-19-2009, 08:59 PM
#2
Joined: May 2009
Location: Palmerston North, New Zealand
Posts: 464
Quote:
Originally Posted by froggy1233 View Post
Hey guys,
I'm not sure about how to create a new thread in the game. I think it's something like:

Code:
[detach newThread ....]
Can anyone help?

Also can the function called by the thread be also called by a timer?

Thanks.
You need
Code:
detachNewThreadSelector:toTarget:withObject:
It is a static method of the NSThread class. It doesn't say there what the parameter to the selector actually is, just that there must be one and only one.

If the method you want to share between the timer and the thread firing doesn't actually use the parameter for anything just cast it as id and it shouldn't matter.

I hope you know what you're getting yourself in for working with threads! Concurrency is a nasty but powerful beast!

Check out Sheepish, 100+ levels of puzzle fun! With sheep!
And more about us: www.pixelthismobile.com
plus+ id: Newtz
08-19-2009, 09:08 PM
#3
Joined: Mar 2009
Location: Singapore
Posts: 1,770
Send a message via AIM to kohjingyu
Quote:
Originally Posted by PixelthisMike View Post
You need
Code:
detachNewThreadSelector:toTarget:withObject:
It is a static method of the NSThread class. It doesn't say there what the parameter to the selector actually is, just that there must be one and only one.

If the method you want to share between the timer and the thread firing doesn't actually use the parameter for anything just cast it as id and it shouldn't matter.

I hope you know what you're getting yourself in for working with threads! Concurrency is a nasty but powerful beast!
Haha, so I can do:

Code:
- (void)viewDidLoad
{
      detachNewThreadSelector:aLoop toTarget:object

      [super viewDidLoad];
}

- (void)aLoop
{
     //Do something
}
Can I specify a certain amount of interval for a thread? And what are the disadvantages to using a thread instead of a timer? I know that it would run faster, about 10 fps faster...
08-19-2009, 09:37 PM
#4
Joined: May 2009
Location: Palmerston North, New Zealand
Posts: 464
Quote:
Originally Posted by froggy1233 View Post
Haha, so I can do:

Code:
- (void)viewDidLoad
{
      detachNewThreadSelector:aLoop toTarget:object

      [super viewDidLoad];
}

- (void)aLoop
{
     //Do something
}
Can I specify a certain amount of interval for a thread? And what are the disadvantages to using a thread instead of a timer? I know that it would run faster, about 10 fps faster...
Almost, you do:

Code:
[NSThread detachNewThreadSelector:@selector(aLoop:) toTarget:self withObject:nil];
And your aLoop method must take a parameter, it looks like it is the object specified at by the withObject parameter. You really should check the NSThread reference as there is info there relating to autorelease pools and suchforth.

Having told you all that it seems to me that a thread is not what you are looking for if you are comparing a thread and a timer as they are very different beasts! Your program switches execution between all the threads it has running as it pleases. It could be in the middle of doing something in one function and then jump off to another thread. This is the essence of concurrency. There is no way to control when your program jumps between threads so no you can't set a time interval on it.

What are you trying to achieve exactly?

Check out Sheepish, 100+ levels of puzzle fun! With sheep!
And more about us: www.pixelthismobile.com
plus+ id: Newtz
08-19-2009, 10:03 PM
#5
Joined: Mar 2009
Location: Singapore
Posts: 1,770
Send a message via AIM to kohjingyu
I'm trying to make my game loop threaded instead of using a timer, as it would mean it would run faster. Should I use a thread? Or a timer?

Thanks.