线程间通信

注意编译时需要加-lpthread

​#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>

#define msgkey 1234

pthread_t tid1, tid2;          //定义两个线程全局变量

struct msgbuf {                            //通信所要使用的结构体
 	long mtype;     /* message type, must be > 0 */
	char mtext[100];  /* message data */
};

void *sendpthread(void *arg)          //功能为发送信息的线程
{
	int ret;
	int msqid = *(int *)arg;            //传过来的信号队列需要进行格式转换
	struct msgbuf msg;
	int old;
	pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &old);  //定义取消属性

		while(1)                    //利用信号量进行发送的循环
		{
			memset(&msg, 0, sizeof(msg));
			msg.mtype = 2;
			scanf("%s", msg.mtext);

			ret = msgsnd(msqid, &msg, sizeof(msg.mtext), 0);
			if(ret == -1)
			{
				perror("msgsnd");
				exit(1);
			}
			if(!strcmp(msg.mtext, "bye"))
			{
				pthread_cancel(tid2);    //结束自身线程的同时结束接受线程
				break;
			}
			memset(&msg, 0, sizeof(msg));
		}
}

void *rcvpthread(void *arg)            //功能为接受的线程
{
	int ret;
	int msqid = *(int *)arg;
	struct msgbuf msg;
	int old;
	pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &old);

		while(1)
		{
			memset(&msg, 0, sizeof(msg));

			ret = msgrcv(msqid, &msg, sizeof(msg.mtext), 1, 0);
			if(ret == -1)
			{
				perror("msgrcv");
				exit(1);
			}
			if(!strcmp(msg.mtext, "bye"))
			{
				pthread_cancel(tid1);
				break;
			}
			printf("\t\t%s\n", msg.mtext); //打印接受到的信息
		}
}

int main()
{
	int ret;

	struct msgbuf msg;
	int msqid = msgget(msgkey, 0);    //定义一个信号队列实现通讯

	ret = pthread_create(&tid1, NULL, sendpthread, &msqid);  //创建两个线程 
	if(ret != 0)
	{
		perror("pthread_create");
		exit(1);
	}

	ret = pthread_create(&tid2, NULL, rcvpthread, &msqid);
	if(ret != 0)
	{
		perror("pthread_create");
		exit(1);
	}

	void *status;                      //回收线程
	pthread_join(tid1, &status);
	pthread_join(tid2, &status);

	return 0;
}
​
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>

#define msgkey 1234

pthread_t tid1, tid2;

struct msgbuf {
 	long mtype;     /* message type, must be > 0 */
	char mtext[100];  /* message data */
};

void *sendpthread(void *arg)
{
	int ret;
	int msqid = *(int *)arg;
	struct msgbuf msg;
	int old;
	pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &old);

		while(1)
		{
			memset(&msg, 0, sizeof(msg));
			msg.mtype = 2;
			scanf("%s", msg.mtext);

			ret = msgsnd(msqid, &msg, sizeof(msg.mtext), 0);
			if(ret == -1)
			{
				perror("msgsnd");
				exit(1);
			}
			if(!strcmp(msg.mtext, "bye"))
			{
				pthread_cancel(tid2);
				break;
			}
			memset(&msg, 0, sizeof(msg));
		}
}

void *rcvpthread(void *arg)
{
	int ret;
	int msqid = *(int *)arg;
	struct msgbuf msg;
	int old;
	pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &old);

		while(1)
		{
			memset(&msg, 0, sizeof(msg));

			ret = msgrcv(msqid, &msg, sizeof(msg.mtext), 1, 0);
			if(ret == -1)
			{
				perror("msgrcv");
				exit(1);
			}
			if(!strcmp(msg.mtext, "bye"))
			{
				pthread_cancel(tid1);
				break;
			}
			printf("\t\t%s\n", msg.mtext);
		}
}

int main()
{
	int ret;

	struct msgbuf msg;
	int msqid = msgget(msgkey, 0);

	ret = pthread_create(&tid1, NULL, sendpthread, &msqid);
	if(ret != 0)
	{
		perror("pthread_create");
		exit(1);
	}

	ret = pthread_create(&tid2, NULL, rcvpthread, &msqid);
	if(ret != 0)
	{
		perror("pthread_create");
		exit(1);
	}

	void *status;
	pthread_join(tid1, &status);
	pthread_join(tid2, &status);

	return 0;
}

 

以上两文件运行后,可实现两个线程间的通讯。 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值