# GSThreadPool class documentation

### Authors

Richard Frith-Macdonald (`rfm@gnu.org`)

Copyright: (C) 2010 Free Software Foundation, Inc.

# Software documentation for the GSThreadPool class

## GSThreadPool : NSObject

> Declared in:
>   GSThreadPool.h

This class provides a thread pool for performing methods of objects in parallel in other threads.
This is similar to the NSOperationQueue class but is a lightweight alternative designed to operate rather faster though with slightly decreased functionality... for instance there is no dependency checking supported.

Instance Variables
Method summary

- +sharedPool
- -drain:
- -flush
- -isEmpty
- -isIdle
- -isSuspended
- -maxOperations
- -maxThreads
- -resume
- -scheduleSelector:onReceiver:withObject:
- -setOperations:
- -setThreads:
- -suspend

### sharedPool

+ (GSThreadPool*) sharedPool;
Returns an instance intended for sharing between sections of code which wish to make use of threading by performing operations in other threads, but which don't mind operations being interleaved with those belonging to oither sections of code.
Always returns the same instance whenever the method is called.

### drain:

- (BOOL) drain: (NSDate*)before;
Waits until the pool of operations is empty or until the specified timestamp. Returns `YES` if the pool was emptied, `NO` otherwise.

### flush

- (NSUInteger) flush;
Removes all operations which have not yet started, returning a count of the abandoned operations.

### isEmpty

- (BOOL) isEmpty;
Returns `YES` if no operations are waiting to be performed, `NO` otherwise.

### isIdle

- (BOOL) isIdle;
Returns `YES` if `NO` operations are in progress, `NO` otherwise.

### isSuspended

- (BOOL) isSuspended;
Returns `YES` if startup of new operations is suspended, `NO` otherwise.

### maxOperations

- (NSUInteger) maxOperations;
Returns the currently configured maximum number of operations which may be scheduled at any one time.

### maxThreads

- (NSUInteger) maxThreads;
Returns the currently configured maximum number of threads in the pool.

### resume

- (void) resume;
Reverses the effect of -suspend .

### scheduleSelector: onReceiver: withObject:

- (void) scheduleSelector: (SEL)aSelector onReceiver: (NSObject*)aReceiver withObject: (NSObject*)anArgument;
Adds the object to the queue for which operations should be performed.
You may add an object more than once, but that may result in the operation being performed simultaneously in more than one thread.
If the pool is configured with zero threads or zero operations, this method will simply perform the operation immediately.
The operation will be performed in a context where there is an exception handler set to trap exceptions, and an autorelease pool to deal with autoreleased objects.

### setOperations:

- (void) setOperations: (NSUInteger)max;
Specify the number of operations which may be waiting.
Default is 100.
Setting a value of zero ensures that operations are performed immediately rather than being queued.

### setThreads:

- (void) setThreads: (NSUInteger)max;
Specify the maximum number of threads in the pool (the actual number used may be lower than this value).
Default is 2.
The pool creates threads on demand up to the specified limit (or a lower limit if dictated by system resources) but will not destroy idle threads unless the limit is subsequently released.
Setting a value of zero means that operations are performed in the main thread. In this case -drain: will be used (with a 30 second limit) followed by -flush to ensure that the queue is emptied before the threads are shut down.

### suspend

- (void) suspend;
Turns off startup of new operations.

## Instance Variables for GSThreadPool Class

### idle

@protected GSLinkedList* idle;
Description forthcoming.

### live

@protected GSLinkedList* live;
Description forthcoming.

### maxOperations

@protected NSUInteger maxOperations;
Description forthcoming.

### maxThreads

@protected NSUInteger maxThreads;
Description forthcoming.

### operations

@protected GSLinkedList* operations;
Description forthcoming.

### poolLock

@protected NSRecursiveLock* poolLock;
Description forthcoming.

### processed

@protected NSUInteger processed;
Description forthcoming.

### shutdown

@protected BOOL shutdown;
Description forthcoming.

### suspended

@protected BOOL suspended;
Description forthcoming.

### unused

@protected GSLinkedList* unused;
Description forthcoming.