#include <signal.h>
#include "messaging.h"
#define THREAD_STARTED JMSG_CUSTOM1
#define THREAD_FAILED JMSG_CUSTOM2
#define THREAD_EXITING JMSG_CUSTOM3
#define THREAD_ACKNOWLEDGE JMSG_CUSTOM4
void sighand( int signum ) {
JMESSAGE msg;
printf("\nSignal Received! Exiting...\n");
msg.message = JMSG_QUIT;
pushmessage(&msg, pthread_self()); /* Put this JMSG_QUIT message on
* the main thread's message queue
* to let the main code know to quit.
*/
return;
}
void *mythread( void *_mainthreadid ) {
pthread_t mainthreadid = (pthread_t)_mainthreadid;
JMESSAGE msg;
signal(SIGINT, &sighand); /* CTRL-C */
if (!createmessagequeue("mythread")) {
printf("main.c > mythread() > createmessagequeue(): Failed.\n");
return 0;
}
/*
* Send a message to the main thread so it can do something when it
* knows we're ready.
*/
msg.message = THREAD_ACKNOWLEDGE;
pushmessage(&msg, mainthreadid);
printf("main.c > mythread(): Launched successfully, using blocking message loop!\n");
do {
waitmessage(&msg); /*
* Wait indefinitely. You can, however, use a
* signal to send a message to this queue to get
* it to move along, or signal it from another thread
* to get it to move along.
*
*/
switch (msg.message) {
case THREAD_ACKNOWLEDGE:
printf("main.c > mythread(): THREAD_ACKNOWLEDGE received from thread \"%s\" (0x%x).\n", msg.fromthreadname, msg.fromthread);
fflush(stdout);
break;
default:
break;
}
} while (msg.message != JMSG_QUIT);
printf("main.c > mythread(): Got JMSG_QUIT.\n");
msg.message = THREAD_EXITING;
pushmessage(&msg, mainthreadid);
printf("main.c > mythread(): Calling destroymessagequeue()!\n");
destroymessagequeue();
printf("main.c > mythread(): Exiting.\n");
return 0;
}
int main( void ) {
JMESSAGE msg;
pthread_t mythreadid;
int ret;
ret = createmessagequeue("Main Thread");
if (!ret) {
printf("main.c > createmessagequeue(): Failed with %d.\n", ret);
return 0;
}
pthread_create(&mythreadid, 0, &mythread, (void *)pthread_self());
printf("main.c > main(): Press [CTRL-C] to terminate program.\n");
do {
/* NON Blocking message queue */
if (popmessage(&msg)) {
switch (msg.message) {
case JMSG_QUIT:
/* Forward the message on to any other queues */
if (pushmessage(&msg, mythreadid)) {
printf("main.c > main(): Received JMSG_QUIT. Forwarded message to mythreadid\n");
fflush(stdout);
pthread_join(mythreadid, 0);
}
break;
case THREAD_ACKNOWLEDGE:
printf("main.c > main(): Received a THREAD_ACKNOWLEDGE from thread \"%s\" (0x%x)\n", msg.fromthreadname, msg.fromthread);
/* Bounce message back for the heck of it */
pushmessage(&msg, mythreadid);
fflush(stdout);
break;
case THREAD_EXITING:
printf("main.c > main(): Received a THREAD_EXITING from thread \"%s\" (0x%x)\n", msg.fromthreadname, msg.fromthread);
fflush(stdout);
break;
default:
break;
}
} else { /* No messages do some important stuff */
usleep(20000); /* Take a breather */
}
} while (msg.message != JMSG_QUIT);
printf("main.c > main(): Calling destroymessagequeue()!\n");
destroymessagequeue();
printf("main.c > main(): Exiting program!\n");
fflush(stdout);
return 0;
}
2条答案
按热度按时间a1o7rhls1#
我还想知道我可以发送数据(显示在代码)到另一个进程,或者我需要声明它作为一个字符数组
是的你可以发送数据到另一个进程
喜欢
如果您了解消息队列,那么消息队列用于进程间通信。
对于多个进程之间的双向通信,还需要多个消息队列
qybjjes12#
这是我写的一个消息队列,我可以将它包含在我的任何多线程程序中。在进程之间,你可以使用sockets或mqueue. h,但是我设计的这个轻量级消息队列库可以创造奇迹。
下面是“messaging.h”头文件。
下面是“messaging.c”源文件:
享受吧!我喜欢它。我在所有的线程中设置了消息循环,它们可以做不同的事情,数据可以轻松快速地传递!
下面是一个使用此代码的工作示例:**确保链接到-lpthread