多线程-读写锁

/*************************************************************************
    > File Name: test.c
    > Author: ndj
    > Mail: 172305913@qq.com
    > Created Time: 2017年08月22日 星期二 10时34分52秒
 ************************************************************************/
//作业请求队列由单个读写锁保护。多个工作线程获取单个主线程分配给他们的作业
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>

struct job
{
    struct job *j_next;
    struct job *j_prev;
    pthread_t j_id;//tells which thread handles this job
    /*more stuff here*/
    char *j_str;
};
//工作队列
struct queue
{
    struct job *q_head;
    struct job *q_tail;
    pthread_rwlock_t q_lock;
};

struct queue job_queue;//工作队列, 全局变量。所以线程均可以访问
/*
 * Initialize a queue
 */
 int queue_init(struct queue *qp)
{
    int err;

    qp->q_head = NULL;
    qp->q_tail = NULL;
    err = pthread_rwlock_init(&qp->q_lock, NULL);//读写锁初始化
    if(err != 0)
        return err;
    /*continue initialization*/
    return 0;
}

/*
 * Inser a job at the head of the 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 the queue
 */
void job_append(struct queue *qp, struct job *jp)
{
    pthread_rwlock_wrlock(&qp->q_lock);//给工作队列加锁
    //qp->q_tail->j_next = jp;
    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);//给工作队列解锁
}

/*
 * Remove the given job from a queue
 */ 
void job_remove(struct queue *qp, struct job *jp)
{
    pthread_rwlock_wrlock(&qp->q_lock);//给工作队列加写锁
    if(jp == qp->q_head)//删除的工作是队头
    {
        qp->q_head = jp->j_next;
        if(qp->q_tail == jp)
            qp->q_tail = NULL;
        else
            jp->j_next->j_prev = jp->j_prev;
    }
    else if(jp == qp->q_tail)//删除队尾
    {
        qp->q_tail = jp->j_prev;
        qp->q_tail->j_next = jp->j_next;
    }
    else
    {
        jp->j_prev->j_next = jp->j_next;
        jp->j_next->j_prev = jp->j_prev;
    }
    pthread_rwlock_unlock(&qp->q_lock);//给工作队列解锁
}

/*
 *Find a job for the given thread ID
 */
struct job* job_find(struct queue *qp, pthread_t  id)
{
    struct job *jp;
    if(pthread_rwlock_rdlock(&qp->q_lock) != 0)
        return (NULL);//加读锁没有成功 

    for(jp = qp->q_head; jp != NULL; jp = jp->j_next)
        if(pthread_equal(id, jp->j_id))
            break;
    pthread_rwlock_unlock(&qp->q_lock);//解锁
    return (jp);
}
/*
 * 线程1函数
 */ 
void* thrd_func1(void *arg)
{
    //从队列中获取任务1
    struct job *jp;

    if((jp = job_find(&job_queue, 1)) != NULL)
        printf("thread 1 get job 1 success\n");
    else
        printf("thread 1 get job 1 failed\n");
    job_remove(&job_queue, jp);
    printf("thread 1: %s\n", jp->j_str);
    pthread_exit(NULL);
}

/*
 * 线程2函数
 */
void* thrd_func2(void *arg)
{
    //从队列中获取任务2
    struct job *jp;
    if((jp = job_find(&job_queue, 2)) != NULL)
        printf("thread 2 get job 2 success\n");
    else
        printf("thread 2 get job 2 failed\n");
    job_remove(&job_queue, jp);
    printf("thread 2: %s\n", jp->j_str);
    pthread_exit(NULL);
}

/*
 * 线程3函数
 */

void* thrd_func3(void* arg)
{
    //从队列中获取 任务3
    struct job *jp;
    if((jp = job_find(&job_queue, 3)) != NULL)
        printf("thread 3 get job 3 success\n");
    else
        printf("thread 3 get job 3 failed\n");
    job_remove(&job_queue, jp);
    printf("thread 3: %s\n", jp->j_str);
    pthread_exit(NULL);
}

int main(void)
{
    int err;
    //struct queue job_queue;//工作队列
    pthread_t thread[3];//线程ID
    struct job job[4] = {
        {NULL, NULL, 1, "job 1"},
        {NULL, NULL, 2, "job 2"},
        {NULL, NULL, 3, "job 3"},
        {NULL, NULL, 4, "job 4"}
    };
    int i;
    /*工作队列初始化*/
    if(queue_init(&job_queue) != 0)
        printf("job queue  init failed\n");
    else
        printf("job queue init success\n");

    for(i=0; i<4; i++)
    {
        job_insert(&job_queue, &job[i]);//向队列这添加任务
    }

    err = pthread_create(&thread[0], NULL, thrd_func1, NULL);//创建线程1
    if(err != 0)
        printf("thread 1 create failed\n");
    else
        printf("thread 1 create success\n");

    err = pthread_create(&thread[1], NULL, thrd_func2,NULL);//创建线程2
    if(err != 0)
        printf("thread 2 create failed\n");
    else
        printf("thread 2 create success\n");

    err = pthread_create(&thread[2], NULL, thrd_func3, NULL);//创建线程3
    if(err != 0)
        printf("thread 3 create failed\n");
    else
        printf("thread  3 create success\n");

    for(i=0; i<3; i++)
    {
        err = pthread_join(thread[i], NULL);
        if(err != 0)
            printf("thread %d can't join\n", i);
        else
            printf("thread %d join\n", i);
    }
    exit(0);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值