线程
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
int var = 100;
void *run(void *arg) {
int i;
var = 200;
i = (int *) arg;
if (i == 2) {
pthread_exit(NULL);
}
printf("pid=%d tid=%lu %d var=%d \n", getpid(), pthread_self(), i, var);
}
void demo() {
printf("pid=%d tid=%lu \n", getpid(), pthread_self());
pthread_t tid;
int ret, i;
for (i = 0; i < 5; i++) {
ret = pthread_create(&tid, NULL, run, (void *) i);
if (ret != 0) {
perror("error");
}
}
printf("var=%d \n", var);
pthread_exit(NULL);
}
struct thre {
int var;
char str[256];
};
void *fun(void *args) {
struct thre *t = (struct thre *) args;
t = malloc(sizeof(t));
t->var = 100;
strcpy(t->str, "hello");
return (void *) t;
}
void demo2() {
pthread_t *tid;
pthread_t arg;
int ret = pthread_create(&tid, NULL, fun, (void *) &arg);
if (ret != 0) {
perror("error");
exit(1);
}
struct thre t2;
struct thre *t = &t2;
pthread_join(tid, (void **) &t);
printf("var=%d str=%s \n", t->var, t->str);
pthread_exit(NULL);
}
void *fun3(void *args) {
while (1) {
pthread_testcancel();
}
return (void *) 66;
}
void demo3() {
pthread_t pthread;
int ret = pthread_create(&pthread, NULL, fun3, NULL);
if (ret != 0) {
perror("error");
exit(1);
}
printf("pid=%d tid=%lu \n", getpid(), pthread_self());
sleep(5);
ret = pthread_cancel(pthread);
void *tret = NULL;
pthread_join(pthread, &tret);
printf("%d \n", tret);
if (ret != 0) {
perror("error");
exit(1);
}
while (1);
}
void *fun4(void *args) {
return (void *) 66;
}
void demo4() {
pthread_t pthread;
int ret = pthread_create(&pthread, NULL, fun4, NULL);
if (ret != 0) {
fprintf(stderr, "error: %s \n", strerror(ret));
exit(1);
}
ret = pthread_detach(pthread);
if (ret != 0) {
fprintf(stderr, "error: %s \n", strerror(ret));
exit(1);
}
sleep(1);
ret = pthread_join(pthread, NULL);
if (ret != 0) {
fprintf(stderr, "error: %s \n", strerror(ret));
exit(1);
}
pthread_exit((void *) 0);
}
void demo5() {
int ret;
pthread_attr_t pa;
ret = pthread_attr_init(&pa);
if (ret != 0) {
fprintf(stderr, "error: %s \n", strerror(ret));
exit(1);
}
ret = pthread_attr_setdetachstate(&pa, PTHREAD_CREATE_DETACHED);
if (ret != 0) {
fprintf(stderr, "error: %s \n", strerror(ret));
exit(1);
}
pthread_t pthread;
ret = pthread_create(&pthread, &pa, fun4, NULL);
if (ret != 0) {
fprintf(stderr, "error: %s \n", strerror(ret));
exit(1);
}
ret = pthread_attr_destroy(&pa);
if (ret != 0) {
fprintf(stderr, "error: %s \n", strerror(ret));
exit(1);
}
ret = pthread_join(pthread, NULL);
if (ret != 0) {
fprintf(stderr, "error: %s \n", strerror(ret));
exit(1);
}
printf("pid=%d tid=%lu \n", getpid(), pthread_self());
pthread_exit((void *) 0);
}
int main() {
return 0;
}
线程池
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
#define DEFAULT_TIME 10
#define MIN_WAIT_TASK_NUM 10
#define DEFAULT_THREAD_VARY 10
#define true 1
#define false 0
typedef struct {
void *(*function)(void *);
void *arg;
} threadpool_task_t;
typedef struct threadpool_t {
pthread_mutex_t lock;
pthread_mutex_t thread_counter;
pthread_cond_t queue_not_full;
pthread_cond_t queue_not_empty;
pthread_t *threads;
pthread_t adjust_tid;
threadpool_task_t *task_queue;
int min_thr_num;
int max_thr_num;
int live_thr_num;
int busy_thr_num;
int wait_exit_thr_num;
int queue_front;
int queue_rear;
int queue_size;
int queue_max_size;
int shutdown;
}threadpool_t ;
void *threadpool_thread(void *threadpool);
void *adjust_thread(void *threadpool);
int is_thread_alive(pthread_t tid);
int threadpool_free(threadpool_t *pool);
threadpool_t *threadpool_create(int min_thr_num, int max_thr_num, int queue_max_size)
{
int i;
threadpool_t *pool = NULL;
do {
if((pool = (threadpool_t *)malloc(sizeof(threadpool_t))) == NULL) {
printf("malloc threadpool fail");
break;
}
pool->min_thr_num = min_thr_num;
pool->max_thr_num = max_thr_num;
pool->busy_thr_num = 0;
pool->live_thr_num = min_thr_num;
pool->queue_size = 0;
pool->queue_max_size = queue_max_size;
pool->queue_front = 0;
pool->queue_rear = 0;
pool->shutdown = false;
pool->threads = (pthread_t *)malloc(sizeof(pthread_t)*max_thr_num);
if (pool->threads == NULL) {
printf("malloc threads fail");
break;
}
memset(pool->threads, 0, sizeof(pthread_t)*max_thr_num);
pool->task_queue = (threadpool_task_t *)malloc(sizeof(threadpool_task_t)*queue_max_size);
if (pool->task_queue == NULL) {
printf("malloc task_queue fail");
break;
}
if (pthread_mutex_init(&(pool->lock), NULL) != 0
|| pthread_mutex_init(&(pool->thread_counter), NULL) != 0
|| pthread_cond_init(&(pool->queue_not_empty), NULL) != 0
|| pthread_cond_init(&(pool->queue_not_full), NULL) != 0)
{
printf("init the lock or cond fail");
break;
}
for (i = 0; i < min_thr_num; i++) {
pthread_create(&(pool->threads[i]), NULL, threadpool_thread, (void *)pool);
printf("start thread 0x%x...\n", (unsigned int)pool->threads[i]);
}
pthread_create(&(pool->adjust_tid), NULL, adjust_thread, (void *)pool);
return pool;
} while (0);
threadpool_free(pool);
return NULL;
}
int threadpool_add(threadpool_t *pool, void*(*function)(void *arg), void *arg)
{
pthread_mutex_lock(&(pool->lock));
while ((pool->queue_size == pool->queue_max_size) && (!pool->shutdown)) {
pthread_cond_wait(&(pool->queue_not_full), &(pool->lock));
}
if (pool->shutdown) {
pthread_mutex_unlock(&(pool->lock));
}
if (pool->task_queue[pool->queue_rear].arg != NULL) {
free(pool->task_queue[pool->queue_rear].arg);
pool->task_queue[pool->queue_rear].arg = NULL;
}
pool->task_queue[pool->queue_rear].function = function;
pool->task_queue[pool->queue_rear].arg = arg;
pool->queue_rear = (pool->queue_rear + 1) % pool->queue_max_size;
pool->queue_size++;
pthread_cond_signal(&(pool->queue_not_empty));
pthread_mutex_unlock(&(pool->lock));
return 0;
}
void *threadpool_thread(void *threadpool)
{
threadpool_t *pool = (threadpool_t *)threadpool;
threadpool_task_t task;
while (true) {
pthread_mutex_lock(&(pool->lock));
while ((pool->queue_size == 0) && (!pool->shutdown)) {
printf("thread 0x%x is waiting\n", (unsigned int)pthread_self());
pthread_cond_wait(&(pool->queue_not_empty), &(pool->lock));
if (pool->wait_exit_thr_num > 0) {
pool->wait_exit_thr_num--;
if (pool->live_thr_num > pool->min_thr_num) {
printf("thread 0x%x is exiting\n", (unsigned int)pthread_self());
pool->live_thr_num--;
pthread_mutex_unlock(&(pool->lock));
pthread_exit(NULL);
}
}
}
if (pool->shutdown) {
pthread_mutex_unlock(&(pool->lock));
printf("thread 0x%x is exiting\n", (unsigned int)pthread_self());
pthread_exit(NULL);
}
task.function = pool->task_queue[pool->queue_front].function;
task.arg = pool->task_queue[pool->queue_front].arg;
pool->queue_front = (pool->queue_front + 1) % pool->queue_max_size;
pool->queue_size--;
pthread_cond_broadcast(&(pool->queue_not_full));
pthread_mutex_unlock(&(pool->lock));
printf("thread 0x%x start working\n", (unsigned int)pthread_self());
pthread_mutex_lock(&(pool->thread_counter));
pool->busy_thr_num++;
pthread_mutex_unlock(&(pool->thread_counter));
(*(task.function))(task.arg);
printf("thread 0x%x end working\n", (unsigned int)pthread_self());
pthread_mutex_lock(&(pool->thread_counter));
pool->busy_thr_num--;
pthread_mutex_unlock(&(pool->thread_counter));
}
pthread_exit(NULL);
}
void *adjust_thread(void *threadpool)
{
int i;
threadpool_t *pool = (threadpool_t *)threadpool;
while (!pool->shutdown) {
sleep(DEFAULT_TIME);
pthread_mutex_lock(&(pool->lock));
int queue_size = pool->queue_size;
int live_thr_num = pool->live_thr_num;
pthread_mutex_unlock(&(pool->lock));
pthread_mutex_lock(&(pool->thread_counter));
int busy_thr_num = pool->busy_thr_num;
pthread_mutex_unlock(&(pool->thread_counter));
if (queue_size >= MIN_WAIT_TASK_NUM && live_thr_num < pool->max_thr_num) {
pthread_mutex_lock(&(pool->lock));
int add = 0;
for (i = 0; i < pool->max_thr_num && add < DEFAULT_THREAD_VARY
&& pool->live_thr_num < pool->max_thr_num; i++) {
if (pool->threads[i] == 0 || !is_thread_alive(pool->threads[i])) {
pthread_create(&(pool->threads[i]), NULL, threadpool_thread, (void *)pool);
add++;
pool->live_thr_num++;
}
}
pthread_mutex_unlock(&(pool->lock));
}
if ((busy_thr_num * 2) < live_thr_num && live_thr_num > pool->min_thr_num) {
pthread_mutex_lock(&(pool->lock));
pool->wait_exit_thr_num = DEFAULT_THREAD_VARY;
pthread_mutex_unlock(&(pool->lock));
for (i = 0; i < DEFAULT_THREAD_VARY; i++) {
pthread_cond_signal(&(pool->queue_not_empty));
}
}
}
return NULL;
}
int threadpool_destroy(threadpool_t *pool)
{
int i;
if (pool == NULL) {
return -1;
}
pool->shutdown = true;
pthread_join(pool->adjust_tid, NULL);
for (i = 0; i < pool->live_thr_num; i++) {
pthread_cond_broadcast(&(pool->queue_not_empty));
}
for (i = 0; i < pool->live_thr_num; i++) {
pthread_join(pool->threads[i], NULL);
}
threadpool_free(pool);
return 0;
}
int threadpool_free(threadpool_t *pool)
{
if (pool == NULL) {
return -1;
}
if (pool->task_queue) {
free(pool->task_queue);
}
if (pool->threads) {
free(pool->threads);
pthread_mutex_lock(&(pool->lock));
pthread_mutex_destroy(&(pool->lock));
pthread_mutex_lock(&(pool->thread_counter));
pthread_mutex_destroy(&(pool->thread_counter));
pthread_cond_destroy(&(pool->queue_not_empty));
pthread_cond_destroy(&(pool->queue_not_full));
}
free(pool);
pool = NULL;
return 0;
}
int threadpool_all_threadnum(threadpool_t *pool)
{
int all_threadnum = -1;
pthread_mutex_lock(&(pool->lock));
all_threadnum = pool->live_thr_num;
pthread_mutex_unlock(&(pool->lock));
return all_threadnum;
}
int threadpool_busy_threadnum(threadpool_t *pool)
{
int busy_threadnum = -1;
pthread_mutex_lock(&(pool->thread_counter));
busy_threadnum = pool->busy_thr_num;
pthread_mutex_unlock(&(pool->thread_counter));
return busy_threadnum;
}
int is_thread_alive(pthread_t tid)
{
int kill_rc = pthread_kill(tid, 0);
if (kill_rc == ESRCH) {
return false;
}
return true;
}
#if 1
void *process(void *arg)
{
printf("thread 0x%x working on task %d\n ",(unsigned int)pthread_self(),*(int *)arg);
sleep(1);
printf("task %d is end\n",*(int *)arg);
return NULL;
}
int main(void)
{
threadpool_t *thp = threadpool_create(3,100,100);
printf("pool inited");
int num[20], i;
for (i = 0; i < 20; i++) {
num[i]=i;
printf("add task %d\n",i);
threadpool_add(thp, process, (void*)&num[i]);
}
sleep(10);
threadpool_destroy(thp);
return 0;
}
#endif
互斥锁
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
pthread_mutex_t mutex;
void *fun(void *arg) {
srand(time(NULL));
while (1) {
pthread_mutex_lock(&mutex);
printf(" hello ");
sleep(rand() % 1);
printf(" world \n");
sleep(rand() % 1);
pthread_mutex_unlock(&mutex);
}
return NULL;
}
void lock() {
srand(time(NULL));
int ret = pthread_mutex_init(&mutex, NULL);
if (ret != 0) {
fprintf(stderr, "error: %s \n", strerror(ret));
exit(1);
}
pthread_t tid;
pthread_create(&tid, NULL, fun, NULL);
while (1) {
pthread_mutex_lock(&mutex);
printf(" HELLO ");
sleep(rand() % 1);
printf(" WORLD \n");
sleep(rand() % 1);
pthread_mutex_unlock(&mutex);
}
pthread_join(tid, NULL);
pthread_mutex_destroy(&mutex);
}
int main() {
return 0;
}
使用条件变量+互斥锁实现生产者消费者
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t condition = PTHREAD_COND_INITIALIZER;
struct msg {
struct msg *next;
int num;
};
struct msg *head;
void *consumer(void *p) {
struct msg *m;
while (1) {
pthread_mutex_lock(&lock);
while (head == NULL) {
pthread_cond_wait(&condition, &lock);
}
m = head;
head = m->next;
pthread_mutex_unlock(&lock);
printf("consumer id=%lu,val=%d \n",pthread_self(), m->num);
free(m);
sleep(rand() % 3);
}
}
void *producer(void *p) {
struct msg *m;
while (1) {
m=malloc(sizeof(struct msg));
m->num = rand() % 3;
pthread_mutex_lock(&lock);
m->next = head;
head = m;
printf("produce val=%d \n", m->num);
pthread_mutex_unlock(&lock);
pthread_cond_signal(&condition);
sleep(rand() % 2);
}
}
int main() {
pthread_t pid, cid,cid2,cid3;
pthread_create(&pid, NULL, producer, NULL);
pthread_create(&cid, NULL, consumer, NULL);
pthread_create(&cid2, NULL, consumer, NULL);
pthread_create(&cid3, NULL, consumer, NULL);
pthread_join(pid, NULL);
pthread_join(cid, NULL);
pthread_join(cid2, NULL);
pthread_join(cid3, NULL);
return 0;
}
使用信号量实现生产者消费者
#include <semaphore.h>
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#define NUM 5
int queue[NUM];
sem_t blank_number, product_number;
void *consumer(void *p) {
int i = 0;
while (1) {
sem_wait(&product_number);
queue[i] = rand() % 1000 + 1;
printf("consumer %d \n", queue[i]);
queue[i]=0;
sem_post(&blank_number);
i = (i + 1) % NUM;
sleep( rand() % 3);
}
}
void *producer(void *p) {
int i = 0;
while (1) {
sem_wait(&blank_number);
queue[i] = rand() % 1000 + 1;
printf("produce %d \n", queue[i]);
i = (i + 1) % NUM;
sleep( rand() % 1);
}
}
int main() {
pthread_t pid, cid;
sem_init(&blank_number, 0, NUM);
sem_init(&product_number, 0, 0);
pthread_create(&pid, NULL, producer, NULL);
pthread_create(&cid, NULL, consumer, NULL);
pthread_join(pid, NULL);
pthread_join(cid, NULL);
sem_destroy(&blank_number);
sem_destroy(&product_number);
return 0;
}