c语言多线程队列读写

最近用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 = 0;
    for( i = 0; 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(1) {
        h = getQueue(g_q);
        printf("%s\n", h);
        if (strcmp(h, "0") == 0) {
            printf("queue is empty , sleep for a while");
            sleep(3);
        } else {
            sleep(1);
        }
    }
    return 0;
}

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(1);
    }
    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(1);
    }
    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 = "0";
        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指针的返回值 会出问题
    return c;
}

//几点收获  指针需要malloc   普通变量不需要, 特别是字符串数组不需要 
时间: 2024-08-25 04:17:05

c语言多线程队列读写的相关文章

Linux下C语言多线程,网络通信简单聊天程序

原文:Linux下C语言多线程,网络通信简单聊天程序 功能描述:程序应用多线程技术,可是实现1对N进行网络通信聊天.但至今没想出合适的退出机制,除了用Ctr+C.出于演示目的,这里采用UNIX域协议(文件系统套接字),程序分为客户端和服务端.应用select函数来实现异步的读写操作. 先说一下服务端:首先先创建套接字,然后绑定,接下进入一个无限循环,用accept函数,接受“连接”请求,然后调用创建线程函数,创造新的线程,进入下一个循环.这样每当有一个新的“连接”被接受都会创建一个新的线程,实现

Go 语言的分布式读写互斥

Go语言默认的sync.RWMutex实现在多核环境中表现并不佳,因为所有的读者在进行原子增量操作时,会抢占相同的内存地址.该文探讨了一种n-way RWMutex,也可以称为"大读者(big reader)"锁,它可以为每个CPU内核分配独立的RWMutex.读者仅需在其核心中处理读锁,而写者则须依次处理所有锁. 查找当前CPU 读者使用CPUID指令来决定使用何种锁,该指令仅需返回当前活动CPU的APICID,而不需要发出系统调用指令抑或改变运行时.这在Intel或AMD处理器上均

C语言多线程编程

原文:C语言多线程编程 注:本文内容来源于互联网,感谢作者整理! Windows的多线程编程 c语言 在Windows的多线程编程中,创建线程的函数主要有CreateThread和_beginthread(及_beginthreadex). CreateThread 和 ExitThread    使用API函数CreateThread创建线程时,其中的线程函数原型:  DWORD WINAPI ThreadProc(LPVOID lpParameter);在线程函数返回后,其返回值用作调用Ex

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

Linux C语言多线程库Pthread中条件变量的的正确用法逐步详解 多线程c语言linuxsemaphore条件变量 (本文的读者定位是了解Pthread常用多线程API和Pthread互斥锁,但是对条件变量完全不知道或者不完全了解的人群.如果您对这些都没什么概念,可能需要先了解一些基础知识) 关于条件变量典型的实际应用,可以参考非常精简的Linux线程池实现(一)——使用互斥锁和条件变量,但如果对条件变量不熟悉最好先看完本文. Pthread库的条件变量机制的主要API有三个: int p

C语言基础文件读写操作

整理了一份C语言的文件读写件操作代码,测试时打开相应的注释即可. 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <unistd.h> 4 #include <string.h> 5 6 /* 7 * EOF: 符号常量,其值为-1. 8 * fgets() 最多读取int-1个字符,遇换行或EOF即返回. 9 * fputs() 写文件时,忽略字符串截止符'\0'. 10 * fread()和fw

C语言栈队列实现二-十/二-八进制转换

C语言栈队列实现二-十/二-八进制转换 2015-04-05 Lover雪儿 1 //利用栈来求取二进制数的十进制与八进制的结果 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <math.h> 5 6 #define STACK_INIT_SIZE 20 //初始栈大小 7 #define STACK_INCREMENT 10 //扩充栈时每次增加的内存 8 9 typedef char ElemType;

C语言 链队列基本操作

C语言链队列基本操作 #include <stdio.h> #include <stdlib.h> #include <malloc.h> /* C语言链队列基本操作 2014年7月11日10:11:41 */ typedef int qType; typedef struct node { qType data; struct node *pNext; }Node,*pNode; typedef struct queue { pNode front; pNode re

android 多线程数据库读写分析与优化【转】

原文:http://blog.csdn.net/lize1988/article/details/9700723 最新需要给软件做数据库读写方面的优化,之前无论读写,都是用一个 SQLiteOpenHelper.getWriteableDataBase() 来操作数据库,现在需要多线程并发读写,项目用的是2.2的SDK. android 的数据库系统用的是sqlite ,sqlite的每一个数据库其实都是一个.db文件,它的同步锁也就精确到数据库级了,不能跟别的数据库有表锁,行锁. 所以对写实在

[转载]C语言实现二进制文件读写

转载于:https://blog.csdn.net/aresgod/article/details/1852321 我一直觉得二进制文件读写是个很容易的事,所以一直没在意,最近在写一个http客户端,实现文件下载的时候,发现总有问题,后来才发现是忘记写文件用二进制方式,惭愧的很啊. 然后,就在网上搜索了一下,发现通过C语言实现二进制文件读写的资料居然出奇的少,这让我很愤怒,因为虽然这东西很简单,但是对于初学者,往往会需要花很长的时间去弄,一旦明白,又发现花的时间很不值得,罢了,这里通过一个文件拷