在OC的框架中从NSOperation到GCD的dispatch queue到处都充斥着队列的概念,OC的框架帮我们把底层线程的调度都已经写好了,这样的好处是我们可以专心于上层的业务逻辑,坏处当然是我们对底层调度的掌控力变弱了。写这个线程池的原因也是练练手,至于效率如何,在发到线上几个版本后,反馈还可以。当然还有空间可以持续优化。
typedef void (*tfmethod)(void *,short *);
typedef struct s_tftask_content{
tfmethod method;
void *arg;
short *cancel_point;
struct s_tftask_content *next;
}s_tftask_content;
typedef struct s_tfthread_config{
pthread_mutex_t mtx;
pthread_cond_t cond;
}s_tfthread_config;
typedef struct s_tftask{
s_tfthread_config config;
short thread_status;
s_tftask_content *task_list;
}s_tftask;
enum tfthread_status{
tfthread_init=0x0,
tfthread_idle,
tfthread_inuse,
tfthread_dead
};
enum tftask_type{
tftask_net_req=0x0,
tftask_first_respond,
tftask_assign,
tftask_io_write
};
typedef struct s_tfthread{
s_tftask *tftask;
char *name;
}s_tfthread;
s_tftask_content *content = (s_tftask_content *)malloc(sizeof(s_tftask_content));
content->next= NULL;
content->method = empty_run;
content->cancel_point=NULL;
s_tftask *task = (s_tftask *)malloc(sizeof(s_tftask));
task->task_list = content;
task->thread_status = tfthread_idle;
s_tfthread_config config = {PTHREAD_MUTEX_INITIALIZER,PTHREAD_COND_INITIALIZER};
task->config = config;
s_tfthread *thread = (s_tfthread *)malloc(sizeof(s_tfthread));
thread->tftask = task;
thread->name = k_tfthread_names[i];
k_threads[i]=thread;
ret = pthread_create(&posix_t_id, &attr, tfthreadpool_task_run,task);