c语言多线程收发数据,c语言多线程队列读写

c语言多线程收发数据,c语言多线程队列读写最近用 c 语言写了个简单的队列服务 记录一下 文件结构为 main cqueue cqueue h 代码如下 主函数 defineNUM THREADS200 include include include include include include includestruc structqueue q char c void putA

最近用c语言写了个简单的队列服务,记录一下,文件结构为 main.c queue.c queue.h,代码如下:

主函数

#define NUM_THREADS 200

#include

#include

#include

#include

#include

#include

#include

struct threadArgs

{

struct queue *q;

char *c ;

};

void* putArg(void *params)

{

struct threadArgs *args = params;

putQueue(args->q, args->c);

}

int main()

{

pthread_t tids[NUM_THREADS]; //线程id

struct queue * g_q;

g_q = initQueue();

char c[LENTH] = “test\0”;

char b[LENTH] = “btest\0”;

char a[LENTH] = “atest\0”;

char *h = “”;

;

; i < NUM_THREADS; ++i ) {

struct threadArgs *args;

args = (struct threadArgs *)malloc(sizeof(struct threadArgs));

args->q = g_q;

args->c = c;

pthread_create(&tids[i], NULL, putArg, args);

}

) {

h = getQueue(g_q);

printf(“%s\n”, h);

) {

printf(“queue is empty , sleep for a while”);

sleep();

} else {

sleep();

}

}

;

}

queue.h

#define LENTH 10240

struct node

{

char * m_content;

struct node * p_next;

};

struct queue

{

struct node * p_head;

struct node * p_tail;

};

struct queue * initQueue();

void putQueue(struct queue *q, char content[LENTH]);

char * getQueue(struct queue *q);

struct node * initNode();

queue.c

#include

#include

#include

#include

struct node * initNode(char c[LENTH]){

struct node *h;

h=(struct node *)malloc(sizeof(struct node));

if (h==NULL) {

printf(“can not malloc struct node memory;”);

exit();

}

h->m_content = (char * )malloc(sizeof(char)*LENTH);

strcpy(h->m_content, c);

printf(“init success \n”);

h->p_next = NULL;

return h;

}

struct queue * initQueue() {

struct queue * q;

q=(struct queue *)malloc(sizeof(struct queue));

if (q == NULL) {

printf(“can not malloc struct node memory;”);

exit();

}

q->p_head = NULL;

q->p_tail = NULL;

return q;

};

void putQueue(struct queue *q, char c[LENTH]) {

struct node * n;

n = initNode(c);

if (q->p_tail == NULL) { // queue is empty

q->p_head = n;

q->p_tail = n;

} else {

q->p_tail->p_next = n;

q->p_tail = n;

}

printf(“put: %s\n”, q->p_tail->m_content);

}

char * getQueue(struct queue *q) {

char *c;

if (q->p_head==NULL) {

c = “;

return c;

}

struct node * h;

h = q->p_head;

c = h->m_content;

printf(“get: %s\n”, c);

q->p_head = q->p_head->p_next;

free(h); //这里不能c指针 回收以后c指针的返回值 会出问题

return c;

}

//几点收获 指针需要malloc 普通变量不需要, 特别是字符串数组不需要

编译   gcc -o q main.c queue.c -I ./  -lpthread

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/177069.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月26日 下午8:23
下一篇 2026年3月26日 下午8:23


相关推荐

  • java integer范围值的大小_求最大值最小值的代码

    java integer范围值的大小_求最大值最小值的代码java中的Integer.MAX_VALUE和Integer.MIN_VLAUE最近在刷leetcode的题时,才发现有几道题的利用到Integer类型的最大值和最小值,尤其是在判断是否溢出的时候,有道题就非常经典直接判断最后一位,比如最大值231-1的最后一位是7,而最小值-231的最后一位是8,这样进行一个判断8.字符串转换整数(atoi)这道题对我在面试过程中被问到如何判…

    2025年10月4日
    8
  • 使用 multipass 管理 Ubuntu VMs

    使用 multipass 管理 Ubuntu VMsMultipass 是一个轻量级的 ubuntu 虚拟实例管理器 可以很快速的创建一个新的虚拟实例用来做开发测试很方便 Multipass 官网 Github 下载页面下载地址为 github 提供 可能很慢或打不开 可以复制页面上的的下载链接地址后使用下方代理地址尝试通过 Github 代理下载 https ghproxy com https github com CanonicalLtd multipass releases download v1 6 2 multipass 1 6 2 2B

    2026年3月19日
    2
  • Idea激活码永久有效Idea2017.3.6激活码教程-持续更新,一步到位

    Idea激活码永久有效Idea2017.3.6激活码教程-持续更新,一步到位Idea激活码永久有效2017.3.6激活码教程-Windows版永久激活-持续更新,Idea激活码2017.3.6成功激活

    2022年6月17日
    31
  • yeild关键字的理解

    yeild关键字的理解yeild 关键字的理解结合 Pythonyield 使用浅析文章与彻底理解 Python 中的 yield 以后有理解了在补充带有 yield 的函数在 Python 中被称之为 generator 生成器 1 求 Fibonacci 数列 deffab max n a b 0 0 1whilenmax printba b

    2026年3月18日
    2
  • FlashFXP 注册码

    FlashFXP 注册码FlashFXP注册码——–FlashFXPRegistrationDataSTART——–FLASHFXPvACq2ssbvAAAAAC1W7cJKQTzmx77zmqJICvA7d3WnUtWNXdrp8YuERRFdIvXfOPbcpABkVix2aRTgg6afcIKFPxS72XYljdE9tgQD/2r+kmfVBngGM4Qc9p7…

    2022年7月26日
    11
  • 从入门到精通:DeepSeek大模型应用开发实战教程

    从入门到精通:DeepSeek大模型应用开发实战教程

    2026年3月16日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号