Recent

Author Topic: Thread Variables  (Read 16264 times)

sam707

  • Guest
Re: Thread Variables
« Reply #15 on: June 21, 2016, 08:53:05 am »
yeah "mostly", sumtimes.. NOT ALWAYS in he main thread

type
  TMyWorkThread = class(TThread)
 ....
  end;
  TCalcThread = class(TThread)
  ...
  public
   procedure Prepare;
  ...
  end;

var
  HeavyCalcUserThread: array[1..4] of TCalcThread;

procedure TMyworkThread.Execute;
var i: integer;
begin
  for i:=1 to 4 do begin
    HeavyCalcUserThread.Create(true);
    HeavyCalcUserThread.Prepare;
    HeavCalcUserThread.Suspended:=False;
  end;
end;

in such cases threads are NOT created nor prepared in the main Thread, so... constructors arn't necessary called from the main thread, and "SOMETIMES!" they'd better to NOT be (it spares time for GUI)...

threadvar modifier is NOT part of TThread class... simply because private and protected areas of the TThread class are OFTEN ENOUGH to make variables local to the Thread instance EVEN IF they lie in the global memory heap
« Last Edit: June 21, 2016, 09:22:17 am by sam707 »

HeavyUser

  • Sr. Member
  • ****
  • Posts: 397
Re: Thread Variables
« Reply #16 on: June 21, 2016, 09:26:19 am »
yeah "mostly", sumtimes.. NOT ALWAYS in he main thread

type
  TMyWorkThread = class(TThread)
 ....
  end;
  TCalcThread = class(TThread)
  ...
  public
   procedure Prepare;
  ...
  end;

var
  HeavyCalcUserThread: array[1..4] of TCalcThread;

procedure TMyworkThread.Execute;
var i: integer;
begin
  for i:=1 to 4 do begin
    HeavyCalcUserThread.Create(true);
    HeavyCalcUserThread.Prepare;
    HeavCalcUserThread.Suspended:=False;
  end;
end;

in such cases threads are NOT created nor prepared in the main Thread, so... constructors arn't necessary called from the main thread, and "SOMETIMES!" they'd better to NOT be (it spares time for GUI)...

threadvar modifier is NOT part of TThread class... simply because private and protected areas of the TThread class are OFTEN ENOUGH to make variables local to the Thread instance EVEN IF they lie in the global memory heap
yeah but then again it is not executed in the intended thread only by the calling thread so always out of context.

sam707

  • Guest
Re: Thread Variables
« Reply #17 on: June 21, 2016, 09:31:25 am »
yeah but then again it is not executed in the intended thread only by the calling thread so always out of context.

for sure! it's better to initialize a thread from outside that thread before running. Its logical

HeavyUser

  • Sr. Member
  • ****
  • Posts: 397
Re: Thread Variables
« Reply #18 on: June 21, 2016, 11:19:57 am »
yeah but then again it is not executed in the intended thread only by the calling thread so always out of context.

for sure! it's better to initialize a thread from outside that thread before running. Its logical
No!
threadvars can only be initialized from with in the thread, before the thread creation they do not exists as variables yet. Think of them as local variables in a procedure it is not logical to be able to initialize them from outside the procedure.
As for other types of variables it is logical to be able to set the initial values that does not initialize the thread variables though only provides some values the same way the procedure parameters do not initialize procedure variables.

As for Logical or not, well my experience suggests that logic is a very personal thing and can only be shared if both parties have the same experiences and share the same interpretation of events in all other cases logic becomes opinion.


User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Thread Variables
« Reply #19 on: June 21, 2016, 01:30:04 pm »
during the execution the threadvar that they touch is the one used by the main thread not the newly created thread's data. Use the execute method anything else is not guarantied to execute in the thread context, well actually unless you call it directly from the thread's execute method it is guarantied to not execute in the thread context but you get the point.
So you are referring to just time efficiency like i said? There is no other harm done in doing that in main thread. Actually i can see opposite cases where initializing thread variables in main thread is the only working way. Say you want to split the load of going through some data that changes at realtime. The way i see it, you would have to give the partial data snapshot to each thread which they will then begin working on. If you read the snapshot in Execute you would get overlaps and different threads would get access to same data at random places.

There are thread variables that you put in the public section of inherited TThread class in type section. Or some that are passed in through a constructor.
« Last Edit: June 21, 2016, 01:33:58 pm by User137 »

HeavyUser

  • Sr. Member
  • ****
  • Posts: 397
