Is there a pthreads API call that can do something simlar to pthread_join()
but with a timeout? I'm looking for a function that is similar to the Windows WaitForSingleObject(HANDLE handle, int timeout) function. I know that there's a pthread_timedjoin()
call, but that's only available on certain version's of Linux (definitely not in Android).
Asked
Active
Viewed 1,958 times
2

Android Noob
- 3,271
- 4
- 34
- 60
1 Answers
2
No, there's no standard API for a timed pthread_join(), and not one available in the bionic C library used by Android. Though looking at the bionic source code, it'd be very easy for them to implement the pthread_timedjoin_np() API as available in glibc.
What you can do is to modify your thread to signal a pthread_cond_t when it's finished and about to exit. Your other thread that wants to wait on that thread does a pthread_cond_timedwait() on the condition variable.

nos
- 223,662
- 58
- 417
- 506
-
Ive seen 2 solutions on the web: http://pubs.opengroup.org/onlinepubs/000095399/xrat/xsh_chap02.html#tag_03_02_08_21 and http://therealdavebarry.blogspot.com/2006/03/waiting-for-thread-pthreadjoin-with.html The first one seems to create a special type of timed thread. The second one..i'm not really sure. – Android Noob Dec 06 '12 at 21:31
-
I ended up going with the second solution. Only problem is, I'd have to find a replacement for pthread_cancel()... – Android Noob Dec 10 '12 at 18:53
-
You could combine the condition variable solution with `pthread_cleanup_push()` and `pthread_cleanup_pop()` [(see the POSIX manual page)](http://pubs.opengroup.org/onlinepubs/007908799/xsh/pthread_cleanup_push.html); that might let you continue to use `pthread_cancel()`. I know this is an old answer, but it still shows up in search results for "pthread_join timeout". – nitrogen Sep 08 '14 at 02:36