C语言一个线程通过队列向另一个线程实时发送数据的例子求代码实例

具有缓存功能,当写入速度大于读取速度时可以实现缓存。   希望能够提供代码实例!!!!!

写了一个子线程写队列,主线程读队列的简单例子。代码如下,如有帮助,请采纳一下,谢谢。

main.cpp:

#include <pthread.h>
#include <queue>
#include <string>
#include <stdio.h>
#include <unistd.h>
using namespace std;

queue<string> g_qMsg;         //队列
pthread_mutex_t g_mutexWork;  //互斥锁

int g_index = 0;

//子线程写输入
void* DoMyJob(void* pParam)
{
	char buf[100] = {0};
	while(true)
	{
		sprintf(buf,"this is %d",g_index);
		g_index++;
		if (g_index >= 80)
		{
			printf("write 80,stop write\n");
			usleep(10000000);
			break;
		}
		pthread_mutex_lock(&g_mutexWork);
		g_qMsg.push(buf);
		pthread_mutex_unlock(&g_mutexWork);
		usleep(10000); //10毫秒写入一次
	}
	return 0;
}



int main()
{
	pthread_mutex_init(&g_mutexWork,NULL); //初始化线程锁
	//启动线程
	pthread_t threadId;
	pthread_create(&threadId, NULL, DoMyJob, 0);

	string strMsg = "";
	//主线程读数据
	while(1)
	{
		
		if (g_qMsg.size() > 0)
		{
			pthread_mutex_lock(&g_mutexWork);
			strMsg = g_qMsg.front();
			g_qMsg.pop();
			pthread_mutex_unlock(&g_mutexWork);
			printf("main read:%s\n",strMsg.c_str());
		}
		

		usleep(500000);  //500毫秒读取一次
	}

	return 0;

}

Makefile文件(这是一个通用的makefile,你自己make也行):

#编译器LINUX
CXX=g++
#编译动态库
CXXDL= -g -lpthread $(LIB)
COMPILE=-c -g
#编译目标文件
CXXFLAGS=-g $(INCLUDE)  -fPIC
#头文件搜索路径
INCLUDE=-I./ 
#动态库
LIB=-L./

#目标文件
OBJ_FILES=main.o


#可执行文件或者动态库
TARGET=./threadtest

$(TARGET):$(OBJ_FILES)
	@echo "Link the $@ starting..."
	$(CXX) -o $(TARGET) $(OBJ_FILES) $(CXXDL)
	@echo "Link the $@ done."	
$OBJ_FILES:%.o:%.cpp
	$(CXX) $< -o $@ $(COMPILE) -I$./
clean:
	rm $(OBJ_FILES) $(TARGET)

 

windows下的:

#include <process.h>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

#define MAXSIZE 100
typedef int QelemType;
typedef struct{
    QelemType *base;
    int front;
    int rear;
}squene;

squene s;

void Initquene(){
    s.base=(QelemType *)malloc(MAXSIZE * sizeof(QelemType));
    if(!s.base){
        exit(-1);
    }
    s.front=0;
    s.rear=0;
}

void enquene(QelemType e){
    //EnterCriticalSection(&s);
    if((s.rear+1)%MAXSIZE==s.front){
        printf("push error");
        exit(-1);
    }
    s.base[s.rear]=e;
    s.rear=(s.rear+1)%MAXSIZE;
    //LeaveCriticalSection(&s);
    printf("%d push\n",e);
}

int dequene(){
    //EnterCriticalSection(&s);
    if(s.rear==s.front){
        printf("pop error");
        exit(-1);
    }
    QelemType e = s.base[s.front];
    s.front=(s.front+1)  % MAXSIZE;
    //LeaveCriticalSection(&s);
    printf("%d pop\n",e);
    return e;
}
int length(){
    return (s.rear-s.front+MAXSIZE) % MAXSIZE;
}

void threadPush(void *a){
    printf("start threadPush\n");
    int i=0;
    while(1){
        enquene(i);
        Sleep(1000);
        i++;
    }
}
void threadPop(void *a){
    printf("start threadPop\n");
    while(1){
        printf("%d\n",length());
        while(length()!=0){
            dequene();
        }
        Sleep(5000);
    }
    _endthread();
}

int main()
{
    Initquene();
    _beginthread(threadPush,0,NULL);
    _beginthread(threadPop,0,NULL);
    while(1){
    };
    return 0;
}