# GSIOThreadPool documentation

### Authors

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

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

### Contents -

1. Software documentation for the GSIOThread class
2. Software documentation for the GSIOThreadPool class
3. GSIOThreadPool types

# Software documentation for the GSIOThread class

## GSIOThread : NSThread

> Declared in:
>   GSIOThreadPool.h

This is the class for threads in the pool.
Each thread runs a runloop and is kept 'alive' waiting for a timer in the far future, but can be terminated earlier using the -terminate: method (which is called when the pool size is changed and the pool wishes to stop an idle thread).
Method summary

- -shutdown
- -startup
- -terminate:

### shutdown

- (void) shutdown;
Called when the thread is shut down (immediately before exit).
Does nothing, provided for subclasses to override.

### startup

- (void) startup;
Called when the thread is started up (before the run loop starts).
Does nothing, provided for subclasses to override.

### terminate:

- (void) terminate: (NSDate*)when;
Terminates the thread by the specified date (as soon as possible if the date is `nil` or is in the past).
If called from another thread, this method asks the receiver thread to perform the method, and waits (running the run loop in the calling thread) until either the receiver thread finishes executing or until the timeout date ( when) is reached.

# Software documentation for the GSIOThreadPool class

## GSIOThreadPool : NSObject

> Declared in:
>   GSIOThreadPool.h

This class provides a thread pool for performing methods which need to make use of a runloop for I/O and/or timers.
Operations are performed on these threads using the standard -performSelector:onThread:withObject:waitUntilDone: method... the pool is simply used to keep track of allocation of threads so that you can share jobs between them.
NB. The threading API in OSX 10.4 and earlier is incapable of supporting this functionality... in that case this class cannot be instantiated and initialised.

Instance Variables
Method summary

- +sharedPool
- -acquireThread
- -countForThread:
- -maxThreads
- -setPoolName:
- -setThreadClass:
- -setThreads:
- -setTimeout:
- -timeout
- -unacquireThread:

### sharedPool

+ (GSIOThreadPool*) 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 other sections of code.
Always returns the same instance whenever the method is called.
The shared pool is created with an initial size as specified by the GSIOThreadPoolSize user default (zero if there is no such positive integer in the defauilts system), however you can modify that using the -setThreads: method.

### acquireThread

- (NSThread*) acquireThread;
Selects a thread from the pool to be used for some job.
This method selectes the least used thread in the pool (ie the one with the lowest acquire count).
If the receiver is configured with a size of zero, the main thread is returned.

### countForThread:

- (NSUInteger) countForThread: (NSThread*)aThread;
Returns the acquire count for the specified thread.

### maxThreads

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

### setPoolName:

- (void) setPoolName: (NSString*)aName;
Sets the base name for threads in this pool. As threads are created they are given names formed by assing the value of a counter to the base name.

### setThreadClass:

- (void) setThreadClass: (Class)aClass;
Sets the class to be used to create any new threads in this pool.
Must be a subclass of the GSIOThread class.

### 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 0 (no thread pooling in use).
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 lowered.
Setting a value of zero means that operations are performed in the main thread.

### setTimeout:

- (void) setTimeout: (NSTimeInterval)t;
Specifies the timeout allowed for a thread to close down when the pool is deallocated or has its size decreased. Any operations in progress in the thread need to close down within this period.

### timeout

- (NSTimeInterval) timeout;
Returns the current timeout set for the pool.

### unacquireThread:

- (void) unacquireThread: (NSThread*)aThread;
Releases a thread previously selected from the pool. This decreases the acquire count for the thread. If a thread has a zero acquire count, it is a candidate for termination and removal from the pool if/when the pool has its size changed.

## Instance Variables for GSIOThreadPool Class

### created

@protected unsigned int created;
Description forthcoming.

### maxThreads

@protected NSUInteger maxThreads;
Description forthcoming.

### poolName

@protected NSString* poolName;
Description forthcoming.

### threadClass

@protected Class threadClass;
Description forthcoming.

### threads

@protected NSMutableArray* threads;
Description forthcoming.

### timeout

@protected NSTimeInterval timeout;
Description forthcoming.

# GSIOThreadPool types

### NSUInteger

typedef unsigned int NSUInteger;
Description forthcoming.