Thread syncronization

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Hi to all
I don't know whether I am missing some documentation or not, about making multithreading apps with b4a. In my Windows C++ experience, to allow access to shared data, we have some objects like CriticalSections, Semaphores etc. Also, the "threading sub" contains an endless loop, exiting from which is obtained by a "thread safe" change of a global variable. I have modified the threading example attached to the library and it seems possible to make, in b4a, a program structure similar to Windows one (i.e. a thread sub with endless loop controlled by a global variable, to stop it). The question is: how to stop/restart a thread to allow to others to safely access shared data? I am investigating this topic by myself, but if anybody has useful informations it will be welcome. Thanks anyway.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
In 99% of the cases you do not need to deal with threads. It is the libraries responsibility to manage long tasks in the background (and then raise an event on the main thread when the task completes). In the case you described you should probably use a Timer.

Look for the Threading library if you want to manually create new threads.
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Thanks for reply. I still have the suspect that things are as you say, but perhaps my situation is more complex. I asyncronously manage a bluetooth device and I work with the AsyncStreams library. Then I have to send these data to Internet, and I also successfully use the HttpUtils stuff. Everything is ok, in ideal conditions, and program perfectly works. The problem is that "event concatenation", in case of errors in communication, may produce situations that will block the entire process. In practice, the bluetooth process must not be influenced by the Internet posting, that may fail, for unpredictable reasons. In case of unsuccessful internet posting, the bluetooth process will and must continue to acquire data. So I decided, hoping to be right, to separate the two processes: the bluetooth process will fill an array of data, and the HttpUtils process will separately Post the data. A global "pointer" will manage the data to send to Internet. From this derives my request on "shared data". Supposing that we have a GlobalData array, a B_Process for bluetooth and an I_Process for internet posting, what I need is: Inside B_Process receive data event, I must "lock" the GlobalData to allow "thread safe pushing" new data in it, and updating the global variable which indicates how many data are still to be sent. Similarly the I_Process should "pop" the data to send and, upon success, update GlobalData and the global pointer. I don't know whether I miss something, as I said, but there is no documentation on the threading library and on the meaning of the Lock, Sleep parameters and on the Threading function. Of course I hope to be wrong. Thanks anyway for your attention.
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Replying to my message, for other's use, if any. In practice my need is to independently send data, stored by an asyncronous process, to Internet. I think that I can resolve this Http post independent cycle, using the JobDone event to make the Internet process to advance till completion of data posting. In other words, a first post will raise the JobDone event, which will call a function that, upon selecting new data (if any) from global array, will issue a new post, which will raise a new JobDone etc. As said, this sequence needs a manual start, if the Post/JobDone sequence ends due to end of data to post. In my case this is possible. If not, much probably the sequence needs to be threated as a thread.
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Always for the benefit of an interested reader, I concluded that the correct architecture for a problem like mine is to use a service for sending data. The main process will push the new data on the global data array, and a service will run in background independently sending data to internet. So, no threading will actually be necessary, according to Erel's initial observation.
 
Upvote 0
Top