Re: Thread Variables
« Reply #20 on: June 21, 2016, 07:01:52 pm »
during the execution the threadvar that they touch is the one used by the main thread not the newly created thread's data. Use the execute method anything else is not guarantied to execute in the thread context, well actually unless you call it directly from the thread's execute method it is guarantied to not execute in the thread context but you get the point.
So you are referring to just time efficiency like i said?
sorry for me time efficiency is something different that has nothing to do with initialization. If you mean synchronization ee the right command at the correct time then yes that is what it is required.
There is no other harm done in doing that in main thread.
Harm? if proper care is taken it does not matter from where is done.
Actually i can see opposite cases where initializing thread variables in main thread is the only working way.
thread variables and threadvars are two different things one exist before the thread execution begins the other it does not, one can be accessed from outside the thread context the other can not, in any case it is wise to not mix thread data with application data at all if possible. 
Say you want to split the load of going through some data that changes at realtime. The way i see it, you would have to give the partial data snapshot to each thread which they will then begin working on. If you read the snapshot in Execute you would get overlaps and different threads would get access to same data at random places.
No you can't, if proper locking is used then it should not be possible for the threads to overlap, on the contrary, by introducing a thread safe list, that any number of threads can query for data, you have a more dynamic design, both on number of threads and size of data acquired from the thread in each iteration. Pushing data is not a recommended strategy in multi threading. 
There are thread variables that you put in the public section of inherited TThread class in type section.
No there are none, public variables in any class are an indicator of bad design, either the designer or the programmer needs to be fired, as always there are exceptions, I have never seen one but that is probably my narrow view. ALL thread variables must be protected against outside access. No public variables no protected variables no direct access to internal variables from outside the thread context with out a proper locking and no access to workload variables from outside the thread code. Only variables that are used to initialize the process or return information/data to the application are allowed to be accessed from outside and those are not to be used directly in the thread execution process.
Or some that are passed in through a constructor.
Yes this can be seen as the parameters in a procedure, I prefer a public method that will initialize those variables, instead of constructor parameters, it is more "usable" but both are acceptable solutions to the problem.
« Last Edit: June 21, 2016, 07:04:37 pm by HeavyUser »

Blestan

  • Sr. Member
  • ****
  • Posts: 461
Re: Thread Variables
« Reply #21 on: June 21, 2016, 11:36:20 pm »
im very disapointed that everybody is talking about tthtead class... thread vars has nothing to do with it but is all about tls registers...
Speak postscript or die!
Translate to pdf and live!

BeniBela

  • Hero Member
  • *****
  • Posts: 959
    • homepage
Re: Thread Variables
« Reply #22 on: June 21, 2016, 11:57:39 pm »
im very disapointed that everybody is talking about tthtead class... thread vars has nothing to do with it but is all about tls registers...

https://github.com/BeRo1985/pasmp/blob/master/src/PasMP.pas#L204-L206

avoid those?

sam707

  • Guest
Re: Thread Variables
« Reply #23 on: June 22, 2016, 12:13:32 pm »
yeah but then again it is not executed in the intended thread only by the calling thread so always out of context.

for sure! it's better to initialize a thread from outside that thread before running. Its logical
No!
threadvars can only be initialized from with in the thread, before the thread creation they do not exists as variables yet. Think of them as local variables in a procedure it is not logical to be able to initialize them from outside the procedure.
As for other types of variables it is logical to be able to set the initial values that does not initialize the thread variables though only provides some values the same way the procedure parameters do not initialize procedure variables.

As for Logical or not, well my experience suggests that logic is a very personal thing and can only be shared if both parties have the same experiences and share the same interpretation of events in all other cases logic becomes opinion.

NO AND NO AND DEF NO!
threadvar declaration can be used ANYWHERE at the programmer purpose and needs, thread ars are COPIES of original GLOBALS that are propagated along threads contextes when they are started

IT IS BETTER TO INITIALIZE STUFFS BEFORE AND FROM OUTSIDE a Worker Thread, its just like considering refueling coal to a boiler, you get THAT coal (representing data to conpute) FROM a factory OUTSIDE the boiler (representing the thread) BEFORE you light the fire

IM GLAD THAT I WILL HOPEFULLY NEVER WORK WITH YOU @HeavyUser, YOU SEEMS DAMN LOST on BAD HABITS forever

@User137 YES YOU GOT IT RIGHT! THANKS
« Last Edit: June 22, 2016, 12:23:32 pm by sam707 »

Thaddy

  • Hero Member
  • *****
  • Posts: 19273
  • Glad to be alive.
