请教如何在两个线程之间通过引用变量来传递数据?

hyacinth531 2007-09-11 12:53:05
各位好:
我编了两个线程程序,一个用来接受数据,另一个用来发送数据.具体说,接受数据的线程收到数据后将该数据运用发送数据线程发送出去.程序如下:
接受数据线程:
"
这是接受数据线程
class ReceiveRobInf : public ArASyncTask
{
protected:
int port;//port use to send message
socklen_t sin_len1;
char message[5];//message[256]; 6.2
int socket_descriptor;
struct sockaddr_in sin;
double myformationtype;

struct ReceiveData
{
char header[4];
double myflag;
};

public:

ReceiveRobInf(int port, double* formationtype);//-----------------(1)
~ReceiveRobInf();
void* runThread(void*);
};
ReceiveRobInf::ReceiveRobInf(int port, double* formationtype):ArASyncTask ()
{//port is port used to receive network information
port=port;

bzero(&sin,sizeof(sin));//use to receive message 6.2
sin.sin_family=AF_INET;
sin.sin_addr.s_addr=htonl(INADDR_ANY);
sin.sin_port=htons(port);
sin_len1=sizeof(sin);
/*create a UDP socket*/
socket_descriptor=socket(AF_INET,SOCK_DGRAM,0);
bind(socket_descriptor,(struct sockaddr*)&sin,sizeof(sin));

myformationtype=*formationtype;


}

void* ReceiveRobInf::runThread(void*)
{
ReceiveData receivedata;

while(this->getRunningWithLock())
{
myMutex.lock();
printf("receive date from other robot!\n");

recvfrom(socket_descriptor,&receivedata,sizeof(receivedata),
0,(struct sockaddr *)&sin,&sin_len1);
//在此处接收数据-------------------------------------------------(2)
myformationtype=receivedata.myflag;
printf("other robot information'flag is %f!\n\n",receivedata.myflag);
myMutex.unlock();
}
"
类似的用发送数据线程
"
SendToR(double* formationflag ,ArRobot*robot, int port , char ip[])

"
主函数像这样调用为:
"
main()
{
..
double formationflag;
ReceiveRobInf receiverobinfor(6789,&formationflag) receiverobinfor.runAsync();

SendToR sendtor(&formationflag, &robot, 6789,"192.168.1.21");
sendtor.runAsync();
..}
"
我的问题是:如何在接收线程的中定义一个变量指向其参数double* formationtype
使得在(2)处当接受线程循环收到数据后改变这个变量,同时formationflag也改变,之后通过这个引用变量将数据传给发送线程.万分感谢!!
...全文
537 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
yangliu817 2007-09-11
  • 打赏
  • 举报
回复
用互斥量来对数据进行操作
hyacinth531 2007-09-11
  • 打赏
  • 举报
回复
已解决:
class ReceiveRobInf : public ArASyncTask
{
protected:
int port;//port use to send message
socklen_t sin_len1;
char message[5];//message[256]; 6.2
int socket_descriptor;
struct sockaddr_in sin;
double &myformationtype; ------------------------------------此处定义引用变量

struct ReceiveData
{
char header[4];
double myflag;
};

public:

ReceiveRobInf(int port, double* formationtype);//-----------------(1)
~ReceiveRobInf();
void* runThread(void*);
};
ReceiveRobInf::ReceiveRobInf(int port, double* formationtype):ArASyncTask ()
,myformationtype(*formationtype)//-------------------------------(2)此处初始化引用变量
{//port is port used to receive network information
port=port;

bzero(&sin,sizeof(sin));//use to receive message 6.2
sin.sin_family=AF_INET;
sin.sin_addr.s_addr=htonl(INADDR_ANY);
sin.sin_port=htons(port);
sin_len1=sizeof(sin);
/*create a UDP socket*/
socket_descriptor=socket(AF_INET,SOCK_DGRAM,0);
bind(socket_descriptor,(struct sockaddr*)&sin,sizeof(sin));
}

void* ReceiveRobInf::runThread(void*)
{
ReceiveData receivedata;

while(this->getRunningWithLock())
{
myMutex.lock();
printf("receive date from other robot!\n");

recvfrom(socket_descriptor,&receivedata,sizeof(receivedata),
0,(struct sockaddr *)&sin,&sin_len1);
//在此处接收数据-------------------------------------------------(2)
myformationtype=receivedata.myflag;//-----------------------(3)直接为成员引用变量附值,该值将同时传给上层参数
printf("other robot information'flag is %f!\n\n",receivedata.myflag);
myMutex.unlock();
}
"
主函数仍然如下:
"
main()
{
..
double formationflag;
ReceiveRobInf receiverobinfor(6789,&formationflag) receiverobinfor.runAsync();

SendToR sendtor(&formationflag, &robot, 6789,"192.168.1.21");
sendtor.runAsync();
..}
"
技巧是在来中定义引用参数,应该如(2)那样初始化,否则,系统提示错误

xugang_2001 2007-09-11
  • 打赏
  • 举报
回复
用全局变量不行么
hyacinth531 2007-09-11
  • 打赏
  • 举报
回复
大家关注关注啊

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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