Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2024-10

threadvar vs docs

8 messages · 6 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Martin Frb

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

Reply by email

Nikolay Nikolov
to Martin Frb
Martin Frb wrote 12 lines
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.

Nikolay

Reply by email

Michael Van Canneyt
to Nikolay Nikolov
Nikolay Nikolov wrote 17 lines
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.

Michael.

Reply by email

Martin Frb
to Michael Van Canneyt
Michael Van Canneyt wrote 26 lines
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?

Reply by email

Nikolay Nikolov
to Martin Frb
Martin Frb wrote 37 lines
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

Reply by email

gabor
to Nikolay Nikolov

W dniu 2024-10-18 o 15:27, Nikolay Nikolov via fpc-devel pisze:

Show quoted text 13 lines
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

Maybe variables containing managed types, such as dynamic arrays could be a problem in this case?

Michał

Reply by email

Marco van de Voort
to gabor

Op 18-10-2024 om 15:46 schreef gabor via fpc-devel:

Maybe variables containing managed types, such as dynamic arrays could be a problem in this case?

"output" used to be a problem.  The contents (and pointers of the buffer in the textrec got duplicated

Reply by email

Sergei Gorelkin
to Martin Frb: Well, the docu may at least be confusing, or lacking some detail. If indeed threadvars…

18.10.2024 16:21, Martin Frb via fpc-devel пишет:

Show quoted text 32 lines
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

Reply by email