简体   繁体   中英

sem_timedwait with Android NDK

During investigation I have found several gaps in Semaphore primitives implementation in NDK. My current task needs sem_timedwait (like sem_wait just with timeout), but I can't find it in NDK. One more important thing: code should be portable on iOS platform.

My current suggestion:

Step 1: Create separate thread ( thrd_id1 ) with sem_wait (wait until sem_post, can stuck there for ever)

Step 2: Create separate thread ( thrd_id2 ) with sleep in seconds and when we are wake

Step 3: When we are wake on ( thrd_id2 ) try to cancel thread with sem_wait ( thrd_id1 ), in case of success return ETIMEOUT , otherwise sem_wait unlocked by sem_post from another thread. However NDK implementation of pthread has no cancellable threads.

Have another idea with pthread_kill , but how can I know was it killed or terminated normally?

Please advise.

sem_timedwait is present in Android NDK at least from version r9d (I don't have earlier versions) - check platforms/android-%yourplatform%/arch-%yourarch%/usr/include/semaphore.h .

If switching to newer NDK is not an option, I would advise you to just take GNU implementation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM