Linux下调用pthread库实现简单线程池
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <pthread.h>
#include <assert.h>
void * routine(void * arg);
int pool_add_job(void *(*process)(void * arg),void *arg);
int pool_init(unsigned int thread_num);
int pool_destroy(void);
void *test(void *);
/* We define a queue of jobs which will be processed in thread pool*/
typedef struct job{
void * (*process)(void *arg); /* process() will employed on job*/
void *arg;/* argument to process */
struct job * next;
}Job;
/*
* A threadpool must have the following parts:
* threads: a list of threads
* jobs : jobs in thread pool
* pool_lock: pthread_mutex_lock for thread pool
* job_ready: pthread_cond_t for job ready
* destroy: flag indicate whether the pool shall be destroyed
* size: current size of jobs list
* thread_num: max thread num initialized in pool
*/
typedef struct thread_pool{
pthread_mutex_t pool_lock;
pthread_cond_t job_ready;
Job * jobs;
int destroy;
pthread_t * threads;
unsigned int thread_num;
int size;
}Thread_pool;
/* global Thread_pool variable*/
static Thread_pool * pool=NULL;
/*Initialize the thread pool*/
int pool_init(unsigned int thread_num)
{
pool=(Thread_pool *)malloc(sizeof(Thread_pool));
if(NULL==pool)
return -1;
pthread_mutex_init(&(pool->pool_lock),NULL);
pthread_cond_init(&(pool->job_ready),NULL);
pool->jobs=NULL;
pool->thread_num=thread_num;
pool->size=0;
pool->destroy=0;
pool->threads=(pthread_t *)malloc(thread_num * sizeof(pthread_t));
int i;
for(i=0;i<thread_num;i++){
pthread_create(&(pool->threads[i]),NULL,routine,NULL);
}
return 0;
}
/*
* Add job into the pool
* assign it to some thread
*/
int pool_add_job(void *(*process)(void *),void *arg)
{
Job * newjob=(Job *)malloc(sizeof(Job));
newjob->process=process;
newjob->arg=arg;
newjob->next=NUL
相关文档:
一、引言
想使用Linux已经很长时间了,由于没有硬性任务一直也没有系统学习,近日由于工作需要必须使用Linux下的MySQL。本以为有
Windows下使用SQL
Server的经验,觉得在Linux下安装MySql应该是易如反掌的事,可在真正安装和使用MySQL时走了很多弯路,遇见很多问题,毕竟Linux
和Windows本身就有很大区别。为了让 ......
C标准函数,似乎不应该有平台问题: 我们看一下这个程序: #include
#include
int main()
{
rename(”/tmp/abc”,”/tmp/bcd”);
perror(”why:”);
} 此程序运行一切正常。 自己将路径修改为不同的分区之间移动,或者将同一分区挂载到不同的mou ......
LINUX 线程函数大全
线程
创建一个缺省的线程
缺省的线程的属性:
l 非绑定
l 未分离
l 一个缺省大小的堆栈
l &nb ......
网上关于中文输入的文章很多,此处只是想疏理一下几个概念。
1。输入法平台:
windows下输入法与输入法平台好像是分不清的,而在Linux下他们的概念还是分开的好,输入法平台是输入法的基础,有时候你安装了某个输入法,却怎么也调不出来,则有可能就是它所需要的平台还没有建立。
几个输入法平台:
xcin(x-ChineseInpu ......