c语言多线程收发数据,c语言多线程队列读写
最近用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
c语言多线程收发数据,c语言多线程队列读写相关推荐
- c语言接收串口数据信息,C51通用串口收发数据C语言程序
#include //C51通用串口收发数据C语言程序模块 #define uchar unsigned char #define uint unsigned int uchar shu; bit ...
- 数据操作语言DML及数据定义语言DDL的区别
分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! 数据操作 ...
- SQL中数据操作语言 (DML) 和数据定义语言 (DDL)
可以把 SQL 分为两个部分:数据操作语言 (DML) 和 数据定义语言 (DDL). SQL (结构化查询语言)是用于执行查询的语法.但是 SQL 语言也包含用于更新.插入和删除记录的语法. ...
- r语言 读服务器数据,R语言数据实战 | 安装R语言
原标题:R语言数据实战 | 安装R语言 1.R的获取和安装 获取和安装R很容易(这也是它"亲民"的地方),具体步骤如下: Step 1: 登陆R语言官方网站https://www. ...
- Java实现多线程写数据到文件【队列-文件】
大致流程 先将要写入文件的数据写入到一个队列中,然后单开一个线程专门读取队列数据写入文件,可以保证写入到文件的数据的顺序, 生产者类即多线程写入文件模拟 package com.ldl.test.do ...
- c语言can收发数据原理,汽车车载网络CAN收发器作用和工作原理
汽车车载网络CAN收发器是一种将CAN数据发送器与数据接收器组合在一起的单片集成电路. CAN收发器的 汽车车载网络CAN收发器的作用 汽车车载网络CAN收发器的作用是用于将CAN控制器提供的数据转换 ...
- r语言读取excel数据_R语言 | 更快的表格文件读取方法!
友情提示:蓝色下划线字体为引文,请保持警惕! 使用R语言读取 Affymetrix Human Exon 1.0 ST Array 芯片平台探针注释文件: https://www.affymetrix ...
- python多线程爬虫数据顺序_多线程爬取小说时如何保证章节的顺序
前言 爬取小说时,以每一个章节为一个线程进行爬取,如果不加以控制的话,保存的时候各个章节之间的顺序会乱掉. 当然,这里说的是一本小说保存为单个txt文件,如果以每个章节为一个txt文件,自然不会存在这 ...
- r语言读取excel数据_R语言操纵Excel进行数据透视与批处理
作者:黄天元,复旦大学博士在读,热爱数据科学与开源工具(R),致力于利用数据科学迅速积累行业经验优势和科学知识发现,涉猎内容包括但不限于信息计量.机器学习.数据可视化.应用统计建模.知识图谱等,著有& ...
最新文章
- 千万级智能推荐系统架构演进!
- python源程序执行的方式是什么执行-python调用可执行文件的方法
- python代码示例图形-纯干货:手把手教你用Python做数据可视化(附代码)
- 英语单词 voltage simulation synthesize junction asynchronous mega optimize
- php cms使用视频教程,PHPCMS v9视频模块使用教程二
- 多线程编程之一——问题提出
- 【一步解决】eclipse jee左边的项目栏不见了
- stack java实现_Stack (堆栈)使用JAVA实现
- Java 算法 素数对猜想
- vue openlayer单击地图事件循环多次执行_Vue中$nextTick的理解
- 【OpenCV学习笔记】【函数学习】十(常用的OpenCV函数汇总说明)
- 嵌入式可视化编程软件选哪个好?(可视化编程平台介绍、测评与选择)【Scratch、Mind+、Mixly】
- 处理器架构 (四) ARM指令集
- 条件概率和正则概率(完)
- 苹果电脑上四款好用的可以记事的日历软件
- 应届生程序员如何写好一份简历?
- 牛人用计算机弹歌曲乐谱,牛人乐谱app
- python正则表达式(关于提取数字)
- 使用 redis 连接指定端口的 redis 数据库
- UERANSIM的使用