discuss-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSThread


From: Patrick Stein
Subject: Re: NSThread
Date: Wed, 22 Nov 2000 20:32:41 +0100

"Philippe C.D. Robert" <address@hidden> wrote:
>I am now playing around with multi-threaded scenegraph rendering/updating 
in
>the 3DKit. I'd like to use GNUstep's thread support to handle this, but I 
am
>not sure if this is possible. I need a way to 'wait' for all threads to 
finish
>before going on with handling new rendering tasks. Is this possible? I did 
not
>see anything which could handle this in the documentation...
>
>any hints?

You normally create a set of worker threads at the beginning, cause thread 
creation and destruction takes time ( and there was a bug in the former 
openstep which crashed the system that way ). And then create a working 
Queue.
Every worker than takes it's stuff from the queue 'till it's done.

In case you need a such a queue , I appended one which works fine for me.


                                                  stay cool - jolly
===================================================================
Patrick Stein or Jolly                               at jinx dot de
===================================================================
  " Hickory, dickory, dock,
    The mouse ran up the clock.
    The clock struck one,
    The mouse ran down,
    Hickory, dickory, dock. "                       - nursery rhyme
===================================================================


MTQueue.m ( MultiThreaded Queue )
#import "MTQueue.h"

#define QUEUE_EMPTY     0
#define QUEUE_NOT_EMPTY 1


@implementation MTQueue
{
    NSConditionLock     *queueLock;
    NSMutableArray      *queueArray;
}


- init
{
    [super init];
    
    queueLock = [[NSConditionLock alloc] initWithCondition:QUEUE_EMPTY];
    queueArray = [[NSMutableArray alloc] init];
    return self;
}

- (void) dealloc
{
    [queueLock release];
    [queueArray release];
    [super dealloc];
}

- pop;
{
    id  anObject;

    [queueLock lockWhenCondition:QUEUE_NOT_EMPTY];
    anObject = [[[queueArray objectAtIndex:0] retain] autorelease];
    [queueArray removeObjectAtIndex:0];
    [queueLock unlockWithCondition:([queueArray 
count]?QUEUE_NOT_EMPTY:QUEUE_EMPTY)];
    
    return anObject;
}

- popDoNotBlock;
{
    id  anObject=nil;

    [queueLock lock];
    if([queueArray count])
    {
        anObject = [[[queueArray objectAtIndex:0] retain] autorelease];
        [queueArray removeObjectAtIndex:0];
    }
    [queueLock unlockWithCondition:([queueArray 
count]?QUEUE_NOT_EMPTY:QUEUE_EMPTY)];
    
    return anObject;
}


- (void) push:(id)anObject;
{
    [queueLock lock];
    [queueArray addObject:anObject];
    [queueLock unlockWithCondition:([queueArray 
count]?QUEUE_NOT_EMPTY:QUEUE_EMPTY)];
}

- (unsigned int) count;
{
    return [queueArray count];
}


@end



reply via email to

[Prev in Thread] Current Thread [Next in Thread]