我要用多线程还是文本传递数据?

benbena 2004-06-22 05:04:27
有这样两个模块,完成不同的功能,两者之间要传递一些数据。A做A的事情,但当B有数据发送则接收B的数据处理并发送给B,B同样也是。那我是要做两个程序,用文件传递数据(可以用内存传递吗?)还是把两个模块做到一起互相传递数据,而每个模块起一个线程来完成各自的处理方便?那个好些?那个简单?那个速度快些?
我几乎不懂Linux下C编程,但我在学习,而且在做之前要完成设计,之后我好根据情况来学习来做。谢谢各位!
...全文
97 9 打赏 收藏 转发到动态 举报
写回复
用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
  • 打赏
  • 举报
回复
多线程

23,125

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