⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 async_safe.c

📁 linux下多线程程序设计,包括signals,mutex,simple_once
💻 C
字号:
/********************************************************
 * An example source module to accompany...
 *
 * "Using POSIX Threads: Programming with Pthreads"
 *     by Brad nichols, Dick Buttlar, Jackie Farrell
 *     O'Reilly & Associates, Inc.
 *
 ********************************************************
 * async_safe --
 *
 * Example showing macro wrappers for calling non-async
 * safe routines when the caller has asynchronous 
 * cancellation turned on
 */

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>

#include <pthread.h>



#define async_cancel_safe_read(fd,buf,amt) \
{ \
	int oldtype; \
	pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldtype); \
	if (read(fd,buf,amt) < 0) \
		perror("read"),exit(1); \
	pthread_setcanceltype(oldtype,NULL); \
	pthread_testcancel(); \
} 
   

#define async_cancel_safe_write(fd,buf,amt) \
{ \
	int oldtype; \
	pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldtype); \
	if (write(fd,buf,amt) < 0) \
		perror("write"), exit(1); \
	pthread_setcanceltype(oldtype,NULL); \
	pthread_testcancel(); \
}

static int fd;
   
void *io(void *arg)
{
	int *fd=(int *)arg; 
	char buf[20]="String";
	int amt=20;
	
	for (;;) 
	{
		async_cancel_safe_write(*fd,buf,amt);
		async_cancel_safe_read(*fd,buf,amt);
	}
	return(NULL);
}

void *killer(void *arg)
{ 
	pthread_t * target = (pthread_t *)arg;
	int rtn;
	sleep(1);
	pthread_cancel(*target);
	return(NULL);
}

extern int	main(void)
{
	int rtn;
	pthread_t io_thread, killer_thread;   
	pthread_t t1;
	
	
	extern int fd;  
	extern void *io(void *);
	extern void *killer(void  *);
	
	if ((fd = open(".ktemp",O_CREAT | O_RDWR, 0666)) < 0)
		perror("open"), exit(1);
	
	pthread_create(&io_thread, NULL, io, (void *)&fd);
	pthread_create(&killer_thread, NULL, killer, (void *)&io_thread);
	
	pthread_join(io_thread, NULL);
	
	pthread_join(killer_thread,NULL);
	
	if ((close(fd)) < 0)
		perror("close"),exit(1);
	if ((unlink(".ktemp")) < 0)
		perror("unlink"),exit(1);
	
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -