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

主函数

  1. #define NUM_THREADS 200
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <queue.h>
  7. #include <pthread.h>
  8. #include <sys/time.h>
  9. #include <unistd.h>
  10. struct threadArgs
  11. {
  12. struct queue *q;
  13. char *c ;
  14. };
  15.  
  16. void* putArg(void *params)
  17. {
  18. struct threadArgs *args = params;
  19. putQueue(args->q, args->c);
  20. }
  21.  
  22. int main()
  23. {
  24. pthread_t tids[NUM_THREADS]; //线程id
  25. struct queue * g_q;
  26. g_q = initQueue();
  27. char c[LENTH] = "test\0";
  28. char b[LENTH] = "btest\0";
  29. char a[LENTH] = "atest\0";
  30. char *h = "";
  31. int i = ;
  32. for( i = ; i < NUM_THREADS; ++i ) {
  33. struct threadArgs *args;
  34. args = (struct threadArgs *)malloc(sizeof(struct threadArgs));
  35. args->q = g_q;
  36. args->c = c;
  37. pthread_create(&tids[i], NULL, putArg, args);
  38. }
  39.  
  40. while() {
  41. h = getQueue(g_q);
  42. printf("%s\n", h);
  43. if (strcmp(h, "") == ) {
  44. printf("queue is empty , sleep for a while");
  45. sleep();
  46. } else {
  47. sleep();
  48. }
  49. }
  50. return ;
  51. }

queue.h

  1. #define LENTH 10240
  2. struct node
  3. {
  4. char * m_content;
  5. struct node * p_next;
  6. };
  7.  
  8. struct queue
  9. {
  10. struct node * p_head;
  11. struct node * p_tail;
  12. };
  13.  
  14. struct queue * initQueue();
  15.  
  16. void putQueue(struct queue *q, char content[LENTH]);
  17. char * getQueue(struct queue *q);
  18. struct node * initNode();

queue.c

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <queue.h>
  4. #include <stdlib.h>
  5.  
  6. struct node * initNode(char c[LENTH]){
  7. struct node *h;
  8. h=(struct node *)malloc(sizeof(struct node));
  9. if (h==NULL) {
  10. printf("can not malloc struct node memory;");
  11. exit();
  12. }
  13. h->m_content = (char * )malloc(sizeof(char)*LENTH);
  14. strcpy(h->m_content, c);
  15. printf("init success \n");
  16. h->p_next = NULL;
  17. return h;
  18. }
  19.  
  20. struct queue * initQueue() {
  21. struct queue * q;
  22. q=(struct queue *)malloc(sizeof(struct queue));
  23. if (q == NULL) {
  24. printf("can not malloc struct node memory;");
  25. exit();
  26. }
  27. q->p_head = NULL;
  28. q->p_tail = NULL;
  29. return q;
  30. };
  31.  
  32. void putQueue(struct queue *q, char c[LENTH]) {
  33. struct node * n;
  34. n = initNode(c);
  35. if (q->p_tail == NULL) { // queue is empty
  36. q->p_head = n;
  37. q->p_tail = n;
  38. } else {
  39. q->p_tail->p_next = n;
  40. q->p_tail = n;
  41. }
  42. printf("put: %s\n", q->p_tail->m_content);
  43. }
  44.  
  45. char * getQueue(struct queue *q) {
  46. char *c;
  47. if (q->p_head==NULL) {
  48. c = "";
  49. return c;
  50. }
  51. struct node * h;
  52. h = q->p_head;
  53. c = h->m_content;
  54. printf("get: %s\n", c);
  55. q->p_head = q->p_head->p_next;
  56. free(h); //这里不能c指针 回收以后c指针的返回值 会出问题
  57. return c;
  58. }
  59.  
  60. //几点收获 指针需要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. 【PHP】基于ThinkPHP框架搭建OAuth2.0服务

    [PHP]基于ThinkPHP框架搭建OAuth2.0服务 http://leyteris.iteye.com/blog/1483403

  2. MySQL默认数据库

    欢迎和大家交流技术相关问题: 邮箱: jiangxinnju@163.com 博客园地址: http://www.cnblogs.com/jiangxinnju GitHub地址: https://g ...

  3. SSH项目Class类的注解与属性的注解

    经过一段日子对SSH的学习,为了有利于随时能熟练的把一个SSH的项目快速的搭建起来,并且在报错的时候,将报错信息和解决 方案记录下来,每天一次的代码练习已成为家常便饭 所以,在反复练习的时候,发现Sp ...

  4. Linux 下安装pip

    安装pip 使用脚本安装和升级pip 要安装或升级pip,需要下载 get-pip.py. 地址:https://bootstrap.pypa.io/get-pip.py 然后运行以下命令 (需要管理 ...

  5. Android基础:Activity

    Activity基本概念 Activity代表活动窗口,通过Context加载页面显示内容,每一个Activity都必须在manifest.xml中注册. 一个应用程序由多个界面构成,回退栈,活动窗口 ...

  6. ListView在列表的头部和底部添加布局——addHeaderView,addFooterView

    addHeaderView()方法:主要是向listView的头部添加布局addFooterView()方法:主要是向listView的底部添加布局 以addHeaderView为例: View he ...

  7. hibernate FetchType理解

    JPA定义实体之间的关系有如下几种: @OneToOne @ManyToOne @OneToMany @ManyToMany 在定义它们的时候可以通过fetch属性指定加载方式,有两个值: Fetch ...

  8. IntelliJ IDEA 使用总结

    IntelliJ IDEA 使用总结 http://my.oschina.net/xianggao/blog/97539 IntelliJ IDEA 问题解决:1.乱码,主要是快捷键的字样显示乱码 中 ...

  9. 通过WebViewJavascriptBridge实现OC与JS交互

      在.m方法当中,申明一个WebViewJavascriptBridge属性: @interface ExampleAppViewController () @property WebViewJav ...

  10. autotools工具使用记录

    参考 http://blog.chinaunix.net/uid-25100840-id-271131.html http://blog.sina.com.cn/s/blog_4c2bf01a0101 ...