0

I implemented Thread pooling using NSOperationQueue. In which i set maxConcurrentOperationCount to 25. i.e. concurrently 25 threads are running at a time.

I am uploading chunks to a server by using this NSOperationQueue. So chunks are allocated to the first 25 threads. After the NSOperationQueue is full, I want to pause the chunking reading part, then whenever threads from the queue complete, resume the chunking part to allocate new threads to NSOperationQueue to replace the thread which complete.

My Code:

NSOperationQueue *operationQueue = [NSOperationQueue new];
operationQueue.maxConcurrentOperationCount=5;

NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self                                                                         selector:@selector(upload:)                                                                            object:f_objChunkDetails->FileStream];

NSUInteger oprCnt=operationQueue.operationCount;

if(oprCnt >= 5) {
    // wait till queue has a free slot
} else {
    [operationQueue addOperation:operation];
}

So how to pause and resume is used in NSOperationQueue? How to implement ManualResetEvent in Objective-C?

Wain
  • 118,658
  • 15
  • 128
  • 151
Snehal
  • 11
  • 1
  • 6

1 Answers1

0

Don't wait or pause. Instead, move your job creation (and check) into a new method. That method should loop to create jobs up to the available limit and then return. Each job that is created should have a completionBlock added which calls the job creation method.

In this way you are event triggered instead of blocking.

Generally, the completionBlock should change to the main thread before calling the job creation method.

Wain
  • 118,658
  • 15
  • 128
  • 151
  • Currently i am achieving uploading part using NSOperationQueue so uploading data to server requires time.. And data which are going to upload is coming from ReadData class which is execute fastly.. So i want to pause the ReadData till NSOperationQueue has space if queue contains space ReadData start working and next data is assign to NSOperationQueue.. In C# it achieve using ManualResetEvent class. What is feasible solution for objective-c? How we can achieve ManualResetEvent for multiple Thread? – Snehal Mar 20 '14 at 03:49
  • If you dislike my solution above you could use `NSConditionLock`. – Wain Mar 20 '14 at 08:25
  • I like your solution but is thr any other way to do this.. I want feasible solution. NSConditionLock or NSLock shared the resources... I want to pause and resume my each thread separately.. As per my knowledge NSConditionLock will not achieve this.. If you think so that NSConditionLock is better option then can u please provide me sample code... Like in c# we use ManualResetEvent Class which uses set,Reset, waitOne method for pause and resume... What is suitable option for objective-c(MAC). Please give your valuable suggestion... – Snehal Mar 24 '14 at 03:45
  • My answer is a feasible non-blocking solution. If you want a blocking solution then `NSConditionLock` can be used as a counting semaphore. As can `dispatch_semaphore_t`. – Wain Mar 24 '14 at 07:39