xtang wrote:
就是 receive button 的进程用pthread_create()自己创一个线程
啊。这个线程没有别的事情,基本就是:
Code:
/* create a channel */
chid = ChannelCreate(…);
printf(“Channel ID is %d\n”, chid);
for (;
{
rcvid = MsgReceive(…);
/* make sure this is a mesage for receive button */
/* store message in a queue */
}
然后receive button的callback里,只要做:
Code:
if (message_queue_is_not_empty) {
/* get the message */
/* display the message */
}
xtang先生:
我按照您的方法写了如下代码,可运行时,当我点击receive button之后,然后再点击sendbutton按钮,此时,在发送消息的文本框内,可以收到MsgReply回复的消息,可是receive button 接收的消息却不能显示出来。以下是我写的代码,希望您能给我一些指点,帮我找出所存在的问题。
/* Standard headers */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <mqueue.h>
#include <sys/iofunc.h>
#include <sys/dispatch.h>
#include <strings.h>
/* Local headers */
#include “ablibs.h”
#include “abimport.h”
#include “proto.h”
#define Q_FLAGS O_RDWR | O_CREAT
#define Q_PERM S_IRUSR | S_IWUSR | S_IROTH
void *thr_receive_msg( void * );
int
receive_msg( PtWidget_t *widget, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo )
{
pthread_t tid;
pthread_attr_t thr_attr;
struct sched_param thr_param;
char buf[100];
mqd_t qd;
/* eliminate ‘unreferenced’ warnings */
widget = widget, apinfo = apinfo, cbinfo = cbinfo;
bzero ( buf, 100);
pthread_attr_init( &thr_attr );
thr_param.sched_priority = 22;
pthread_attr_setschedparam( &thr_attr, &thr_param );
pthread_attr_setschedpolicy( &thr_attr, SCHED_FIFO );
pthread_create( &tid, &thr_attr, &thr_receive_msg, NULL );
delay ( 500);
qd = mq_open ( “test_queue”, O_RDONLY);
if ( qd == -1 )
{
perror ( “Can not open the test_queue!\n”);
exit ( 1 );
}
else
{
if ( mq_receive ( qd, buf, 100, NULL) > 0)
{
printf ( “The receive msg is: %s\n”, buf );
PtTerminalPuts ( ABW_terminal, buf);
PtTerminalPuts ( ABW_terminal, “\r\n”);
}
mq_close ( qd );
}
return( Pt_CONTINUE );
}
void *thr_receive_msg ( void *a )
{
name_attach_t *attach;
char rmsg[100];
char buf[100];
int rcvid;
mqd_t qd;
struct mq_attr attr;
bzero( buf, 100);
bzero ( rmsg, 100 );
attr.mq_maxmsg = 100;
attr.mq_msgsize = 200;
attach = name_attach ( NULL, “set_text”, 0 );
for ( ; ; )
{
rcvid = MsgReceive ( attach → chid, &rmsg, sizeof ( rmsg ), NULL );
if ( rcvid > 0 )
{
qd = mq_open ( “test_queue”, Q_FLAGS, Q_PERM, &attr);
if ( qd == -1 )
{
perror ( “Can not create the test_queue\n”);
exit ( 1 );
}
else
{
sprintf ( buf, rmsg );
mq_send ( qd, buf, 100, 0 );
strcpy ( rmsg, “Send ok!” );
MsgReply ( rcvid, 1, &rmsg, sizeof ( rmsg));
}
mq_close ( qd );
}
}