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

主函数

#define NUM_THREADS 200     

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <queue.h>
#include <pthread.h>
#include <sys/time.h>
#include <unistd.h>
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 = "";
int i = ;
for( i = ; 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);
} while() {
h = getQueue(g_q);
printf("%s\n", h);
if (strcmp(h, "") == ) {
printf("queue is empty , sleep for a while");
sleep();
} else {
sleep();
}
}
return ;
}

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 <stdio.h>
#include <string.h>
#include <queue.h>
#include <stdlib.h> 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语言多线程队列读写的更多相关文章

  1. 转载~kxcfzyk:Linux C语言多线程库Pthread中条件变量的的正确用法逐步详解

    Linux C语言多线程库Pthread中条件变量的的正确用法逐步详解   多线程c语言linuxsemaphore条件变量 (本文的读者定位是了解Pthread常用多线程API和Pthread互斥锁 ...

  2. android 多线程数据库读写分析与优化

    最新需要给软件做数据库读写方面的优化,之前无论读写,都是用一个 SQLiteOpenHelper.getWriteableDataBase() 来操作数据库,现在需要多线程并发读写,项目用的是2.2的 ...

  3. C语言基础文件读写操作

    整理了一份C语言的文件读写件操作代码,测试时打开相应的注释即可. #include <stdio.h> #include <stdlib.h> #include <uni ...

  4. C 语言多线程与锁机制

    C 语言多线程与锁机制 多线程 #include <pthread.h> void *TrainModelThread(void *id) { ... pthread_exit(NULL) ...

  5. linux下C语言多线程编程实例

    用一个实例.来学习linux下C语言多线程编程实例. 代码目的:通过创建两个线程来实现对一个数的递加.代码: //包含的头文件 #include <pthread.h> #include ...

  6. C语言多线程编程一

    1. Windows下同时打开多个对话框: #include <Windows.h> #include <process.h> //创建线程 void runmsg(void ...

  7. C++多线程队列实现

    C++多线程队列实现 C++多线程队列学习 介绍 在项目中,进行多线程队列实现是一个比较麻烦的事, 找到了一个实现比较好的多线程队列实现, 自己做了一点修改更加适应自己的项目, 记录下来, 有需要的自 ...

  8. C语言文件的读写

    对文件的读和写是最常用的文件操作.在C语言中提供了多种文件读写的函数: 字符读写函数  :fgetc和fputc 字符串读写函数:fgets和fputs 数据块读写函数:freed和fwrite 格式 ...

  9. 如何用Java语言向串口读写数据

    原作者:赛迪网作者 shihuchen ,我在他的基础上进行了部分修改 [赛迪网讯]串口, RS-232-C(又称EIA RS-232-C,以下简称RS232)是在1970年由美国电子工业协会(EIA ...

随机推荐

  1. ios 模拟器不显示系统版本了,后边都是 uuid 了,怎么弄回来?系统升级xcode6.4,模拟器找不到选择了?

    当我用El Capitan Beta 下 Xcode6.4版本时候出现了问题 常用的Scheme 选择版本不见了 而在Xcode 7.0 beta 6中显示有 简直就是坑,经过查资料其实是一个bug ...

  2. 【java基础】方法2

    让形参可变的方法 jdk1.5之后,java允许定义形参长度可变的参数,允许为方法指定数量不确定的形参. package object; public class VariableParam { // ...

  3. (Python)导出指定文件夹中as文件的完全限定类名

    AS3程序在编译的过程中,有一个特点是这样的,不管是项目中的类,还是标准库或者第三方库的类,编译的时候只会把用到的那些类文件编译进去,也就是说,某一些类,只要没有被主程序引用到,那这个文件是不会被编译 ...

  4. csu 1804 有向无环图

    题目地址 分析:从复杂度来看,一定不可能是枚举和来计算.1e5的规模来看,应该是复杂度比较合适. 我是这么想的,对于三个点,假设1->2有a种走法,2->3有b种走法.那么1->3应 ...

  5. 已知GBK的某段码表,码表对应的字符

    for i in range(0xA1A2,0xA1A5):                                                                       ...

  6. iOS开发拓展篇—音频处理(音乐播放器1)

    iOS开发拓展篇—音频处理(音乐播放器1) 说明:该系列文章通过实现一个简单的音乐播放器来介绍音频处理的相关知识点,需要重点注意很多细节的处理. 一.调整项目的结构,导入必要的素材 调整后的项目结构如 ...

  7. Python、PIP环境变量的配置

    Python安装的路径:D:\Python35 pip的环境变量 Python和pip的PATH: PIP下载链接:https://pypi.python.org/pypi/pip 随意解压好,然后C ...

  8. Java中的异常处理

    描述: 如果Java中的函数有可能抛出异常,则该异常要么被catch住,要么在声明函数时必须声明该函数体会throws exception. 处理的时候的流程是,当发生异常时,首先结束当前函数后续语句 ...

  9. 初学DOM树解析xml文件

    做了一次设计模式实验的题目: 某软件公司为新开发的智能手机控制与管理软件提供了一键备份功能,通过该功能可以将原本存储在手机中的通信录.短信.照片.歌曲等资料一次性全部拷贝到移动存储介质(例如MMC卡或 ...

  10. transform:rotate在手机上显示有锯齿的解决方案

    transform:rotate 属于简单好用的效果,但在手机上显示时,会有比较明显锯齿. 解决方案也很简单, 利用外层容器的overflow:hidden 加上图片margin:-1px 就可以解决 ...