[m-users.] Global variables that are local to the thread

Sean Charles (emacstheviking) objitsu at gmail.com
Mon Nov 6 22:20:30 AEDT 2023


I have never used anything but state passing with my Mercury code, sometimes it feels like more work up front but it always pays back in the end.

On that front, I was reading the docs for 'io' and noted that the 'globals' field is now obselete! The comment says:

    % The globals field is obsolete. A mutable declaration will provide
    % the same functionality with better type safety.
    %

But I wasn't sure that that actually means, who provides the mutable and where etc ?



> On 6 Nov 2023, at 10:47, Volker Wysk <post at volker-wysk.de> wrote:
> 
> Am Montag, dem 06.11.2023 um 16:04 +1100 schrieb Julien Fischer:
>> On Sun, 5 Nov 2023, Volker Wysk wrote:
>> 
>>> I'm considering to build a new version of the ODBC library, that's thread-
>>> safe. There's a point which I don't understand yet.
>>> 
>>> The ODBC library has a number of global variables. For instance, the ODBC
>>> environment handle. This makes it thread-unsafe. 
>>> 
>>> Can these variables be made local to the thread, while still being global,
>>> so all foreign procedures in the same thread can access it? Or do I need to
>>> introduce a new argument of some new type odbc_data, which is to be passed
>>> to all predicates of the library? This type would hold all the formerly
>>> global data, but it would be burdensome to pass it around all the time.
>> 
>> Either approach could work. Personally, I would pass the extras
>> infromation to all the predicates and avoid global state at all.
> 
> Then I'll probably go along with you.
> 
>>> 
>>> Maybe use a thread-local mutable variable?
>> 
>> You could use a thread-local mutable; I note that the reference manual
>> doesn't currently talk about how to access the value of a thread-local
>> mutable from C code.  (It's done using the
>> MR_{get,set}_thread_local_mutable macros in runtime/mercury_thread.h.)
> 
> Thanks
> 
> Volker
> _______________________________________________
> users mailing list
> users at lists.mercurylang.org <mailto:users at lists.mercurylang.org>
> https://lists.mercurylang.org/listinfo/users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mercurylang.org/archives/users/attachments/20231106/64bf4a23/attachment.html>


More information about the users mailing list