• [多线程学习笔记] 一个线程安全的队列


    我写了一个简单的线程安全的队列,这个队列写入的速度比读取的速度快,我不明白为什么。

    /*************************************************************************
            > File Name: m_queue.c
            > Author: likeyi
            > Mail: likeyiyy@sina.com
            > Created Time: Fri 25 Apr 2014 09:29:10 AM CST
     ************************************************************************/
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <pthread.h>
    #include "m_queue.h"
    queue_t * init_queue()
    {
        queue_t * q = malloc(sizeof(queue_t));
        if(q == NULL)
        {
            DEBUG("alloc queue error");
            return NULL;
        }
        q->head = q->tail = NULL;
        q->length = 0;
        int status = pthread_mutex_init(&q->lock,NULL);
        if(status != 0)
        {
            DEBUG("init mutex error");
            return NULL;
        }
        return q;
    }
    int is_empty(queue_t * q)
    {
        if(q->length == 0)
            return 1;
        return 0;
    }
    int push_to_queue(queue_t * q, void * data)
    {
        pthread_mutex_lock(&q->lock);
        node_t * node = malloc(sizeof(node_t));
        if(node == NULL)
        {
         pthread_mutex_unlock(&q->lock);
    return -1; } node->data = data; node->prev = node->next = NULL; if((q->head == NULL) && (q->tail == NULL)) { q->head = q->tail = node; } else { q->tail->next = node; node->prev = q->tail; q->tail = node; } ++q->length; pthread_mutex_unlock(&q->lock); return 0; } int pop_from_queue(queue_t * q,void ** data) { pthread_mutex_lock(&q->lock); int err = -1; if(q->length != 0) { node_t * node = q->head; q->head = node->next; if(q->head == NULL) { q->tail = NULL; } else { q->head->prev = NULL; } *data = node->data; /* * 数据并不释放,由上层调用,由上层释放。 * */ free(node); node = NULL; --q->length; err = 0; } pthread_mutex_unlock(&q->lock); return err; }
    /*************************************************************************
        > File Name: m_queue.h
        > Author: likeyi
        > Mail: likeyiyy@sina.com 
        > Created Time: Fri 25 Apr 2014 09:29:03 AM CST
     ************************************************************************/
           
    #ifndef MTHREAD_QUEUE_H
    #define MTHREAD_QUEUE_H
    #include <pthread.h>
    #define LIKEYI_DEBUG
    #ifdef LIKEYI_DEBUG
           
    #define DEBUG(format,...) printf("FILE: "__FILE__", LINE: %d: "format"
    ", __LINE__, ##__VA_ARGS__)
           
    #else
    #define DEBUG(format,...)  
    #endif
    typedef struct _node
    {   
        struct _node * prev;
        struct _node * next;
        void * data;
    }node_t;
    typedef struct _queue 
    {   
        node_t * head;
        node_t * tail;
        unsigned long long  length;
        pthread_mutex_t lock;
    }queue_t;
    /*  
    * 不设长度限制.
    * */
    queue_t * init_queue();
    int is_empty(queue_t * q); 
    int push_to_queue(queue_t * q, void * data);
    int pop_from_queue(queue_t * q,void ** data);                                                                                                                                          
           
    #endif
  • 相关阅读:
    ip聚合(百度之星资格赛1003)
    encoding(hdoj1020)
    Candy Sharing Game(hdoj1034)
    you can Solve a Geometry Problem too(hdoj1086)
    Holding Bin-Laden Captive!(hdoj1085)代码并未完全看懂
    Computer Transformation(hdoj 1041)
    Digital Roots(hdoj1013)
    humble number(hd1058)
    FatMouse' Trade(hdoj1009)
    1021 Fibonacci Again (hdoj)
  • 原文地址:https://www.cnblogs.com/likeyiyy/p/3688615.html
Copyright © 2020-2023  润新知