Recent

Author Topic: What am I doing wrong with TThread?  (Read 1790 times)

Dasun

  • New Member
  • *
  • Posts: 41
What am I doing wrong with TThread?
« on: October 16, 2016, 07:15:28 pm »
After a good read of all the doco I could find on TThread I thought I got it and I expected the code below to work...so compiled in console mode then got the following:


Create is printed,
Then an '...AccessViolation - 45CF64'

So crashed on the create call but never executed the constructor create code.

FPC 3.0.0  & Mint latest and greatest



Code: Pascal  [Select][+][-]
  1. {$mode objfpc} {H+}
  2.  
  3. Program runprocess ;
  4.  
  5. Uses
  6.     cthreads ,
  7. //    cmem,
  8.     Process,
  9.     StrUtils,
  10.     SysUtils,
  11.     Classes ;
  12.  
  13. Type
  14.  
  15.     n_Run_Thread = Class(TThread)
  16.             Private
  17.  
  18.             Protected
  19.                Procedure Execute; OverRide ;
  20.             Public
  21.                is_Output : String ;
  22.  
  23.                Constructor Create(ab_Suspend : Boolean) ;
  24.  
  25.     End ;
  26.  
  27.  
  28. Procedure n_Run_Thread.Execute ;
  29. Begin
  30.  
  31.     Self.is_Output := 'And Away We Go' ;
  32.     Writeln(Self.is_Output) ;
  33. End ;
  34.  
  35. Constructor n_Run_Thread.Create(ab_Suspend : Boolean);
  36. Begin
  37.  
  38.     writeln('1');      
  39.  
  40.     Inherited Create(ab_Suspend) ;
  41. End ;
  42.  
  43.  
  44. Var    
  45.     ln_Run_Thread : n_Run_Thread ;
  46. Begin
  47.  
  48. writeln('Create') ;
  49.  
  50.   ln_Run_Thread.Create(True) ;
  51.   ln_Run_Thread.Start ;
  52.  
  53. //writeln('Wait') ;
  54. //      ln_Run_Thread.WaitFor ;
  55.   FreeAndNil(ln_Run_Thread) ;
  56.  
  57.  
  58. Writeln('Done') ;
  59.  
  60. End.
  61.  
  62. [\code]

molly

  • Hero Member
  • *****
  • Posts: 2345
Re: What am I doing wrong with TThread?
« Reply #1 on: October 16, 2016, 07:18:50 pm »
ln_Run_Thread.Create(True) ;

should read:
ln_Run_Thread := n_Run_Thread.Create(True) ;

Please use T prefix for type definitions. it makes things much more clear.

PS: afaik writeln is not threadsafe.
« Last Edit: October 16, 2016, 07:21:29 pm by molly »

Dasun

  • New Member
  • *
  • Posts: 41
Re: What am I doing wrong with TThread?
« Reply #2 on: October 16, 2016, 07:31:34 pm »
OMG I am an idiot for not noticing that !!!! :-[ Should not work on a Sunday!!1

Thank you Molly - and sorry for my odd naming conventions, comes from work!

molly

  • Hero Member
  • *****
  • Posts: 2345
Re: What am I doing wrong with TThread?
« Reply #3 on: October 16, 2016, 08:07:21 pm »
np Dasun. it happens sometimes.

You are of course allowed to use any naming scheme you wish. Just saying that when i read:
T_Run_Thread.Create(True) ;

it immediately distinguishes from:
ln_Run_Thread.Create(True);

The latter would  then be able to obviously tell that a variable is used to try create a new instance. The former is using the right approach but doesn't store itself into a variable (which is allowed when f.i. using a with statement in front).

Using your type naming scheme was problematic (to me) simply because i am not used of having types names that are not using T prefix. You could apply the same for n_ vs ln_ but in this particular case that went wrong for you :D

 

TinyPortal © 2005-2018