5 pthread_attr_t __thread_attr
;
8 pthread_mutex_t __posix_mutex
;
9 pthread_mutexattr_t __posix_mutexattr
;
11 /// entry point for the spawned thread
12 void* __pthread_launcher(void* p
) {
13 // let the thread be killable under any circumstances
14 // (without this function call, this test always succeeds !)
15 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS
, NULL
);
17 printf("2nd thread is running\n");
20 // this will block this 2nd thread, since we already
21 // locked this mutex by the main thread
22 pthread_mutex_lock(&__posix_mutex
);
24 // here we would access shared resources, etc.
26 // just pro forma, the remainder of this function is actually not of
27 // interest, since this thread is always terminated at its mutex lock call
28 pthread_mutex_unlock(&__posix_mutex
);
33 // initialize mutex and thread attribute
34 pthread_mutexattr_init(&__posix_mutexattr
);
35 pthread_mutex_init(&__posix_mutex
, &__posix_mutexattr
);
36 pthread_attr_init(&__thread_attr
);
38 // already lock the mutex by the main thread ...
39 pthread_mutex_lock(&__posix_mutex
);
43 // create and run a 2nd thread
44 res
= pthread_create(&__thread_id
, &__thread_attr
, __pthread_launcher
, NULL
);
46 printf("thread creation failed\n");
51 // give the other thread a chance to spawn
54 // kill the other thread
55 pthread_cancel(__thread_id
);
56 pthread_detach(__thread_id
);
58 // give the other thread a chance to finish its execution
62 // (just pro forma, doesnt actually matter for this test case)
63 pthread_attr_destroy(&__thread_attr
);
64 pthread_mutex_destroy(&__posix_mutex
);
65 pthread_mutexattr_destroy(&__posix_mutexattr
);
67 printf("TEST PASSED! :-)\n");