Re: NSCondition


Sandor Szatmari
 

Regarding the call to -isCancelled in the consumer thread... presumably you will have a handle to this thread somewhere and want to -cancel it gracefully prior to termination...  Or cancel it and restart it...  

For whatever your reason may be... checking the -isCanceled state gives you the change to break out of the threads loop...

So instead of -detachThread.... you could use -initWithSelector.... and hold onto this handle to your thread...  then you can either make it directly available as a property or define an interface on you class to allow outside interaction...

Sandor

On Mon, Mar 12, 2018 at 7:06 AM, Dave <dave@...> wrote:
Hi,

Thanks for the sample code Sandor, I’ve now got it working correctly now and I’m really pleased with it. I realised what was wrong just before I went to sleep last night and just added the fix and it works! The major difference in the way your project works is that you have a separate Producer Thread, in my case, a delegate method of the producer task is called with the Data on the Main Thread and you must completely deal with the data it passes before the method returns...

The problem was that it was processing the whole queue with the Condition Locked, so nothing else could get it, this is the revised Consumer Task and it works really well:

-(void) consumerTask
{
while (YES)
        {
        [self.pQueueTaskConsumerTaskWakeUpCondition lock];
        [self.pQueueTaskConsumerTaskWakeUpCondition wait];

//**
//**    Get Head Object - Locked
//**
        self.pQueueTaskLastObjectRemoved = [self.pQueueTaskProcessingQueue queueGetHead];
        [self.pQueueTaskConsumerTaskWakeUpCondition unlock];

//**
//**    Process the Entire Queue
//**
        while (self.pQueueTaskLastObjectRemoved != nil)
                {
//**
//**    Process Object - Unlocked
//**
                [self processObject:self.pQueueTaskLastObjectRemoved];

//**
//**    Get Head Object - Locked
//**
                [self.pQueueTaskConsumerTaskWakeUpCondition lock];
                self.pQueueTaskLastObjectRemoved = [self.pQueueTaskProcessingQueue queueGetHead];
                [self.pQueueTaskConsumerTaskWakeUpCondition unlock];
                }
        }
}

I noticed that your Consumer thread checks “isCancelled”, I’ve added this to my version, although I’m not sure it is needed.

I’ve written this as two generalised classes, a Task Class and a Queue Class, to use it all you need to do is created the Task Object with a Delegate that contains two methods one that is called just before an Object is added to the Queue and one that is called to process the Object. Once I’ve had a chance to tidy it up I’ll send it to anyone that is interested.

Thanks again,
All the Best
Dave





Join cocoa@apple-dev.groups.io to automatically receive all group messages.