[fpc-devel] threadvar vs docs
Sergei Gorelkin
gorelkin at nanoreflex.ru
Sat Oct 19 05:17:58 CEST 2024
18.10.2024 16:21, Martin Frb via fpc-devel пишет:
> 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?
As I remember, FPC programs used to start in single-thread mode, with threadvars behaving as regular
variables. First call to BeginThread switched the RTL into multi-thread mode.
That involved allocating a thread-specific memory block and copying all existing (and already
initialized at that point) threadvars there, using the mentioned procedures. Of course it happened
for already running 'main' thread. Newly created threads got their threadvars initialized to zeroes.
Regards,
Sergei
More information about the fpc-devel
mailing list