Recent

Author Topic: Thread Variables  (Read 16253 times)

Blestan

  • Sr. Member
  • ****
  • Posts: 461
Thread Variables
« on: June 19, 2016, 12:24:44 pm »
Hi guys!
please give some advice on what is the correct way to initialize / finalize thread vars

Regards,
Blestan
Speak postscript or die!
Translate to pdf and live!

Thaddy

  • Hero Member
  • *****
  • Posts: 19262
  • Glad to be alive.
Re: Thread Variables
« Reply #1 on: June 19, 2016, 01:54:18 pm »
Just as you would normal variables. The only difference is they are per thread local, so when needed you need to do it per thread instead of per process.
objects are fine constructs. You can even initialize them with constructors.

Blestan

  • Sr. Member
  • ****
  • Posts: 461
Re: Thread Variables
« Reply #2 on: June 19, 2016, 06:37:06 pm »
is there a way to install a callbacks to be executed.on thread begin and thread end?
Speak postscript or die!
Translate to pdf and live!

HeavyUser

  • Sr. Member
  • ****
  • Posts: 397
Re: Thread Variables
« Reply #3 on: June 19, 2016, 07:07:05 pm »
is there a way to install a callbacks to be executed.on thread begin and thread end?
The thread code begins when the execute method starts to run and exists when the execute method finishes. I'm assuming you are using the TThread class. You can introduce your own events in there.

Blestan

  • Sr. Member
  • ****
  • Posts: 461
Re: Thread Variables
« Reply #4 on: June 19, 2016, 08:12:02 pm »
ok i know that oop way ... but if a thread is started with beginthread() the tls will be not initialized properly
Speak postscript or die!
Translate to pdf and live!

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Thread Variables
« Reply #5 on: June 20, 2016, 12:10:54 am »
Simple place to initialize vars is in the thread's constructor. You can finalize in destructor too if want.

Ondrej Pokorny

  • Full Member
  • ***
  • Posts: 220
Re: Thread Variables
« Reply #6 on: June 20, 2016, 10:00:03 am »
Simple place to initialize vars is in the thread's constructor. You can finalize in destructor too if want.

The thread's constructor is called in the calling thread (mostly main thread) and not in the new thread's thread. That could be a problem sometimes.

Thaddy

  • Hero Member
  • *****
  • Posts: 19262
  • Glad to be alive.
Re: Thread Variables
« Reply #7 on: June 20, 2016, 10:51:49 am »
Simple place to initialize vars is in the thread's constructor. You can finalize in destructor too if want.

The thread's constructor is called in the calling thread (mostly main thread) and not in the new thread's thread. That could be a problem sometimes.

@Ondej
Feel free to write that more strongly. "Sometimes" in the context of Threads is a threat to your code. (note the t).
My advice would be to always set up the Threadvars at the beginning of the threadcode and finalize them if needed after the thread loop if any has finished. Or: consider a thread as a process.
objects are fine constructs. You can even initialize them with constructors.

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Thread Variables
« Reply #8 on: June 20, 2016, 01:48:04 pm »
That "sometimes" i guess is something where you need to initialize array of 10000000 variables or something. I would consider that as work though, as it belongs to Execute.

Blestan

  • Sr. Member
  • ****
  • Posts: 461
Re: Thread Variables
« Reply #9 on: June 20, 2016, 07:49:04 pm »
ok so, there is no way to execute a procedure in the begining of every thread... no matters if it use  oop tthread or procedural way ... if some lib is starting a newthread there is no way to perepare variables for it?
Speak postscript or die!
Translate to pdf and live!

Ondrej Pokorny

  • Full Member
  • ***
  • Posts: 220
Re: Thread Variables
« Reply #10 on: June 20, 2016, 10:03:08 pm »
ok so, there is no way to execute a procedure in the begining of every thread... no matters if it use  oop tthread or procedural way ... if some lib is starting a newthread there is no way to perepare variables for it?

Initialize your variables as the very first thing in Execute. Finalize them as the very last thing in Execute. You can also use try-finally block if needed.

sam707

  • Guest
Re: Thread Variables
« Reply #11 on: June 21, 2016, 12:26:48 am »
T hread
L ocal
S torage

use the ThreadVar declaration for T hreads L ocal S torage management

not a big mistery

http://www.freepascal.org/docs-html/ref/refse25.html

and this is how FP Compiler makes it EASY

BeniBela

  • Hero Member
  • *****
  • Posts: 959
    • homepage
Re: Thread Variables
« Reply #12 on: June 21, 2016, 01:21:09 am »
and this is how FP Compiler makes it EASY

And very slow

(bero said)

sam707

  • Guest
Re: Thread Variables
« Reply #13 on: June 21, 2016, 02:08:06 am »

And very slow

(bero said)

... ... ... well, depending your programming skill, your bad habits, your data structures, strategy guidelines to access them, and etc .... even Assembly language can be slow,  :D  :D  :D
« Last Edit: June 21, 2016, 02:11:20 am by sam707 »

HeavyUser

  • Sr. Member
  • ****
  • Posts: 397
Re: Thread Variables
« Reply #14 on: June 21, 2016, 07:29:26 am »
ok i know that oop way ... but if a thread is started with beginthread() the tls will be not initialized properly
Even using beginthread you need to provide a procedure to be executed initialize it in there.

Simple place to initialize vars is in the thread's constructor. You can finalize in destructor too if want.
The thread's constructor is called in the calling thread (mostly main thread) and not in the new thread's thread. That could be a problem sometimes.
sometimes? the constructor and destructor of a TThread is executed in the main thread, 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.

 

TinyPortal © 2005-2018