1 /* 2 * Copyright (c) 2002, Intel Corporation. All rights reserved. 3 * Created by: rolla.n.selbak REMOVE-THIS AT intel DOT com 4 * This file is licensed under the GPL license. For the full content 5 * of this license, see the COPYING file at the top level of this 6 * source tree. 7 8 * Test that pthread_detach() 9 * 10 * If 'thread' has not terminated, pthread_detach() shall not cause it to 11 * terminate. The effect of multiple pthread_detach() calls on the same 12 * 13 * STEPS: 14 * 1.Create a joinable thread 15 * 2.Detach that thread 16 * 3.Verify that the thread did not terminate because of this 17 * 18 */ 19 20 #include <pthread.h> 21 #include <stdio.h> 22 #include <errno.h> 23 #include <unistd.h> 24 #include "posixtest.h" 25 26 void *a_thread_func() 27 { 28 29 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); 30 31 /* If the thread wasn't canceled in 10 seconds, time out */ 32 sleep(10); 33 34 perror("Thread couldn't be canceled (at cleanup time), timing out\n"); 35 pthread_exit(0); 36 return NULL; 37 } 38 39 int main(void) 40 { 41 pthread_attr_t new_attr; 42 pthread_t new_th; 43 int ret; 44 45 /* Initialize attribute */ 46 if (pthread_attr_init(&new_attr) != 0) { 47 perror("Cannot initialize attribute object\n"); 48 return PTS_UNRESOLVED; 49 } 50 51 /* Set the attribute object to be joinable */ 52 if (pthread_attr_setdetachstate(&new_attr, PTHREAD_CREATE_JOINABLE) != 53 0) { 54 perror("Error in pthread_attr_setdetachstate()\n"); 55 return PTS_UNRESOLVED; 56 } 57 58 /* Create the thread */ 59 if (pthread_create(&new_th, &new_attr, a_thread_func, NULL) != 0) { 60 perror("Error creating thread\n"); 61 return PTS_UNRESOLVED; 62 } 63 64 /* Detach the thread. */ 65 if (pthread_detach(new_th) != 0) { 66 printf("Error detaching thread\n"); 67 return PTS_FAIL; 68 } 69 70 /* Verify that it hasn't terminated the thread */ 71 ret = pthread_cancel(new_th); 72 73 if (ret != 0) { 74 if (ret == ESRCH) { 75 printf("Test FAILED\n"); 76 return PTS_FAIL; 77 } 78 perror("Error canceling thread\n"); 79 return PTS_UNRESOLVED; 80 } 81 82 printf("Test PASSED\n"); 83 return PTS_PASS; 84 } 85