//
// Threadpool. h
/* *
* Inherit this class and override the execute Method
*/
@ Interface Workitem: nsobject
- ( Void ) Execute;
@ End
@ Class Threadpoolinner;
@ Interface Threadpool: nsobject {
Threadpoolinner * _ Inner;
}
- ( ID ) Initwiththreadcount :( nsinteger) threadcount;
- ( Void ) Addworkitem :( workitem * ) Workitem;
- ( Void ) Cancelallworkitems;
- ( Void ) Dealloc;
@ End
//
// Threadpool. m
# Import " Threadpool. h "
# Include < Pthread. h >
@ Interface Threadpoolinner: nsobject {
Nsmutablearray * _ Workthreads;
Nsinteger _ workthreadcount;
Nscondition * _ Condition;
Nsmutablearray * _ Workitems;
}
- ( ID ) Initwiththreadcount :( nsinteger) threadcount;
- ( Void ) Pushworkitem :( workitem * ) Workitem;
- (Workitem * ) Popworkitem;
- ( Void ) Removeallworkitems;
- ( Void ) Dealloc;
@ End
@ Interface Workthread: nsobject {
Threadpoolinner * _ Inner;
Nsthread * _ Thread;
Pthread_t _ nativethread;
Volatile Bool _ isrun;
}
- ( ID ) Initwiththreadpoolinner :( threadpoolinner * ) Inner;
- ( Void ) Run;
- ( Void ) Dispose;
- ( Void ) Dealloc;
@ End
@ Implementation Workitem
- ( Void ) Execute {
}
@ End
@ Implementation Threadpool
- ( ID ) Initwiththreadcount :( nsinteger) threadcount {
Self = [Super init];
If (Self ){
Self -> _ Inner = [[Threadpoolinner alloc] initwiththreadcount: threadcount];
}
Return Self;
}
- ( Void ) Addworkitem :( workitem * ) Workitem {
[_ Inner pushworkitem: workitem];
}
- ( Void ) Cancelallworkitems {
[_ Inner removeallworkitems];
}
- ( Void ) Dealloc {
[_ Inner release];
[Super dealloc];
}
@ End
@ Implementation Threadpoolinner
- ( ID ) Initwiththreadcount :( nsinteger) threadcount {
Self = [Super init];
If (Self ){
Self -> _ Workthreads = [[Nsmutablearray alloc] initwithcapacity: threadcount];
Self -> _ Workthreadcount = Threadcount;
Self -> _ Condition = [[Nscondition alloc] init];
Self -> _ Workitems = [[Nsmutablearray alloc] init];
}
Return Self;
}
- ( Void ) Startworkthreads {
If ([_ Workthreads count] = 0 && _ Workthreadcount > 0 ){
For (Nsinteger Index = 0 ; Index < _ Workthreadcount; index ++ ){
Workthread * Workthread = [[Workthread alloc] initwiththreadpoolinner: Self];
[_ Workthreads addobject: workthread];
[Workthread release];
}
}
}
- ( Void ) Pushworkitem :( workitem * ) Workitem {
[Self startworkthreads];
[_ Condition Lock ];
[_ Workitems addobject: workitem];
[_ Condition broadcast];
[_ Condition unlock];
}
- (Workitem * ) Popworkitem {
Workitem * Workitem = Nil;
[_ Condition Lock ];
If ([_ Workitems count] > 0 ){
Workitem = [[_ Workitems objectatindex: 0 ] Retain];
[_ Workitems removeobjectatindex: 0 ];
} Else {
[_ Condition Wait];
}
[_ Condition unlock];
Return Workitem;
}
- ( Void ) Removeallworkitems {
[_ Condition Lock ];
[_ Workitems removeallobjects];
[_ Condition unlock];
}
- ( Void ) Disposeworkthreads {
For (Workthread * Workthread In _ Workthreads ){
[Workthread dispose];
}
}
- ( Void ) Dealloc {
[Self disposeworkthreads];
[_ Workthreads release];
[_ Workitems removeallobjects];
[_ Condition release];
[Super dealloc];
}
@ End
@ Implementation Workthread
- ( ID ) Initwiththreadpoolinner :( threadpoolinner * ) Inner {
Self = [Super init];
If (Self ){
Self -> _ Inner = Inner;
Self -> _ Nativethread = NULL;
_ Thread = [[Nsthread alloc] initwithtarget: Self selector: @ selector (run) Object : Nil];
_ Isrun = Yes;
[_ Thread start];
}
Return Self;
}
- ( Void ) Run {
NSAID utoreleasepool * Pool = [[NSAID utoreleasepool alloc] init];
If ( ! _ Nativethread ){
_ Nativethread = Pthread_self ();
}
While (_ Isrun ){
Workitem * Workitem = [_ Inner popworkitem];
If (Workitem ){
[Workitem execute];
[Workitem release];
}
}
[Pool drain];
}
- ( Void ) Dispose {
_ Isrun = No;
Pthread_join (_ nativethread, null );
_ Nativethread = NULL;
[_ Thread release];
_ Thread = Nil;
}
- ( Void ) Dealloc {
[Super dealloc];
}
@ End