简体   繁体   English

sem_timedwait与Android NDK

[英]sem_timedwait with Android NDK

During investigation I have found several gaps in Semaphore primitives implementation in NDK. 在调查过程中,我发现NDK中的信号量原语实现方面存在一些空白。 My current task needs sem_timedwait (like sem_wait just with timeout), but I can't find it in NDK. 我当前的任务需要sem_timedwait (就像sem_wait只是超时),但是我在NDK中找不到它。 One more important thing: code should be portable on iOS platform. 还有一件事很重要:代码应该可以在iOS平台上移植。

My current suggestion: 我目前的建议:

Step 1: Create separate thread ( thrd_id1 ) with sem_wait (wait until sem_post, can stuck there for ever) 第1步:使用sem_wait创建单独的线程( thrd_id1 )(等到sem_post永久停留在那里)

Step 2: Create separate thread ( thrd_id2 ) with sleep in seconds and when we are wake 步骤2:创建单独的线程( thrd_id2 ),并在几秒钟内以及我们醒来时进入睡眠状态

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. 步骤3:当我们唤醒( thrd_id2 )时,尝试使用sem_wait( thrd_id1 )取消线程,如果成功返回ETIMEOUT ,否则sem_wait由另一个线程的sem_post解锁。 However NDK implementation of pthread has no cancellable threads. 但是NDK的pthread实现没有可取消的线程。

Have another idea with pthread_kill , but how can I know was it killed or terminated normally? pthread_kill有另一个想法,但是我怎么知道它被杀死或正常终止了?

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 . sem_timedwait至少存在于r9d版本的Android NDK中(我没有更早的版本)-检查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 . 如果不能切换到较新的NDK,我建议您仅采用GNU实现

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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