免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2826 | 回复: 4
打印 上一主题 下一主题

[C] 简单多线程消息队列,无效帖子 [复制链接]

论坛徽章:
3
处女座
日期:2015-03-18 14:35:45羊年新春福章
日期:2015-03-18 14:48:23午马
日期:2015-03-18 14:51:09
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-11-30 13:50 |只看该作者 |倒序浏览
本帖最后由 mr_sev 于 2014-11-30 17:56 编辑

//queue.h

#ifndef __QUEUE_H__
#define __QUEUE_H__

#include <semaphore.h>

typedef struct{       
    pthread_mutex_t lock;
    sem_t  sem;
    int read_off;
    int write_off;
    int msg_size;
    int max_msg;
    char msg[0];
}msg_queue_t;

extern msg_queue_t * CreateMsgQueue(unsigned int msgMax,unsigned int msgSize);

extern int DestoryMsgQueue(msg_queue_t *msgQueue);

extern int SendMsg(msg_queue_t *msgQueue,void *msg);

extern int RecvMsg(msg_queue_t *msgQueue,void *msg,int blocked);
   
#endif

//queue.c
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include "semaphore.h"
#include "queue.h"
msg_queue_t * CreateMsgQueue(unsigned int msgMax,unsigned int msgSize)
{
    msg_queue_t *newQueue;
    newQueue = (msg_queue_t *)malloc(sizeof(msg_queue_t) + msgMax * msgSize);
        if(newQueue)
        {
            newQueue->msg_size=msgSize;
            newQueue->max_msg = msgMax;
            newQueue->read_off = newQueue->write_off = 0;
            pthread_mutex_init(&newQueue->lock,NULL);
            sem_init (&newQueue->sem, 0, 0);
        }
        return newQueue;
}

int  DestoryMsgQueue(msg_queue_t *msgQueue)
{
    int ret = -1;
    if(msgQueue)
    {
            ret = sem_destroy(&msgQueue->sem);
            ret |= pthread_mutex_destroy(&msgQueue->lock);
            free(msgQueue);
    }
        return ret;
}

int SendMsg(msg_queue_t *msgQueue,void *msg)
{
    int ret = -1;
        pthread_mutex_lock(&msgQueue->lock);

        if((msgQueue->write_off + 1) % msgQueue->max_msg == msgQueue->read_off)
        {
                pthread_mutex_unlock(&msgQueue->lock);
        fprintf(stderr,"message queue is full\n");
                return ret;
        }

    memcpy(&msgQueue->msg[msgQueue->write_off * msgQueue->msg_size],msg,msgQueue->msg_size);
        msgQueue->write_off ++;
        if(msgQueue->write_off >= msgQueue->max_msg)
        {
                msgQueue->write_off = 0;
        }
        ret = sem_post (&msgQueue->sem);
        pthread_mutex_unlock(&msgQueue->lock);
        return ret;
}

int RecvMsg(msg_queue_t *msgQueue,void *msg,int blocked)
{
    int ret = 0;
        if(blocked)
        {
                sem_wait(&msgQueue->sem);
        }
        else
        {
                sem_trywait(&msgQueue->sem);
        }
        pthread_mutex_lock (&msgQueue->lock);
        if (msgQueue->read_off != msgQueue->write_off)
        {
            memcpy(msg,msgQueue->msg + msgQueue->read_off * msgQueue->msg_size,msgQueue->msg_size);
            ++ msgQueue->read_off;
            if (msgQueue->read_off >= msgQueue->max_msg)
                msgQueue->read_off = 0;
        }
        else
        {
                pthread_mutex_unlock (&msgQueue->lock);
        fprintf(stderr,"message queue is empty\n");
        printf("line:%d\tread offset:%d\t write offset:%d\n",__LINE__,msgQueue->read_off,msgQueue->write_off);
        ret = -1;
        }       
        pthread_mutex_unlock (&msgQueue->lock);
    return ret;
}

论坛徽章:
3
处女座
日期:2015-03-18 14:35:45羊年新春福章
日期:2015-03-18 14:48:23午马
日期:2015-03-18 14:51:09
2 [报告]
发表于 2014-11-30 13:51 |只看该作者
本帖最后由 mr_sev 于 2014-11-30 15:51 编辑

//demo.c

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
#include "queue.h"

typedef struct{
    pthread_t thread;
    int     rand_number;
}msg_t;

void *product(void *param)
{
    msg_queue_t *msgQueue=(msg_queue_t *)param;
    msg_t msg;
    msg.thread = pthread_self();
    while(1)
    {
        msg.rand_number = rand() % 100;
        if(SendMsg(msgQueue,&msg) < 0)
        {
            sleep(1);
        }
        usleep(1000);
    }
}
void *customer(void *param)
{
    msg_queue_t *msgQueue = (msg_queue_t *)param;
    msg_t msg;
    while(1)
    {
        RecvMsg(msgQueue,&msg,1);
        printf("thread:%u\t rand sum:%d\n",msg.thread,msg.rand_number);
    }
}
int main()
{
    int cnt;
    msg_t msg;
    pthread_t thread1,thread2,thread3,thread4,thread5,thread6,thread7,thread8;
    msg.thread = 0;
    msg_queue_t *msgQueue = NULL;
    if(msgQueue = CreateMsgQueue(10,sizeof(msg_t)) < 0)
    {
        fprintf(stderr,"Queue created failt!\n");
        return -1;
    }
    pthread_create(&thread1,NULL,product,msgQueue);
    pthread_create(&thread2,NULL,product,msgQueue);
    pthread_create(&thread3,NULL,product,msgQueue);
    pthread_create(&thread6,NULL,product,msgQueue);
    pthread_create(&thread7,NULL,product,msgQueue);
    pthread_create(&thread8,NULL,product,msgQueue);
    pthread_create(&thread4,NULL,customer,msgQueue);
    pthread_create(&thread5,NULL,customer,msgQueue);
    pthread_join(thread1,NULL);
    pthread_join(thread2,NULL);
    pthread_join(thread3,NULL);
    pthread_join(thread6,NULL);
    pthread_join(thread7,NULL);
    pthread_join(thread8,NULL);
    pthread_join(thread4,NULL);
    pthread_join(thread5,NULL);
    DestoryMsgQueue(&msgQueue);
    return 0;
}

