Recent

Author Topic: Thread - linux - raspbian-buster  (Read 1346 times)

Mi-Ki

  • Jr. Member
  • **
  • Posts: 74
Thread - linux - raspbian-buster
« on: August 30, 2019, 09:10:28 am »
Hello.
How to make thead?
Everything I try and works under windows doesn't work in linux.

Project projectl raised exception class 'RunError(232)'.
At address 6B
https://i.postimg.cc/RV0MK05y/error.png

Function thead also does not work in linux.

Code: Pascal  [Select][+][-]
  1. var
  2.   Form1: TForm1;
  3.   thh: THandle;
  4.  
  5. implementation
  6.  
  7. {$R *.lfm}
  8.  
  9. { TForm1 }
  10. Function OtevriThread(Param: Pointer): Longint;
  11. begin
  12.   beep;
  13.   EndThread (thh);
  14. end;
  15.  
  16. procedure TForm1.Button1Click(Sender: TObject);
  17. var
  18.   thid: Cardinal;
  19. begin
  20.   thh:=BeginThread(nil,0,@OtevriThread,nil,0,thid);
  21. end;                        

Linux error 'RunError(232)'.
It works in windows.
« Last Edit: August 30, 2019, 09:17:41 am by Mi-Ki »

Xor-el

  • Sr. Member
  • ****
  • Posts: 404
Re: Thread - linux - raspbian-buster
« Reply #1 on: August 30, 2019, 09:22:01 am »
Hi, did you add the unit cthreads to the uses clause?

https://www.freepascal.org/docs-html/current/rtl/cthreads/index.html

Mi-Ki

  • Jr. Member
  • **
  • Posts: 74
Re: Thread - linux - raspbian-buster
« Reply #2 on: August 30, 2019, 09:36:08 am »
@Xor-el
When I add cthreads to uses then error.
Project projectl raised exception class 'RunError(211)'.

avra

  • Hero Member
  • *****
  • Posts: 2514
    • Additional info
Re: Thread - linux - raspbian-buster
« Reply #3 on: August 30, 2019, 09:52:43 am »
When I add cthreads to uses then error.
Did you add it as a first unit in main project file? As shown:
Code: Pascal  [Select][+][-]
  1. uses
  2.   {$ifdef unix}cthreads{$endif},
  3.    classes, sysutils;
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

rvk

  • Hero Member
  • *****
  • Posts: 6169
Re: Thread - linux - raspbian-buster
« Reply #4 on: August 30, 2019, 09:53:52 am »
Why are you calling endthread with parameter thh?

It is
procedure EndThread(ExitCode : DWord);

So you give it an exitcode (i.e. 0 or any other if it's an error).
Calling it with tth will pass a thread pointer as exitcode which leads to unexpected results.
 
So just use
EndThread(0);


Mi-Ki

  • Jr. Member
  • **
  • Posts: 74
Re: Thread - linux - raspbian-buster
« Reply #5 on: August 30, 2019, 10:25:48 am »
Code: Pascal  [Select][+][-]
  1. uses
  2.   {$ifdef unix}cthreads{$endif},
  3.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;  
  4.  
  5. Function OpenThread(Param: Pointer): Longint;
  6. begin
  7.   beep;
  8.   EndThread(0);
  9. end;
  10.  
  11. procedure TForm1.Button1Click(Sender: TObject);
  12. var
  13.  thh: THandle;
  14.  thid: Cardinal;
  15. begin
  16.  thh:=BeginThread(nil,0,@OpenThread,nil,0,thid);
  17. end;
                               
Project projectl raised exception class 'RunError(211)'.

devEric69

  • Hero Member
  • *****
  • Posts: 648
Re: Thread - linux - raspbian-buster
« Reply #6 on: August 30, 2019, 11:47:37 am »
Seriously, I think you should read how to use the Lazarus TThread class ( and it will save you time for the future ), as well as examples of synchronous or asynchronous inter-threads use.

My two cents.
use: Linux 64 bits (Ubuntu 20.04 LTS).
Lazarus version: 2.0.4 (svn revision: 62502M) compiled with fpc 3.0.4 - fpDebug \ Dwarf3.

Thaddy

  • Hero Member
  • *****
  • Posts: 14381
  • Sensorship about opinions does not belong here.
Re: Thread - linux - raspbian-buster
« Reply #7 on: August 30, 2019, 01:03:40 pm »
Project projectl raised exception class 'RunError(211)'.
That seems highly unlikely, because 211 is an abstract error and you are not using classes. RunError( 232) is more likely: use cthreads unit.

Try the class method TThread.ExecuteInThread:
Code: Pascal  [Select][+][-]
  1. {$mode delphi}{$H+}
  2. uses cthreads,classes;
  3.  
  4. procedure Aproc(data:pointer);
  5. begin
  6.   writeln('written from thread');
  7. end;
  8. begin
  9.   TThread.ExecuteInThread(@AProc); // class method, no instance necessary
  10. end.
Tested on Raspbian buster.
« Last Edit: August 30, 2019, 01:08:54 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

avra

  • Hero Member
  • *****
  • Posts: 2514
    • Additional info
Re: Thread - linux - raspbian-buster
« Reply #8 on: August 30, 2019, 01:07:32 pm »
Project projectl raised exception class 'RunError(211)'.
You use cthreads in unit where your form is. That is not main project LPR file.
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

Thaddy

  • Hero Member
  • *****
  • Posts: 14381
  • Sensorship about opinions does not belong here.
Re: Thread - linux - raspbian-buster
« Reply #9 on: August 30, 2019, 01:10:36 pm »
You use cthreads in unit where your form is. That is not main project LPR file.
Yes but that is runerror(232), NOT 211 which is an abstract error.
https://freepascal.org/docs-html/current/user/userap4.html#x190-197000D

I think that was a typo.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

munair

  • Hero Member
  • *****
  • Posts: 798
  • compiler developer @SharpBASIC
    • SharpBASIC
Re: Thread - linux - raspbian-buster
« Reply #10 on: August 30, 2019, 03:39:14 pm »
You use cthreads in unit where your form is. That is not main project LPR file.

Indeed. In a recent project I implemented a thread and it compiles and runs fine on both Windows 32/64 and Linux 64. In the lpr file the following uses clause is included:

Code: Pascal  [Select][+][-]
  1. uses
  2.   {$IFDEF UNIX}{$IFDEF UseCThreads}
  3.   cthreads,
  4.   {$ENDIF}{$ENDIF}
  5.   Interfaces, // this includes the LCL widgetset
  6.   Forms, umain, LazSerialPort
  7.   { you can add units after this };

As you can see it is placed BEFORE the others. Then in the main form's unit a thread class is defined:

Code: Pascal  [Select][+][-]
  1. unit umain;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls,
  9.   Math, LazSerial, DateUtils, FileUtil, LazSysUtils;
  10.  
  11. type
  12.   TReadDataThread = class(TThread)
  13.   private
  14.     procedure UpdateList;
  15.   protected
  16.     procedure Execute; override;
  17.   public
  18.     constructor Create(CreateSuspended: boolean);
  19.   end;
  20.  
  21.   { TForm1 }
  22. ...

Works like a charm, but you have to read some documentation  in order to implement it properly. Especially interface related stuff should be done separately (in my example this is done by calling Synchronize(@UpdateList); from the Execute procedure.
keep it simple

 

TinyPortal © 2005-2018