社区
Linux/Unix社区
帖子详情
我要用多线程还是文本传递数据?
benbena
2004-06-22 05:04:27
有这样两个模块,完成不同的功能,两者之间要传递一些数据。A做A的事情,但当B有数据发送则接收B的数据处理并发送给B,B同样也是。那我是要做两个程序,用文件传递数据(可以用内存传递吗?)还是把两个模块做到一起互相传递数据,而每个模块起一个线程来完成各自的处理方便?那个好些?那个简单?那个速度快些?
我几乎不懂Linux下C编程,但我在学习,而且在做之前要完成设计,之后我好根据情况来学习来做。谢谢各位!
...全文
97
9
打赏
收藏
我要用多线程还是文本传递数据?
有这样两个模块,完成不同的功能,两者之间要传递一些数据。A做A的事情,但当B有数据发送则接收B的数据处理并发送给B,B同样也是。那我是要做两个程序,用文件传递数据(可以用内存传递吗?)还是把两个模块做到一起互相传递数据,而每个模块起一个线程来完成各自的处理方便?那个好些?那个简单?那个速度快些? 我几乎不懂Linux下C编程,但我在学习,而且在做之前要完成设计,之后我好根据情况来学习来做。谢谢各位!
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
9 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
lvgame
2004-07-02
打赏
举报
回复
楼上的不错。
多线程是很好的解决方法。
blankman
2004-07-01
打赏
举报
回复
对了,编译 g++ -lpthread thread_test.cpp -o thread_test
blankman
2004-07-01
打赏
举报
回复
用 互斥锁 解决竞争问题,这段代码中两个线程采用相同的函数,你可以替换掉
应该符合你的要求吧,我也是刚学这里,前天刚写的代码,欢迎切磋 :)
blankman@tom.com
QQ:18606366
// thread_test.cpp
#include <curses.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#define DEBUG_PUSH 0
#define DEBUG_POP 0
/* ************************************************************************** */
#define QUEUE_LENGTH 10
class Queue
{
private:
int top, bottom;
int value[QUEUE_LENGTH];
void NextTop(void);
void NextBottom(void);
pthread_mutex_t qMutex;
public:
Queue(void);
int IsEmpty(void);
int IsFull(void);
int Push(int newValue);
int Pop(void);
};
Queue::Queue(void)
{
top=0;
bottom=0;
pthread_mutex_init(&qMutex, NULL);
}
void Queue::NextTop(void)
{
if( !IsFull() )
{
top = top==(QUEUE_LENGTH-1) ? 0:top+1;
}
}
void Queue::NextBottom(void)
{
if( !IsEmpty() )
{
bottom = bottom==(QUEUE_LENGTH-1) ? 0:bottom+1;
}
}
int Queue::IsEmpty(void)
{
return top==bottom;
}
int Queue::IsFull(void)
{
if( bottom==0 )
{
return top==(QUEUE_LENGTH-1);
}
else
{
return top==(bottom-1);
}
}
int Queue::Push(int newValue)
{
if( !IsFull() )
{
if(DEBUG_PUSH) printf("\n【push】 Old top:%d\t", top);
pthread_mutex_lock(&qMutex);
value[top] = newValue;
NextTop();
pthread_mutex_unlock(&qMutex);
if(DEBUG_PUSH) printf("New top:%d\tValue:%d", top, newValue);
return 1;
}
else
{
return 0;
}
}
int Queue::Pop(void)
{
int result=-1;
if( !IsEmpty() )
{
if(DEBUG_POP) printf("\n【pop】 Old bottom:%d\t", bottom);
pthread_mutex_lock(&qMutex);
result = value[bottom];
NextBottom();
pthread_mutex_unlock(&qMutex);
if(DEBUG_POP) printf("New bottom:%d\tValue:%d", bottom, result);
}
return result;
}
void Queue::printtb(void)
{
printf("【Current】 Top:%d\tButtom:%d", top, bottom);
}
/* ************************************************************************** */
void *pop(void *arg)
{
Queue *queue = (Queue *)arg;
static int count = 0;
while(count<200)
{
while( !queue->IsEmpty() )
{
printf("\nValue: %d\tCount:%d", queue->Pop(), ++count);
}
usleep(100);
}
}
void *push(void *arg)
{
Queue *queue = (Queue *)arg;
int value;
for( int i=0;i<100;i++ )
{
while( (value=random())>65535 );
while( !queue->Push(value) ) usleep(10);
}
}
/* ************************************************************************** */
int main(void)
{
char c[20];
int work=15;
int errcode;
Queue *queue1 = new Queue();
pthread_t tpush1;
pthread_t tpush2;
pthread_t tpop;
errcode = pthread_create(&tpush1, NULL, push, (void *)queue1);
if( errcode )
{
printf("\nError when create push thread 1 ...");
goto END;
}
else
{
printf("\nThread push1 is created ...");
}
errcode = pthread_create(&tpush2, NULL, push, (void *)queue1);
if( errcode )
{
printf("\nError when create push thread 2 ...");
goto END;
}
else
{
printf("\nThread push2 is created ...");
}
errcode = pthread_create(&tpop, NULL, pop, (void *)queue1);
if( errcode )
{
printf("\nError when create pop thread ...");
goto END;
}
else
{
printf("\nThread pop is created ...");
}
pthread_join(tpush1, NULL);
pthread_join(tpush2, NULL);
pthread_join(tpop, NULL);
END:
delete queue1;
return 0;
}
blankman
2004-07-01
打赏
举报
回复
建议多线程,最方便,解决竞争用 互斥锁 pthread_mutex_t
很方便,晚上给你贴段代码吧
可以用线程的共享数据,也可以用管道,也可以用共享内存,或者文件
Jerry1986
2004-07-01
打赏
举报
回复
看一下 pthread.h 里面的函数定义,其实可以用指针当作参数传递
leemuxiang
2004-07-01
打赏
举报
回复
用管道最方便了
wxywh
2004-06-23
打赏
举报
回复
用多线程是个好主意。
tibet
2004-06-23
打赏
举报
回复
建议你用多线程做吧
firstyi
2004-06-23
打赏
举报
回复
多线程
易语言启动线程传递多个类型变量参数源码.rar
易语言的启动线程默认是传递一个整数的,要传递多个变量,思路是我们把要传递的变量定义成一个整数型数组,传递的是参数内容的内存地址指针,由于传递的参数可能是
文本
和整数,所以我们要封装两个取
文本
指针地址和...
易语言
多线程
传递
文本
参数两种方法源码-易语言
易语言
多线程
传递
文本
参数两种方法源码
Qt高级系列之8:Qt
多线程
线程池数据同步机制及文件下载器项目实战
1.带领您深入学习QT5/C++:Qt
多线程
线程池及数据同步机制 ,
多线程
文件下载器项目实战,继承自QThread,继承自QObject,QRunnable和QThreadPool,QtConcurrent,Qt
多线程
技术与数据加锁保护机制:QMutex,QSemaphore...
易语言大漠
多线程
启动线程传递参数自定义数据
线程传参自定义数据 • 1、自定义数据类型 • 2、传递自定义数据类型的指针 • 3、把数据内存指针转换为新数据内存指针 • 4、赋给新的自定义数据变量源码: .子程序 子程序1 .参数 a, 整数型 .局部变量 C, ...
Linux/Unix社区
23,125
社区成员
74,509
社区内容
发帖
与我相关
我的任务
Linux/Unix社区
Linux/Unix社区 应用程序开发区
复制链接
扫一扫
分享
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章