[fpc-devel] threadvar vs docs
Nikolay Nikolov
nickysn at gmail.com
Fri Oct 18 15:27:56 CEST 2024
On 10/18/24 4:21 PM, Martin Frb via fpc-devel wrote:
> On 18/10/2024 14:30, Michael Van Canneyt via fpc-devel wrote:
>>
>>
>> On Fri, 18 Oct 2024, Nikolay Nikolov via fpc-devel wrote:
>>
>>>
>>> On 10/18/24 12:10 AM, Martin Frb via fpc-devel wrote:
>>>> https://wiki.freepascal.org/Threadvar
>>>>> If threads are used then a copy is made for each thread (including
>>>>> the main thread). Note that the copy is made with the original
>>>>> value of the variable, not with the value of the variable at the
>>>>> time the thread is started.
>>>>
>>>> But what is that original value? Since
>>>> threadvar
>>>> foo: integer = 1;
>>>>
>>>> gives
>>>> Error: Cannot initialize variables declared as threadvar
>>>
>>> I think the documentation is wrong. Threadvars are always
>>> initialized as zero.
>>
>> Then please explain what do copy_all_unit_threadvars and
>> copy_unit_threadvars do ?
>>
>> This is called from InitThreadVars when a new thread is started.
>
> Well, the docu may at least be confusing, or lacking some detail.
>
> If indeed threadvars are always 0, then why copy a value, instead of
> initializing it? (never mind the implementation details, but what it
> will be to the end user).
>
> Or maybe there is somewhere a way for (some?) threadvars to have a
> different (initial) value? And that would presumingly then be, before
> it is even copied to the main thread?
In C (with gcc) it's possible to give them initial value, other than 0:
__thread int i = 5;
But it doesn't seem to be supported by FPC for threadvars. And I don't
know about Delphi. FPC's thread initialization does indeed include code
for copying the initial values, when creating a thread. Maybe it's for
compatibility with thread-aware C libraries (including e.g. glibc),
linked with the program, or for a possible future support for
initialized threadvars in FPC?
Nikolay
More information about the fpc-devel
mailing list