linux POSIX 消息队列多个线程间通信 
时间: 2023-05-25 12:03:52 浏览: 55
POSIX消息队列是一种进程内通信机制,它可以使多个线程之间通过消息传递来完成通信。下面是一个简单的示例:
1. 打开消息队列
```
mqd_t mq = mq_open("/my_queue", O_CREAT | O_RDWR, 0666, NULL);
```
2. 发送消息
```
char* message = "Hello, this is a message";
mq_send(mq, message, strlen(message), 0);
```
3. 接收消息
```
char buffer[MAX_SIZE];
unsigned int priority = 0;
ssize_t bytes_received = mq_receive(mq, buffer, MAX_SIZE, &priority);
if (bytes_received > 0) {
buffer[bytes_received] = '\0';
printf("Received message: %s\n", buffer);
}
```
在多个线程之间使用消息队列时,每个线程都可以通过mq_open打开同一个消息队列,然后通过mq_send发送消息,通过mq_receive接收消息。需要注意的是,发送和接收消息的线程可能不在同一个线程中,所以要使用线程同步机制来保证消息传递的顺序。此外,还需要考虑消息队列的并发访问问题,例如使用锁来保证消息队列只被一个线程访问。
相关问题
linux 使用POSIX 消息队列实现线程间通信的例子
以下是一个使用 POSIX 消息队列实现线程间通信的例子:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <mqueue.h>
#define MSG_SIZE 256
#define MAX_MSG 10
mqd_t mqd;
pthread_t tid[2];
pthread_attr_t attr;
// 线程1:发送消息
void *send_func(void *arg) {
char msg[MSG_SIZE];
int i;
for (i = 0; i < MAX_MSG; i++) {
memset(msg, 0, MSG_SIZE);
sprintf(msg, "Message %d from thread 1", i);
if (mq_send(mqd, msg, strlen(msg) + 1, 0) == -1) {
perror("mq_send");
exit(1);
}
printf("Thread 1 sent: %s\n", msg);
sleep(1);
}
}
// 线程2:接收消息
void *recv_func(void *arg) {
char msg[MSG_SIZE];
unsigned int prio;
int i;
for (i = 0; i < MAX_MSG; i++) {
memset(msg, 0, MSG_SIZE);
if (mq_receive(mqd, msg, MSG_SIZE, &prio) == -1) {
perror("mq_receive");
exit(1);
}
printf("Thread 2 received: %s\n", msg);
sleep(1);
}
}
int main() {
struct mq_attr attr;
attr.mq_flags = 0;
attr.mq_maxmsg = 10;
attr.mq_msgsize = MSG_SIZE;
attr.mq_curmsgs = 0;
if ((mqd = mq_open("/test_mq", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &attr)) == -1) {
perror("mq_open");
exit(1);
}
if (pthread_attr_init(&attr) != 0) {
perror("pthread_attr_init");
exit(1);
}
if (pthread_create(&tid[0], &attr, send_func, NULL) != 0) {
perror("pthread_create");
exit(1);
}
if (pthread_create(&tid[1], &attr, recv_func, NULL) != 0) {
perror("pthread_create");
exit(1);
}
if (pthread_join(tid[0], NULL) != 0) {
perror("pthread_join");
exit(1);
}
if (pthread_join(tid[1], NULL) != 0) {
perror("pthread_join");
exit(1);
}
if (mq_close(mqd) == -1) {
perror("mq_close");
exit(1);
}
if (mq_unlink("/test_mq") == -1) {
perror("mq_unlink");
exit(1);
}
return 0;
}
```
该程序创建了一个 POSIX 消息队列 `/test_mq`,其中维护了最大消息数为 10,每条消息为 256 字节。程序启动两个线程,一个用于发送消息,一个用于接收消息,它们都可以同时操作消息队列。发送线程每秒钟向队列中发送一条消息,接收线程每秒钟从队列中接收一条消息并打印出来。程序使用 `pthread_create()` 创建线程,使用 `pthread_join()` 以等待线程完成,使用 `mq_send()` 发送消息,使用 `mq_receive()` 接收消息。最后程序清理了 POSIX 消息队列。
linux posix消息队列例程
下面是一个简单的Linux POSIX消息队列的例程,包括创建、发送、接收和删除消息队列。
```c++
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <mqueue.h>
#include <errno.h>
#define MSG_SIZE 1024
#define QUEUE_NAME "/my_queue"
int main(int argc, char *argv[]) {
mqd_t mq; // 消息队列描述符
char buffer[MSG_SIZE]; // 消息缓冲区
unsigned int prio; // 消息优先级
/* 创建消息队列并打开 */
mq = mq_open(QUEUE_NAME, O_CREAT | O_RDWR, 0666, NULL);
if (mq == (mqd_t) -1) {
perror("mq_open");
exit(1);
}
/* 发送消息 */
strncpy(buffer, "hello, world!", MSG_SIZE);
prio = 10;
if (mq_send(mq, buffer, strlen(buffer) + 1, prio) == -1) {
perror("mq_send");
exit(1);
}
/* 接收消息 */
if (mq_receive(mq, buffer, MSG_SIZE, &prio) == -1) {
perror("mq_receive");
exit(1);
}
printf("Received message: %s (prio: %u)\n", buffer, prio);
/* 删除消息队列 */
if (mq_unlink(QUEUE_NAME) == -1) {
perror("mq_unlink");
exit(1);
}
return 0;
}
```
运行该程序会创建一个名为 "/my\_queue" 的消息队列并发送一条消息 "hello, world!",接着再接收该消息并打印出来,最后删除该消息队列。需要注意的是,该程序并不是一个完整的例程,更多使用方式和 API 可以查看 Linux 的 mq\_open、mq\_send、mq\_receive 和 mq\_unlink 文档。
相关推荐
















