I have created a product (UCalc Fast Math Parser, which is featured in April issues of Visual C++ Mag, and Windows Developer Journal) with PB. People often ask if it is “thread-safe”. The way I designed it, it is not thread-safe. The main problem is that I use some global variables. In order for it to be thread-safe, the global variables would have to be local to each thread (but global for the functions within the thread), instead of shared between the threads. My DLL includes direct support for leading compilers, such as VB, VC++, C++ Builder, Delphi, etc... I am aware of TLS, although it seems a little complicated. Looking at the documentation for various compilers, I see that they have their own native ways of handling TLS which is much easier than using things like TlsAlloc, TlsSetValue, etc... I would like to suggest that PowerBASIC include a native feature for supporting TLS as well.
Here is what I’ve found so far, looking at the documentations from other compilers:
In Borland C++ Builder, you can define a thread-local variable like this:
int __thread x;
When the explanation before this said “Sometimes, however, you may want to use variables that are global to all the routines running in your thread, but not shared with other instances of the same thread class.”, I knew that this was exactly what I needed.
Visual C++ supports the following (so does C++ Builder):
__declspec( thread ) int tls_i = 1;
Delphi supports the following syntax:
threadvar X: Integer;
In Visual Basic it’s even easier (the previous version of my DLL component was done in VB). All I had to do was select the type of Instancing I wanted, from a combo-box menu.
So it would be nice to have a built-in way of doing this in a future version of PowerBASIC as well. Meanwhile, how can I make use of Thread Local Variables in PB?
How can I for instance create a trivial DLL with the following function?:
What would the LibMain function have to look like if I wanted the IncrementX function to return a 0 every first time it is called from a new thread (instead of returning a value incremented from another thread)?
-------------
Daniel Corbier
UCalc Fast Math Parser
http://www.ucalc.com
Here is what I’ve found so far, looking at the documentations from other compilers:
In Borland C++ Builder, you can define a thread-local variable like this:
int __thread x;
When the explanation before this said “Sometimes, however, you may want to use variables that are global to all the routines running in your thread, but not shared with other instances of the same thread class.”, I knew that this was exactly what I needed.
Visual C++ supports the following (so does C++ Builder):
__declspec( thread ) int tls_i = 1;
Delphi supports the following syntax:
threadvar X: Integer;
In Visual Basic it’s even easier (the previous version of my DLL component was done in VB). All I had to do was select the type of Instancing I wanted, from a combo-box menu.
So it would be nice to have a built-in way of doing this in a future version of PowerBASIC as well. Meanwhile, how can I make use of Thread Local Variables in PB?
How can I for instance create a trivial DLL with the following function?:
Code:
Global X as Long Function IncrementX Export as Long IncrementX = X X = X+1 End Function
-------------
Daniel Corbier
UCalc Fast Math Parser
http://www.ucalc.com
Comment