B4J Question Critical sections / semaphores

wl

Well-Known Member
Licensed User
Longtime User
Hi,

I understand that server class instances run in their own threads and that a callsubdelayed to a code module will cause the method to be called to be run on the main thread.

However, I want to have as much logic as possible in the Server class (and not in the code module), but for some parts of code - thus inside the server class - (reading an updating a global Map for example) I want it to run inside a critical section/semaphore and avoid multiple server class instances (on different threads) at the same time.

How would I do this: a simple mechanism to avoid a block of code in different server class instances (and thus threads) to run a the same time. I can invent all kinds of suboptimal solutions where a part runs on the main thread and then calls a method back on the Server class instance/thread again, but this is all so cumbersome ...

Eg:

Start Critical section
if not (GlobalMap.ContainsKey(someKey)) then
GlobalMap.Put(someKey, someValue) 'I do not want separate threads adding the same key (which would fail)
end if
End CriticalSection

Thanks
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0
Top