最近用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. contiki-进程

    进程的结构 Contiki的进程由两部分组成:进程控制块和进程线程.进程控制块存储在内存中,它包含进程运行时的信息,比如:进程名.进程状态.指向进程线程的指针. 进程线程是存储在ROM中的一个代码块. ...

  2. 尽可能保留原有数据,建立UEFI与BIOS双启PE优盘

    尽可能保留原有数据,建立UEFI与BIOS双启PE优盘1.确保优盘或者移动硬盘有一个FAT32分区,如果没有FAT32分区,就用傲梅分区助手或者ppm转换一个现有的分区到FAT32分区0x0C,或者新 ...

  3. 【nginx】利用yum源安装nginx

    先安装nginx的yum源 http://nginx.org/en/linux_packages.html#stable 找到链接,安装: rpm -ivh http://nginx.org/pack ...

  4. linux hugepage

    The intent of this file is to give a brief summary of hugetlbpage support inthe Linux kernel.  This ...

  5. Nginx 支持 CI 框架的配置并禁止使用 ip 访问

    #CIserver {        listen      80;        server_name www.ci.com;        index       index.php index ...

  6. 第八章 企业项目开发--分布式缓存memcached

    注意:本节代码基于<第七章 企业项目开发--本地缓存guava cache> 1.本地缓存的问题 本地缓存速度一开始高于分布式缓存,但是随着其缓存数量的增加,所占内存越来越大,系统运行内存 ...

  7. Linux设备驱动中的并发控制

    1.并发是指多个执行单元同时.并行的执行.并发的执行单元对共享资源的访问很容易导致竞态. 在 Linux 内核中,主要的竞态发生于如下几种情况: ①对称多处理器(SMP)的多个 CPU ②单CPU内进 ...

  8. jquery mobile 教程

    简介:jQuery Mobile框架可以轻松的帮助我们实现非常好看的.可跨设备的Web应用程序.我们将后续的介绍中向大家介绍大量的代码及实例. jQuery一直以来都是非常流行的富客户端及Web应用程 ...

  9. 微信、QQ浏览器X5内核问题汇总

    一. 资料汇总 1.前端H5调起QQ浏览器的总结:http://km.oa.com/group/22486/articles/show/210189?kmref=search 2.Android We ...

  10. [HB2014 Week5] Allot 人员分配

    这两天决心专门搞好网络流了 - - 题解在什么瞎胡搞跟我说要连n+2和n+1容量为无穷的边…我看了下std才做的… 坑死人的地方就是,需要求多次网络流,每次别忘了把流给清空了…这次是用链表所以专门写了 ...