Simple Thread Pool

Source: Internet
Author: User
//  
// 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.