4

In a situation where code is running across multiple android applications, is there a way for synchronization to be maintained/communicated?

This is a question in general, though my specific use case is the simplest of which there is only the need for a single application to acquire the lock at a time, out of N.

CasualT
  • 4,869
  • 1
  • 31
  • 53

1 Answers1

1

To my best knowledge, the framework does not provide such capability. It is quite possible that you can implement it if you dive into NDK, but I don't know the details.

I can think of one very complex scheme involving AIDL and Binder counting, but it is more like a theoretical approach and I wouldn't use it myself (because there is no way to reliably test locking scheme).

I know of two workarounds for this task: file locking and socket locking.

File locking is described here in details.

As far as I know, file locking allows you to lock across processes, but not across threads in the same process. This means that you need to make sure that you don't rely on this mechanism for intra-process multithreading.

Socket locking is described here. I have never used it myself though.

Vasiliy
  • 16,221
  • 11
  • 71
  • 127
  • Thanks for the reply! I have considered both the file and socket based locking approaches, so good to see that others have looked into it in detail. I will take a gander at those. :) – CasualT Aug 10 '17 at 17:30
  • Am also curious to see if anyone else replies with something "clever" ;) (which hopefully doesn't correlate to complicated/brittle, lol) – CasualT Aug 10 '17 at 17:30
  • 1
    @CasualT, I'm also interested to see if there are additional ways. Though in multithreading (and IPC) I would attempt to avoid "clever" solutions. – Vasiliy Aug 10 '17 at 17:44
  • 1
    omgosh, yes, so much. as per my complicated/brittle comment :D – CasualT Aug 10 '17 at 18:18