Reputation: 133
What is the proper way to context switch pthreads, from my understanding pthread_yield()
and sched_yield()
respectively are not available/have no effect when using NDK.
EDIT: Is this really all sched_yield()
does?
ftp://sourceware.org/pub/pthreads-win32/sources/pthreads-w32-2-9-1-release/sched_yield.c
EDIT2:(in response to first two comments) Chris: Found this source. Its in ASM, not sure what it does. http://code.google.com/p/android-source-browsing/source/browse/libc/arch-arm/syscalls/sched_yield.S?repo=platform--bionic&r=7582a9c119a4e4d0c306996b2513205889a9fb6c
ENTRY(sched_yield)
mov ip, r7
ldr r7, =__NR_sched_yield
swi #0
mov r7, ip
movs r0, r0
bxpl lr
b __set_syscall_errno
END(sched_yield)
(EDIT2A:Just found this also: https://android.googlesource.com/platform/external/pthreads/+/master/sched_yield.c
uses Sleep (0);
also.)
I'm new to android/linux stuff. Would I compile with the android libs to get the same results on none android linux? Like the 'strace' suggestion, haven't head of it.
fadden: I'm porting a multithread c++ application to android/ndk. It uses multiple threads that need to share the cpu time as they can block each other, so deadlocking is a very real possibility that needs to be prevented(hierarchy, is not a issue, api/system calls doing what they are suppose to is). Using a 'sleep' as a workaround seems like it will give me unpredictable behavior and will be cpu cycle expensive.
Upvotes: 2
Views: 1066
Reputation: 2113
sched_yield() is available with the Android NDK (on all supported platforms). Just include <sched.h> before using it, just like a regular Posix system.
Upvotes: 2