Re: Thread Variables
« Reply #24 on: June 22, 2016, 12:22:54 pm »
NO AND NO AND DEF NO!
threadvar declaration can be used ANYWHERE at the programmer purpose and needs, thread vars are COPIES of original GLOBALS that are propagated among threads when they are started
No, they aren't. threadvar a in thread 1 is different from threadvar a in thread 2.
Quote
IT IS BETTER TO INITIALIZE STUFFS BEFORE AND FROM OUTSIDE a Worker Thread, its just like considering refueling coal to a boiler, you get THAT coal (representing data to conpute) FROM OUTSIDE the boiler (representing the thread)
No. It is even impossible to do it properly with normal pascal constructs for the same reason as the above.
Threadvars should be initialized in the context of its own threads.

What's all that kind of shouting and nonsense. It is about threadvars. These are only valid for the thread and can only be initialized for any normal intend and purpose IN the thread itself.
Stop shouting and learn something about thread programming.

Threadvars are thread local. They are only a copy in the naming....
« Last Edit: June 22, 2016, 12:26:01 pm by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

sam707

  • Guest
Re: Thread Variables
« Reply #25 on: June 22, 2016, 12:25:09 pm »
NO AND NO AND DEF NO!
threadvar declaration can be used ANYWHERE at the programmer purpose and needs, thread vars are COPIES of original GLOBALS that are propagated among threads when they are started
No, they aren't. threadvar a in thread 1 is different from threadvar a in thread 2.
Quote
IT IS BETTER TO INITIALIZE STUFFS BEFORE AND FROM OUTSIDE a Worker Thread, its just like considering refueling coal to a boiler, you get THAT coal (representing data to conpute) FROM OUTSIDE the boiler (representing the thread)
No. It is even impossible to do it properly with normal pascal constructs for the same reason as the above.
Threadvars should be initialized in the context of its own threads.

What's all that kind of shouting and nonsense. It is about threadvars. These are only valid for the thread and can only be initialized for any normal intend and purpose IN the thread itself.
Stop shouting and learn something about thread programming.

strange then, i am a blue martian, since 1997, BECAUSE IT IS THE ONLY WAY I EVER MADE IT
« Last Edit: June 22, 2016, 12:30:09 pm by sam707 »

sam707

  • Guest
Re: Thread Variables
« Reply #26 on: June 22, 2016, 12:26:40 pm »
me, my staff, and @User137 I guess ;) @Thaddy

Thaddy

  • Hero Member
  • *****
  • Posts: 19273
  • Glad to be alive.
Re: Thread Variables
« Reply #27 on: June 22, 2016, 12:28:30 pm »

strange then, i am a blue martian, since 1997, BECAUSE IT IS THE ONLY WAY I EVER MAKE IT

Strange then, because unless you rely on sychronizing whatever into infinity you always did it wrong and it never will work.

Give a simple example, I call your bluff. Show your cards.
objects are fine constructs. You can even initialize them with constructors.

sam707

  • Guest
Re: Thread Variables
« Reply #28 on: June 22, 2016, 12:29:37 pm »

strange then, i am a blue martian, since 1997, BECAUSE IT IS THE ONLY WAY I EVER MAKE IT

Strange then, because unless you rely on sychronizing whatever into infinity you always did it wrong and it never will work.

Give a simple example, I call your bluff. Show your cards.

Ok then, Im also DANG HAPPY to NEVER have to work with you on serious projects ;)

Thaddy

  • Hero Member
  • *****
  • Posts: 19273
  • Glad to be alive.
Re: Thread Variables
« Reply #29 on: June 22, 2016, 12:41:44 pm »

strange then, i am a blue martian, since 1997, BECAUSE IT IS THE ONLY WAY I EVER MAKE IT

Strange then, because unless you rely on sychronizing whatever into infinity you always did it wrong and it never will work.

Give a simple example, I call your bluff. Show your cards.

Ok then, Im also DANG HAPPY to NEVER have to work with you on serious projects ;)

Show your code. I am known to know a few things about multit-hreaded pascal. That's not bluff. That is a known fact in the community for over 25 years.
So: show the code. stop bullying. If I am wrong I admit it. O:-) But I am not. No code? Be silent. very very silent.

Look at e.g.: http://www.delphibasics.co.uk/RTL.asp?Name=ThreadVar to put you wrong.

Furthermore: Threadvars can not even be initialized globally according to the documentation.
Of course not!
« Last Edit: June 22, 2016, 01:12:42 pm by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

 

TinyPortal © 2005-2018