I have two set of values in my two threads, for example, in my 1st thread i have odd numbers and in 2nd thread i have even number, but i want to print values like 1,2,3,4…..how to print the thread values alternatively???
NSThread* myThread = [[NSThread alloc] initWithTarget:self selector:@selector(myThreadMainMethod:) object:nil];
[myThread setThreadPriority:1];
NSThread* myThread2 = [[NSThread alloc] initWithTarget:self selector:@selector(myThreadMainMethod2:) object:nil];
[myThread2 setThreadPriority:1];
[myThread start]; // Actually create the thread
[myThread2 start]; // Actually create the thread
Thanks.
If you want your threads to do their output ordered (which in your case seems to be a really bad idea) then you’ll have to synchronize them with an event so they signal each other to perform the next step in the sequence.
This however will loose you all benefit of having threads in the first place. If you want to print an ordered sequence of numbers, just do it on the GUI thread.
For work that makes sense doing in a threaded environment, you’ll be splitting so that as little as possible of it relies on any other work done by any other thread, so as to achieve real parallelism.
Doing stuff by threads which requires too much synchronization so the effect is that they essential run serially, is senseless.