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
相关文档:
1、下载MySQL的安装文件
安装MySQL需要下面两个文件:
MySQL-server-5.0.46.i386.rpm
MySQL-client-5.0.46.i386.rpm
2、安装MySQL
rpm文件是Red Hat公司开发的软件安装包,rpm可让Linux在安装软件包时免除许多复杂的手续。该命令在安装时常用的参数是
ivh
,其中i表示将安装指定的rmp软件包,V表 ......
无意中看到了一篇zz
http://hi.baidu.com/lyricidyll/blog/item/54e8f9a9c35bb8bbca130c7c.html
挖Linux中的古老缩略语
Unix已经有35年历史了。许多人认为它开始于中世纪,这个中世纪是相对于计算机技术的产生和发展来说的。在过去的时间里,Unix和它的子分支
Linux收集有许多的历史和一些完全古老的语言。 ......
By:
吴垠 Date:
2007-05-18 Email:
lazy_fox#msn.com Homepage:
http://blog.csdn.net/wooin Link:
http://blog.csdn.net/wooin/archive/2007/05/21/1619141.aspx 版权信息:
该文章版权由Wu Yin所有。可在非商业目的下任意传播和复制。
对于商业目的下对本文的任何行为需经作者 ......
LINUX 线程函数大全
线程
创建一个缺省的线程
缺省的线程的属性:
l 非绑定
l 未分离
l 一个缺省大小的堆栈
l &nb ......
查看安装的系统信息:
简单的信息:uname -a
详细的信息:cat /proc/version
cat /etc/issue
lsb_release -a
Gentoo上安装mysql,直接运行:
(1) bigner
@localhost
/ $ sudo emerge mysql
  ......