Home | History | Annotate | Download | only in pthread_attr_setdetachstate
      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_attr_setdetachstate()
      9  *
     10  * If the thread is created detached, then use of the ID of the newly created
     11  * thread by the pthread_detach() or pthread_join() function is an error.
     12  *
     13  * Steps:
     14  * 1.  Initialize a pthread_attr_t object using pthread_attr_init().
     15  * 2.  Using pthread_attr_setdetachstate(), set the detachstate to
     16  *     PTHREAD_CREATE_DETACHED.
     17  * 3.  Create a thread with this attribute.
     18  * 4.  Call pthread_detach() and pthread_join() on this thread, it should give
     19  *     an error.
     20  *
     21  */
     22 
     23 #include <pthread.h>
     24 #include <stdio.h>
     25 #include <errno.h>
     26 #include "posixtest.h"
     27 
     28 void *a_thread_func()
     29 {
     30 
     31 	pthread_exit(0);
     32 	return NULL;
     33 }
     34 
     35 int main(void)
     36 {
     37 	pthread_t new_th;
     38 	pthread_attr_t new_attr;
     39 	int ret_val;
     40 
     41 	/* Initialize attribute */
     42 	if (pthread_attr_init(&new_attr) != 0) {
     43 		perror("Cannot initialize attribute object\n");
     44 		return PTS_UNRESOLVED;
     45 	}
     46 
     47 	/* Set the attribute object to PTHREAD_CREATE_DETACHED. */
     48 	if (pthread_attr_setdetachstate(&new_attr, PTHREAD_CREATE_DETACHED) !=
     49 	    0) {
     50 		perror("Error in pthread_attr_setdetachstate()\n");
     51 		return PTS_UNRESOLVED;
     52 	}
     53 
     54 	/* Create a new thread passing it the new attribute object */
     55 	if (pthread_create(&new_th, &new_attr, a_thread_func, NULL) != 0) {
     56 		perror("Error creating thread\n");
     57 		return PTS_UNRESOLVED;
     58 	}
     59 
     60 	/* If pthread_join() or pthread_detach fail, that means that the
     61 	 * test fails as well. */
     62 	ret_val = pthread_join(new_th, NULL);
     63 
     64 	if (ret_val != EINVAL) {
     65 		printf("Test FAILED\n");
     66 		return PTS_FAIL;
     67 	}
     68 
     69 	ret_val = pthread_detach(new_th);
     70 
     71 	if (ret_val != EINVAL) {
     72 		printf("Test FAILED\n");
     73 		return PTS_FAIL;
     74 	}
     75 
     76 	printf("Test PASSED\n");
     77 	return PTS_PASS;
     78 }
     79