线程之双线程进行通信
/*编程实现以下功能:
- 主线程实现以下功能:
定义全局变量key;
创建两个线程;
如果线程正常结束,得到线程的结束状态值,并打印; - 线程一完成以下操作:
设置全局变量key的值为字符串“hello world”;
打印3次字符串“当前线程ID:key值”;
接收到线程二发送的取消请求信号后退出;
结束的时候打印字符串“thread1 ,exited!:key值”; - 线程二完成以下操作:
设置key值为6;
给线程一发送取消请求信号;
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <pthread.h>
#include <sys/syscall.h>
pthread_key_t key;
struct message
{
unsigned long int tp;
};
void cleanup(void *arg)
{
printf("cleanup: %s\n",(char *)arg);
}
void *child1 ()
{
char *Key = "hello world";
char *get_key = NULL;
pthread_setspecific(key, Key);
get_key = (char *)pthrea