Android Question B4A Critical sections / semaphores

Semen Matusovskiy

Well-Known Member
Licensed User
Hi, guys --

I found very old topic https://www.b4x.com/android/forum/threads/threading-library.6775/#content . Does B4A include built-in tools to lock processes ? My app executes concurrent downloads and I need to lock a place, which controls access/refresh tokens

PS. I tried the Lock object from threading library and my own method, using variables. No success. Probably, a reason is relative to CallSub2, Wait For or Sleep. The construction is following. Activity calls a subroutine ("RefreshTokens") inside service (using CallSub2). "RefreshTokens" tries to lock itself at the begining and to unlock, when done.
 
Last edited:

Semen Matusovskiy

Well-Known Member
Licensed User
In my case :

1) In main activity

CallSub3 (ServiceUpload, "subRefreshTokens", ...)
' Then waits (using variables), when subRefreshTokens will be done

2) In ServiceUpload

Sub subRefreshTokens

'<--- Here I need to lock

httpjobHttpJob.PostMultipart ...
Wait For (httpjobHttpJob) JobDone (httpjobHttpJob As HttpJob)

'<-- Here I need to unlock

End Sub

How can I lock/unlock ?
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
Just an idea

- it's bad to fire a lot of jobs against a server (might be seen as an attack)
- how many jobs are we talking about? (10 isn't worth it, 1000 -> why?)
- an Android device is not a server, where are these "many" jobs come from?
- You can pack the data to one job by using a List with maps containing all the data (php or another component will handle it on the server)
- So you just send "one" job with all the data
- I first started to send x jobs, too. I got into troubles so now I'm with lists/maps which is absolutely reliable (maps can contain anything)
 
Upvote 0

Semen Matusovskiy

Well-Known Member
Licensed User
Erel --

Like Windows developer I obviously do not understand how Android processes events/messages.
I did following "lock".

Do While flagBusy = True
Sleep (10)
Loop
flagBusy = True
Log ("Enter")
....

Wait For

....

flagBusy = False
Log ("Exit")

flagBusy is Process_Globals variable. In log file I see "Enter", "Enter", "Exit", "Exit". How Anfroid bypasses similar lock ?

-----------------------------

KMatle --

For example, one request is relative to information, which user sees on the screen, when activity starts. Another to Navigation Drawer, which I will show later. Of course, it's possible to download files step by step. But I want to open drawer asap.
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Last edited:
Upvote 0

Semen Matusovskiy

Well-Known Member
Licensed User
It seems to me, I found the solution. It's necessary to check "busy"-flag inside Activity, not inside the service.

Service:
B4X:
Sub Process_Globals
    Public booleanBusy As Boolean 
End Sub

Sub RefreshTokens  
    '--- Protected ---
    '------------------------     
    booleanBusy = False 
End Sub

In Activity:

B4X:
' Wait ready status
Do While (MyService.booleanBusy)
      Sleep (10)
 Loop
' Occupy and call
 MyService.booleanBusy = True
 CallSub (MyService, "RefreshTokens")
' Wait ready (means Done)
Do While (MyService.booleanBusy)
       Sleep (10)
 Loop

Thanx for replies
 
Last edited:
Upvote 0
Top