[fpc-devel] threadvar vs docs

Martin Frb lazarus at mfriebe.de
Fri Oct 18 15:21:39 CEST 2024


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?


More information about the fpc-devel mailing list