Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Alexandre: One more question about safe vars.
#1
Hi Alexandre,    (all others please feel free to comment too)

In my application, in the many forms it consist of, I use a number of data manipulation functions. Like a functions to return a part of a string after a certain character in the string, or special formatting routines and so on. All small functions that only manipulates what they get, and never hold onto any data.

Over time I have built up quite a collection of functions, and with the ease of adding such a "toolbox" unit to the uses clause, and having all them functions available in the code, I must admit i have being holding on to a lot of those, instead of using the newer Windows equivalents. I have adapted many to use newer windows standards, but still keep the ease of having a function at hand, instead of writing it the windows way directly in code, every time it needed.

Some of these functions I also use in my IW application, and I have added such a unit to the uses clause of a number of the forms in the application.

My question regarding safe vars in this context is: Since these units are formless, and as such not created specifically by the form, how safe are they then ?

And if they are not safe, how do I make them safe ?

I do not want to add every one of the functions I use to the Form Class def. in every form where it is used, if I can avoid it, but will of course if I have to. In that case it would likely be as an .inc file and compiler directive.

I have attached a small example to maybe better understand what I'm referring to, though I'm pretty sure you know very well what I'm talking about.

The attached file should have been LFunks.pas but the extension was not allowed as attachment, so I renamed it LFunks.txt.

Regards
Soren


Attached Files
.txt   LFunks.txt (Size: 2.93 KB / Downloads: 8)
Reply
#2
Global functions are fine.

Its global variables that must be protected in a threaded application.
Reply
#3
Hi Kudzu,

Thanks. I hoped it was so and have not seen any problems, so expected it in a way, but then again, I did not see the problem with mixed search results either, until we had a multi user test, so a bit uncertain. I'm not worried any more. Thanks for the information.

Regards
Soren
Reply
#4
As already pointed out, normal functions and procedures a fine, as long as they only use other functions and local vars, never global vars.

Also you must take care about somethings we take as safe to both read and write in normal single threaded apps, but are not safe to *write* to, in multithreaded apps. One example is FormatSettings. You can use FormatSettings anywhere, but you can't write to it, i.e. change it, unless you are protected.

It is safe, for instance to set FormatSettings *once* in your ServerController.OnCreate or OnConfig events (the later is preferred), but not in other parts of the application.
Reply
#5
Thanks Aleaandre,

I do set FormatSettings but only in UserSession and only once. I'll move it to servercontroller.onconfig.

Regards
Soren
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)