Skip to content

June 3, 2017

Using Semaphores on Android

by Joe Kuan

If you plan to use semaphores on Android, then don’t. This is because the sem_open, sem_close and sem_unlink are not implemented. If you call sem_open on Android, you will get EACCESS error regardlessly.

This is what is defined in the /usr/include/semaphore.h in NDK’s sysroot directory.

int sem_destroy(sem_t*);
int sem_getvalue(sem_t*, int*);
int sem_init(sem_t*, int, unsigned int);
int sem_post(sem_t*);
int sem_timedwait(sem_t*, const struct timespec*);
int sem_trywait(sem_t*);
int sem_wait(sem_t*);

/* These aren't actually implemented. */
sem_t* sem_open(const char*, int, ...);
int sem_close(sem_t*);
int sem_unlink(const char*);


#endif /* _SEMAPHORE_H */

The only workaround is to use mutex and conditional variable. Fortunately, there are many online helps on how to do it. Here is an example.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Note: HTML is allowed. Your email address will never be published.

Subscribe to comments

%d bloggers like this: