[Solved] Thread problems on Linux Ubuntu 12.04 Lazarus 1.0 lazarus / 32-Linux ---------------------------------------------------------------------- Max JRB · 2012-10-04 18:22 Hi, i'm new on Lazarus, and this is my fisrt question on the forum. I'm having problems when I run an application using threads, on windows works fine, but when I run it on Linux Ubuntu 12.04 my app crashes. It's a very small app just to understand threads. this is the code: for type: Code: [Select]unit Unit1; {$mode objfpc}{$H+} interface uses   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,   ComCtrls; type   { TMyThread }   TMyThread = class(TThread)   private     fStatusText: string;     procedure CambiaLabel();   protected     procedure Execute; override;   public     constructor Create(CreateSuspended: boolean);   end; implementation {$R *.lfm} { TMyThread } procedure TMyThread.CambiaLabel(); begin     Form1.Label1.Caption:=fStatusText;         Form1.ProgressBar1.StepIt;     if Form1.ProgressBar1.Position = Form1.ProgressBar1.Max then begin        Form1.miHilo.Terminate;        Form1.ProgressBar1.Position := 0;     end; end; procedure TMyThread.Execute; var   newStatus : string; begin   fStatusText := 'TMyThread comenzando...';   fStatusText := 'TMyThread Corriendo ...';   while (not Terminated) and (true {any condition required}) do begin     //here goes the code of the main thread loop      Synchronize(@CambiaLabel);   end; end; constructor TMyThread.Create(CreateSuspended: boolean); begin   FreeOnTerminate := True;   inherited Create(CreateSuspended); end; and the form: Code: [Select]{ TForm1 } procedure TForm1.Button1Click(Sender: TObject); begin     if miHilo <> nil then begin    ShowMessage('Ya se esta ejecutando');    end    else begin    miHilo := TMyThread.Create(false);    end; end; procedure TForm1.Button2Click(Sender: TObject); begin    if miHilo <> nil then begin    miHilo.Terminate;    ShowMessage('Hilo terminado');    miHilo:=nil;    end; end; procedure TForm1.FormCreate(Sender: TObject); begin end; end. I attached images with the error messages that shows when crashing ---------------------------------------------------------------------- Blaazen · 2012-10-04 19:23 And your *.lpr file ? You have to add there line {$define UseCThreads}: Code: [Select]program myproject; {$mode objfpc}{$H+} {$define UseCThreads} uses    {$IFDEF UNIX}{$IFDEF UseCThreads}   cthreads,   {$ENDIF}{$ENDIF} .... Do you have it there ? ---------------------------------------------------------------------- tomek · 2012-10-04 21:14 X11 isn't thread safe. Only main thread for the GUI. ---------------------------------------------------------------------- Max JRB · 2012-10-05 03:16 Thanks for the answers, checking the .lpr files comparing it with the one from the multithread example I found that had one difference. the one from my project had this: Code: [Select]{$mode objfpc}{$H+} uses   {$IFDEF UNIX}{$IFDEF UseCThreads}   cthreads,   {$ENDIF}{$ENDIF} and the one from multithreadingexample: Code: [Select]  {$IFDEF UNIX}   cthreads,   {$ENDIF} doesn't have this validation {$IFDEF UseCThreads} I deleted it and now works thanks ---------------------------------------------------------------------- Max JRB · 2012-10-05 03:25 Quote from: Blaazen on October 04, 2012, 09:23:07 pmAnd your *.lpr file ? You have to add there line {$define UseCThreads}: Code: [Select]program myproject; {$mode objfpc}{$H+} {$define UseCThreads} uses    {$IFDEF UNIX}{$IFDEF UseCThreads}   cthreads,   {$ENDIF}{$ENDIF} .... Do you have it there ? Oh! thanks! I just see your answer That was the problem, thanks again, I'm happy to see such a quick answers on this forum