论坛徽章:
3
处女座
日期:2015-03-18 14:35:45羊年新春福章
日期:2015-03-18 14:48:23午马
日期:2015-03-18 14:51:09
3 [报告]
发表于 2014-11-30 13:55 |只看该作者
本帖最后由 mr_sev 于 2014-11-30 17:54 编辑

mrdong@mrdong-Lenovo-Erazer-Z500:~/project/msg_queue/demo$ ./demo
thread:3075873600         rand sum:83
thread:3067480896         rand sum:86
thread:3059088192         rand sum:77
thread:3050695488         rand sum:15
thread:3042302784         rand sum:93
thread:3033910080         rand sum:35
message queue is full
thread:3075873600         rand sum:83
thread:3067480896         rand sum:86
thread:3059088192         rand sum:77
thread:3050695488         rand sum:15
thread:3050695488         rand sum:49
thread:3050695488         rand sum:49
thread:3075873600         rand sum:59
thread:3059088192         rand sum:63
thread:3050695488         rand sum:26
thread:3042302784         rand sum:90
thread:3067480896         rand sum:40
thread:3067480896         rand sum:68
thread:3075873600         rand sum:11
thread:3042302784         rand sum:26
thread:3075873600         rand sum:11
thread:3075873600         rand sum:11
thread:3067480896         rand sum:82
thread:3059088192         rand sum:30
thread:3050695488         rand sum:29
thread:3042302784         rand sum:67
thread:3075873600         rand sum:62
thread:3059088192         rand sum:29
thread:3075873600         rand sum:67
thread:3075873600         rand sum:67
thread:3042302784         rand sum:23
thread:3075873600         rand sum:67
thread:3042302784         rand sum:22
thread:3059088192         rand sum:67
thread:3067480896         rand sum:58
thread:3050695488         rand sum:69
thread:3075873600         rand sum:93
thread:3042302784         rand sum:11
thread:3050695488         rand sum:73
thread:3050695488         rand sum:73
thread:3075873600         rand sum:56
thread:3050695488         rand sum:73
thread:3042302784         rand sum:19
thread:3067480896         rand sum:84
thread:3050695488         rand sum:37
thread:3075873600         rand sum:21
thread:3059088192         rand sum:98
thread:3042302784         rand sum:70
thread:3042302784         rand sum:70
thread:3042302784         rand sum:70
thread:3075873600         rand sum:24
thread:3042302784         rand sum:70
thread:3075873600         rand sum:91
thread:3067480896         rand sum:80
thread:3050695488         rand sum:56
thread:3059088192         rand sum:73
thread:3042302784         rand sum:62
thread:3075873600         rand sum:70
thread:3050695488         rand sum:81
thread:3042302784         rand sum:25
thread:3067480896         rand sum:96
thread:3042302784         rand sum:25
thread:3075873600         rand sum:84
thread:3059088192         rand sum:36
thread:3050695488         rand sum:27
thread:3042302784         rand sum:46
thread:3067480896         rand sum:5
thread:3075873600         rand sum:29
thread:3050695488         rand sum:13
thread:3042302784         rand sum:95
thread:3042302784         rand sum:95
thread:3042302784         rand sum:95
thread:3075873600         rand sum:82
thread:3050695488         rand sum:45
thread:3042302784         rand sum:34
thread:3059088192         rand sum:14
thread:3067480896         rand sum:67
thread:3075873600         rand sum:64
thread:3050695488         rand sum:43
thread:3067480896         rand sum:87
thread:3067480896         rand sum:87
thread:3067480896         rand sum:87
thread:3075873600         rand sum:76
thread:3067480896         rand sum:88
thread:3042302784         rand sum:84
thread:3050695488         rand sum:78
thread:3059088192         rand sum:3
thread:3075873600         rand sum:51

论坛徽章:
3
处女座
日期:2015-03-18 14:35:45羊年新春福章
日期:2015-03-18 14:48:23午马
日期:2015-03-18 14:51:09
4 [报告]
发表于 2014-11-30 13:57 |只看该作者
本帖最后由 mr_sev 于 2014-11-30 17:55 编辑

@@@@@@@@@@@@@@@@@@

论坛徽章:
3
处女座
日期:2015-03-18 14:35:45羊年新春福章
日期:2015-03-18 14:48:23午马
日期:2015-03-18 14:51:09
5 [报告]
发表于 2014-11-30 14:01 |只看该作者
本帖最后由 mr_sev 于 2014-11-30 17:56 编辑

@@@@@@@@@@@@@@@@@@@@@
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP