1 /********************************************************
2 * An example source module to accompany...
4 * "Using POSIX Threads: Programming with Pthreads"
5 * by Brad nichols, Dick Buttlar, Jackie Farrell
6 * O'Reilly & Associates, Inc.
8 ********************************************************
11 * Example showing macro wrappers for calling non-async
12 * safe routines when the caller has asynchronous
13 * cancellation turned on
20 #include <sys/types.h>
26 #define async_cancel_safe_read(fd,buf,amt) \
29 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldtype); \
30 if (read(fd,buf,amt) < 0) \
31 perror("read"),exit(1); \
32 pthread_setcanceltype(oldtype,NULL); \
33 pthread_testcancel(); \
37 #define async_cancel_safe_write(fd,buf,amt) \
40 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldtype); \
41 if (write(fd,buf,amt) < 0) \
42 perror("write"), exit(1); \
43 pthread_setcanceltype(oldtype,NULL); \
44 pthread_testcancel(); \
53 char buf
[20]="String";
57 async_cancel_safe_write(*fd2
,buf
,amt
);
58 async_cancel_safe_read(*fd2
,buf
,amt
);
63 void *killer(void *arg
)
65 pthread_t
* target
= (pthread_t
*)arg
;
67 pthread_cancel(*target
);
74 pthread_t io_thread
, killer_thread
;
76 // extern void *io(void *);
77 // extern void *killer(void *);
79 if ((fd
= open(".ktemp",O_CREAT
| O_RDWR
, 0666)) < 0)
80 perror("open"), exit(1);
82 pthread_create(&io_thread
,
86 pthread_create(&killer_thread
,
91 pthread_join(io_thread
, NULL
);
93 pthread_join(killer_thread
,NULL
);
96 perror("close"),exit(1);
97 if ((unlink(".ktemp")) < 0)
98 perror("unlink"),exit(1);