Ask Reuben

Threading

Does Genero have threading?

Can Genero start multiple child processes and let me know when they have finished?

A question that gets asked from time to time is can Genero do threading?  I think that question comes from developers who have used Java in the past.

The strict answer to the question is No, the Genero process (fglrun) is single threaded.  A fglrun process has exclusive access to its data structures, you never have to worry about variable values being changed by another thread.

However when questioned as to what it is the developer is trying to achieve when they ask a threading question, the answer is typically that the developer can achieve what they want with a little bit of thinking.

There are some key points that when you grasp you can normally see a solution.

First of all, note the following …

A Genero process can start multiple Genero processes by use of the RUN WITHOUT WAITING syntax

A Genero process can uniquely identify itself in many ways.  These include FGL_GETPID() or to be a bit more robust you might use a UUID.  This unique identifier can be passed to child processes as a command line argument.

To enable communication between Genero processes, you might set up a messaging system.  How you implement it is upto you, wether you use a library accessing database tables, or perhaps a Web Service accessing database tables, possibly in a seperate database.  A Genero process when starting can register itself with this messaging system using its own unique identifier and the unique identifier of its parent Genero process that has been passed to it as a command line argument.  The messaging system now has the information it needs so that a parent process can now communicate with its children, and a child can communicate with its parent.  So if process 123 starts processes 456 and 789, your messaging system would have two pairs in its database.  pid=123 id=456, and pid=123 id=789.  The parent process (123) can now put a message in the message queue of its children (456 and 789), and a child processes (456 or 789) can put a message in the message queue of its parent (123).

So if you wanted a Genero process to start many child sub-processes, process them independently in the background, and do something when they have all finished, you could achieve this by use of such a messaging system in conjunction with RUN WITHOUT WAITING.

These messaging systems normally have methods to :-

  • register
  • deregister
  • send a message
  • get a count of messages for me
  • read a message

Secondly, a Genero process as it is single threaded, what it is doing at any one point in time can be identified.  It is either in a Dialog instruction e.g. MENU, DISPLAY ARRAY, PROMPT, INPUT, INPUT ARRAY, CONSTRUCT, DIALOG statement waiting for the user to do something, or it is processing a 4gl instruction (this might include a database statement, a web service call).  When developers ask for threading they might say something like, start these child processes and when they have finished do X.  However when pressed they don’t want X to occur in the middle of a dialog instruction.  What they really mean is when the user has finished the dialog interaction or there has been some inactivity as measured by ON IDLE.  So with a messaging system in place, this means checking for messages when ready to process any messages i.e. not in the middle of user interaction.

Finally, when we say Genero is single threaded, if you think about it there are two processes running at the same time.  There is the back-end fglrun process, and there is the front-end process (used to be able to say gdc.exe, now it is gbc.js and more), as per the Dynamic User Interface and the Front End View Protocol.  One request behind threading is to be able to display a message or a notification to the user when the background processes have finished.  What you can also now investigate is having the front-end process communicate with the messaging system.  This is effectively what Mobile Push Notifcations are doing.  I haven’t seen anyone implement it but an advanced GBC customisation might be to have a widget (toolbar item, image etc) communicate periodically with a messaging system and display a count of outstanding messages, that is independently of the fglrun process.

A long term Genero customer most likely has a messaging system in place implemented via database tables.  If I was doing one today, I might consider using Web Services with their own seperate database connection, thus avoiding any database transaction issues.

If you think you need threading for a solution to do something the way you did in another language, you probably don’t.  There is most likely a solution available to you by taking a different approach.