APUE中文第三版11章读写锁的一个可运行的例子

来源:互联网 发布:linux网络故障排查 编辑:程序博客网 时间:2024/06/06 00:46

我的测试环境是CentOS7(内核版本3.10.0),X86_64.

头文件: wrlock.h

#ifndef _WRLOCK_H#define _WRLOCK_H#include <stdlib.h>#include <pthread.h>#include <stdio.h>struct job {    struct job  *j_next;    struct job  *j_prev;    pthread_t   tid;    void *( *job_func)( void *);};struct queue {    struct job      *q_head;    struct job      *q_tail;    pthread_rwlock_t    q_lock; };int queue_init( struct queue *qp );void job_insert( struct queue *qp, struct job *jp );void job_append( struct queue *qp, struct job *jp );void job_remove( struct queue *qp, struct job *jp );struct job *job_find( struct queue *qp, pthread_t tid );#endif

队列的相关操作,这一部分与书上的基本一样。
11_14.c

#include "wrlock.h"intqueue_init( struct queue *qp ){    int err;    qp->q_head = qp->q_tail = NULL;    if ((err = pthread_rwlock_init(&qp->q_lock,NULL)) != 0)        return err;    return 0;}/* insert a job at the head of queue */void job_insert( struct queue *qp, struct job *jp ){    pthread_rwlock_wrlock(&qp->q_lock);    jp->j_next = qp->q_head;    jp->j_prev = NULL;    if ( qp->q_head != NULL )        qp->q_head->j_prev = jp;    else            qp->q_tail = jp;    qp->q_head = jp;    pthread_rwlock_unlock(&qp->q_lock);}/* append a job on the tail of queue */void job_append( struct queue *qp, struct job *jp ){    pthread_rwlock_wrlock(&qp->q_lock);    jp->j_next = NULL;    jp->j_prev = qp->q_tail;    if ( qp->q_tail != NULL )        qp->q_tail->j_next = jp;    else            qp->q_head = jp;    qp->q_tail = jp;    pthread_rwlock_unlock(&qp->q_lock);}voidjob_remove( struct queue *qp, struct job *jp ){    pthread_rwlock_wrlock(&qp->q_lock);    /*     ** remove the head job,maybe jp pointing the only job.    */    if (jp == qp->q_head){        qp->q_head = jp->j_next;        if (jp->j_next != NULL)            jp->j_next->j_prev = NULL;        else             qp->q_tail = NULL;    }  /*        ** remove the tail job, if jp pointing the only job,        ** then if{} statements has executed, we can't get here.       */    else if ( jp == qp->q_tail ){        qp->q_tail = jp->j_prev;        jp->j_prev->j_next = NULL;    } else{        jp->j_next->j_prev = jp->j_prev;        jp->j_prev->j_next = jp->j_next;    }    pthread_rwlock_unlock(&qp->q_lock);}struct job *job_find( struct queue *qp, pthread_t tid ){    struct job *jtmp;    if(pthread_rwlock_rdlock(&qp->q_lock) != 0)        return NULL;    for ( jtmp = qp->q_head; jtmp != NULL; jtmp = jtmp->j_next ){        if (pthread_equal(jtmp->tid,tid) != 0)            break;    }    pthread_rwlock_unlock(&qp->q_lock);    return jtmp;}

main函数部分,主线程负责初始化队列,将产生的job插入队列,job结构中有指派的线程的id,以及给出的任务(函数指针)。其他的工作线程从队列中找到属于自己的job,然后利用这个函数指针调用函数,完成任务。任务很简单,就是打印一句话。

main.c:

#include "wrlock.h"#define _GNU_SOURCEvoid *task1( void *str1 ){    printf("I am thread 1, %s\n",(char *)str1);}void *task2( void *str2 ){    printf("I am thread 2, %s\n",(char *)str2);}void *task3( void *str3 ){    printf("I am thread 3, %s\n",(char *)str3);}void *thr1_func( void *arg ) //arg points to queue.{    pthread_t   tid;    struct job  *jp1;    tid = pthread_self();    while (1){        if ((jp1 = job_find(arg,tid)) != NULL){            jp1->job_func("task completly!");            job_remove(arg,jp1);             free(jp1);        }else{            pthread_yield();        }    }}void *thr2_func( void *arg ) //arg points to queue.{    pthread_t   tid;    struct job  *jp2;    tid = pthread_self();    while (1){        if ((jp2 = job_find(arg,tid)) != NULL){            jp2->job_func("task completly!");            job_remove(arg,jp2);             free(jp2);        }else{            pthread_yield();        }    }}void *thr3_func( void *arg ) //arg points to queue.{    pthread_t   tid;    struct job  *jp3;    tid = pthread_self();    while (1){        if ((jp3 = job_find(arg,tid)) != NULL){            jp3->job_func("task completly!");            job_remove(arg,jp3);             free(jp3);        }else{            pthread_yield();        }    }}int main( void ){    unsigned long   n;    int         err;    int         foo;    struct job  *jobptr;    struct queue    *qptr;    pthread_t   tid[3];    void *(*func[3])(void *) = { task1, task2, task3 };    /* initialise the queue */    if ((qptr = malloc(sizeof( struct queue ))) == NULL)        exit(-1);    queue_init(qptr);    /* produce task in main thread */    err = pthread_create(&tid[0],NULL,thr1_func,qptr);    if (err != 0)        exit(-1);    else        printf("create thread 1 success\n");    err = pthread_create(&tid[1],NULL,thr2_func,qptr);    if (err != 0)        exit(-1);    else        printf("create thread 2 success\n");    err = pthread_create(&tid[2],NULL,thr3_func,qptr);    if (err != 0)        exit(-1);    else        printf("create thread 3 success\n");    for ( n = 1; n <= 10; n++){        if ((jobptr = malloc(sizeof(struct job))) == NULL)            exit(-1);        foo = rand()%3;        jobptr->tid = tid[foo];        jobptr->job_func = func[foo];        job_insert(qptr,jobptr);    }    sleep(10);    exit(0);}
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 落枕10天还不好怎么办 落枕十几天没好怎么办 肩一边高一边低怎么办 35岁无稳定工作怎么办 机械手表走的快怎么办 机械表发条紧了怎么办 两只乌龟互相咬怎么办? 乌龟鼻子摔烂了怎么办 剃刀龟脖子肿了怎么办 遇见有戾气的人怎么办 身上的寒气太重怎么办 身体里寒气太重怎么办 做人事招不到人怎么办 苹果6cpu坏了怎么办 苹果6s升级不了怎么办 冬天打游戏手冷怎么办 漂流瓶不能用了怎么办 感冒鼻子闻不到味道怎么办 胃难受想吐头晕怎么办 心口窝堵得慌怎么办 嘴巴里苦的很怎么办 怀孕了嘴巴好苦怎么办 嘴巴没味道想吐怎么办 手机流量不够用怎么办移动 sd卡图片不显示怎么办 苹果忘了id账号怎么办 苹果id号忘记了怎么办 7icloud存储满了怎么办 苹果6icloud满了怎么办 电脑内存槽坏了怎么办 苹果7照片删不了怎么办 屋里太冷怎么办小妙招 天气太热,没空调怎么办 8岁儿童发烧39度怎么办 4岁儿童发烧39度怎么办 6岁儿童发烧39度怎么办 手机被晒得很烫怎么办 子宫肌瘤引起的贫血怎么办 月子没做好腰疼怎么办 狗狗屁股流血水怎么办 狗狗屁股在流血怎么办