简单的线程池

来源:互联网 时间:1970-01-01

//

// ThreadPool.h

/**

* 继承此类,重写execute方法 */
@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{
NSAutoreleasePool* pool = [[NSAutoreleasePool 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


相关阅读:
Top