1

I am running an application with multiple threads. My application has set of queue objects(simply array).The input for the thread will be from this queue. Each thread will get a queue object as an input. And I am creating and starting this thread in simple manner as below.

threadOne = [[NSThread alloc] initWithTarget:self
                                                  selector:@selector(initThread:)
                                                    object:nil];
[threadOne main];

Likewise I am starting multiple available threads for every objects.

When I run my application in debug mode during my webservice call, My current running thread doesn't wait for response (approximately my webservice call will take 2-3 seconds to generate response). Here my queue process gets stopped due to web service call. Below is the code for reference.

dispatch_sync(dispatch_get_main_queue(), ^{

        [service genID:self action:@selector(genHandler:) username: username password:password ];

    });

I want to run multiple threads in parallel, in my application. Is there any solution to accomplish the above.

2vision2
  • 4,933
  • 16
  • 83
  • 164
santhosh
  • 1,191
  • 4
  • 14
  • 28
  • Do you understand the basics of multitasking or threading? You are using the mainQueue blocking all other tasks with the example above. – Volker Feb 03 '14 at 14:16

1 Answers1

1

You probably have a deadlock because you are calling

dispatch_sync(dispatch_get_main_queue()

from main thread. You can find an excellent explanation in this answer.

As a side note, I would recommend using NSOperationQueue or Grand Central Dispatch, because Apple does not recommend using NSThread objects directly:

In the past, introducing concurrency to an application required the creation of one or more additional threads. Unfortunately, writing threaded code is challenging. Threads are a low-level tool that must be managed manually. Given that the optimal number of threads for an application can change dynamically based on the current system load and the underlying hardware, implementing a correct threading solution becomes extremely difficult, if not impossible to achieve. In addition, the synchronization mechanisms typically used with threads add complexity and risk to software designs without any guarantees of improved performance.

Both OS X and iOS adopt a more asynchronous approach to the execution of concurrent tasks than is traditionally found in thread-based systems and applications. Rather than creating threads directly, applications need only define specific tasks and then let the system perform them. By letting the system manage the threads, applications gain a level of scalability not possible with raw threads. Application developers also gain a simpler and more efficient programming model.

Community
  • 1
  • 1
Michał Ciuba
  • 7,876
  • 2
  • 33
  • 59