arilotta Posted November 13, 2018 Share Posted November 13, 2018 Hi all, I was wondering if it is safe to use procedures not belonging to a class in UniGUI, for example: function IfThenElse(Condizione: Boolean; CondTrue: string; CondFalse: string): string; begin if Condizione then Result := CondTrue else Result := CondFalse; end; In the documentation, it is specified to not use global variables, but nothing is told about global procedures. Thanks for all the replies. Andrea Quote Link to comment Share on other sites More sharing options...
wprins Posted November 13, 2018 Share Posted November 13, 2018 Of course you can use plain procedures or functions, but they key thing you must understand is that UniGUI is of necessity a multi-threaded environment. That means that you need to keep in the back of your mind the question whether there could be concurrency problems when you access any "shared thing" out there. Again, while one can mostly program with a single threaded "typical" mindset (ignoring threading issues) providing you're using things belonging to the "current" session, anything else that is shared must be carefully considered regarding possible concurrency problems. What would happen for example in your procedure above if 2 sessions execute it at exactly the same time? Since all the parameters would be distinct and there's no shared variables anywhere, there's no problem in this case. But if there was (for example) a shared global variable referenced in that procedure, then you'd have to think how multiple threads simultaneously trying to update such a shared resource would behave. You'd likely need a lock or monitor or mutex or something to ensure the threads interoperate correctly on the shared resource. Quote Link to comment Share on other sites More sharing options...
Abaksoft Posted November 13, 2018 Share Posted November 13, 2018 IMHO Simple answer : Not safe Declare your function inside the class. Never declare it alone as we do it in delphi (global function). This is part of my rules of good unigui practises, Quote Link to comment Share on other sites More sharing options...
Volk65 Posted November 14, 2018 Share Posted November 14, 2018 19 hours ago, Abaksoft said: IMHO Simple answer : Not safe Declare your function inside the class. Never declare it alone as we do it in delphi (global function). This is part of my rules of good unigui practises, Then it turns out that all Delphi functions are unsafe: IntToStr, Format, Trim, Len, Copy and 20,000 more functions. Quote Link to comment Share on other sites More sharing options...
Abaksoft Posted November 14, 2018 Share Posted November 14, 2018 33 minutes ago, Volk65 said: Then it turns out that all Delphi functions are unsafe: IntToStr, Format, Trim, Len, Copy and 20,000 more functions. Mmm....?! Quote Link to comment Share on other sites More sharing options...
arilotta Posted November 14, 2018 Author Share Posted November 14, 2018 Hi, thanks to all of you for the replies. In fact I think that Volk65 is right, while Abaksoft is a little too strict. "global" procedures that do not access (in update mode) global variables should be fine. The issues arise when a global variable is set, while the following should be safe in a "global" procedure not belonging to a class: - create/read/set a local object - read/set procedure params - read a global variable What do you think ? Quote Link to comment Share on other sites More sharing options...
Tokay Posted November 14, 2018 Share Posted November 14, 2018 Quote Then it turns out that all Delphi functions are unsafe: IntToStr, Format, Trim, Len, Copy and 20,000 more functions. All this functions are thread safe afaik. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.