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 * shall detach a thread. It shall indicate to the implementation that storage 11 * for 'thread' can be reclaimed when that thread terminates. 12 * 13 * STEPS: 14 * 1. Create a joinable thread 15 * 2. Detach that thread with pthread_detach() 16 * 3. Try and join the thread to main() using pthread_join() 17 * 4. An error should return from the pthread_join() function saying that the 18 * thread is detched. The test passes. 19 * 5. Else, if pthread_join is successful, the test fails. 20 */ 21 22 #include <pthread.h> 23 #include <stdio.h> 24 #include <errno.h> 25 #include <unistd.h> 26 #include "posixtest.h" 27 28 void *a_thread_func() 29 { 30 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); 31 32 /* If the thread wasn't canceled in 10 seconds, time out */ 33 sleep(10); 34 35 perror("Thread couldn't be canceled (at cleanup time), timing out\n"); 36 pthread_exit(0); 37 return NULL; 38 } 39 40 int main(void) 41 { 42 pthread_attr_t new_attr; 43 pthread_t new_th; 44 int ret; 45 46 /* Initialize attribute */ 47 if (pthread_attr_init(&new_attr) != 0) { 48 perror("Cannot initialize attribute object\n"); 49 return PTS_UNRESOLVED; 50 } 51 52 /* Set the attribute object to be joinable */ 53 if (pthread_attr_setdetachstate(&new_attr, PTHREAD_CREATE_JOINABLE) != 54 0) { 55 perror("Error in pthread_attr_setdetachstate()\n"); 56 return PTS_UNRESOLVED; 57 } 58 59 /* Create the thread */ 60 if (pthread_create(&new_th, &new_attr, a_thread_func, NULL) != 0) { 61 perror("Error creating thread\n"); 62 return PTS_UNRESOLVED; 63 } 64 65 /* Detach the thread. */ 66 if (pthread_detach(new_th) != 0) { 67 printf("Error detaching thread\n"); 68 return PTS_FAIL; 69 } 70 71 /* Now try and join it. This should fail. */ 72 ret = pthread_join(new_th, NULL); 73 74 /* Cleanup: Cancel the thread */ 75 pthread_cancel(new_th); 76 77 if (ret == 0) { 78 printf("Test FAILED\n"); 79 return PTS_FAIL; 80 } else if (ret == EINVAL) { 81 printf("Test PASSED\n"); 82 return PTS_PASS; 83 } else { 84 perror("Error in pthread_join\n"); 85 return PTS_UNRESOLVED; 86 } 87 88 } 89