Recent

Author Topic: Compile - Threads - Linux - 64bit  (Read 13132 times)

tomek

  • Jr. Member
  • **
  • Posts: 85
Compile - Threads - Linux - 64bit
« on: April 16, 2012, 10:44:24 pm »
I've some drawing on BGRABitmap running with threads.
On Windows 2000/XP/Vista/7 everything works perfect.
Now I try to compile my project on Linux (Fedora 16 64bit).
App crash. Drawing in thread ends with
Code: [Select]
xcb_io.c:221: poll_for_event: Assertion `(((long) (event_sequence) - (long) (dpy->request)) <= 0)' Without threads (on main app thread) works ok.

I try to find what is goin on, and when I compile with -Cr (range) and -O2 then I get
Code: [Select]
bgrabitmap5.7.1/bgracanvas2d.pas(292,5) Error: Asm: [movq mem??,reg64] invalid combination of opcode and operands
so I suspect that this is related to this problem.

Someone can help me with this ?

Ocye

  • Hero Member
  • *****
  • Posts: 518
    • Scrabble3D
Re: Compile - Threads - Linux - 64bit
« Reply #1 on: April 17, 2012, 05:54:50 pm »
I'm not familar with BGRABitmap but any GUI operation must not be done within a thread. You can use Synchronize() but I guess there are principal changes necessary. Perhaps, you can add a code snippet (I'm confused about "Without threads (on main app thread) works ok").
BTW, on Linux you need to add cthreads as first unit for a multi-threaded application (you will find all information in the wiki).
Lazarus 1.7 (SVN) FPC 3.0.0

circular

  • Hero Member
  • *****
  • Posts: 4195
    • Personal webpage
Re: Compile - Threads - Linux - 64bit
« Reply #2 on: April 17, 2012, 07:24:02 pm »
I try to find what is goin on, and when I compile with -Cr (range) and -O2 then I get
Code: [Select]
bgrabitmap5.7.1/bgracanvas2d.pas(292,5) Error: Asm: [movq mem??,reg64] invalid combination of opcode and operands
so I suspect that this is related to this problem.
I don't see anything special here.

By the way, I would be very interested if you can explain briefly your current project on this page. And more precisely for what you are using Canvas2D.
http://www.lazarus.freepascal.org/index.php/topic,16636.0.html
Conscience is the debugger of the mind

tomek

  • Jr. Member
  • **
  • Posts: 85
Re: Compile - Threads - Linux - 64bit
« Reply #3 on: April 17, 2012, 09:32:05 pm »
I use BGRABitmap as backend for TAChart, something like:
Code: [Select]

bmpBGRA := TBGRABitmap.Create(Chart1.Width+1, Chart1.Height+1);
id := TBGRABitmapDrawer.Create(bmpBGRA);
Chart1.Draw(id, Rect(0, 0, Chart1.Width+1, Chart1.Height+1));   
bmpBGRA.Draw(RepPlot.Canvas, -1 , -1 , false);                          <----- crash
I will try to bring the simplest version this part of my project and I'll attach.

circular

  • Hero Member
  • *****
  • Posts: 4195
    • Personal webpage
Re: Compile - Threads - Linux - 64bit
« Reply #4 on: April 17, 2012, 10:23:02 pm »
Oh ok, then Ocye must be right. You seem to draw on a form canvas without Synchronize.
Conscience is the debugger of the mind

tomek

  • Jr. Member
  • **
  • Posts: 85
Re: Compile - Threads - Linux - 64bit
« Reply #5 on: April 17, 2012, 11:18:28 pm »
Oh ok, then Ocye must be right. You seem to draw on a form canvas without Synchronize.
I forgot, I use Chart as non visual Chart1:=TChart.Create(nil) (only for draw chart to bitmap), and on MS systems works ok.

circular

  • Hero Member
  • *****
  • Posts: 4195
    • Personal webpage
Re: Compile - Threads - Linux - 64bit
« Reply #6 on: April 18, 2012, 12:10:43 am »
Yes, but when you do
Code: [Select]
bmpBGRA.Draw(RepPlot.Canvas, -1 , -1 , false); 
You are drawing on a canvas, right ?
Conscience is the debugger of the mind

tomek

  • Jr. Member
  • **
  • Posts: 85
Re: Compile - Threads - Linux - 64bit
« Reply #7 on: April 18, 2012, 08:54:14 am »
Yes, but when you do
Code: [Select]
bmpBGRA.Draw(RepPlot.Canvas, -1 , -1 , false); 
You are drawing on a canvas, right ?
Canvas of Bitmap, RepPlot is TBitmap.

tomek

  • Jr. Member
  • **
  • Posts: 85
Re: Compile - Threads - Linux - 64bit
« Reply #8 on: April 18, 2012, 09:26:35 am »
Ok, simple example attached.

Ocye

  • Hero Member
  • *****
  • Posts: 518
    • Scrabble3D
Re: Compile - Threads - Linux - 64bit
« Reply #9 on: April 18, 2012, 01:11:36 pm »
Why do you want to draw threaded? For speed, animation etc. you should use OpenGL. A simple rect is drawn faster than your screen refreshes. If you add a Synchronize()-method and call Plot() from within this method, the thread is waiting until this procedure has finished. Thus, it wouldn't be faster.

Perhaps you can do calculation in threads and let the main thread paint whenever possible or by timer. Something like this:
Code: [Select]
type
  TData=class
     <many values>
end;
TTest=class(TThread)
private
  FData:TData;
public
  procedure Execute;
  property Data: TData read FData;
end;

procedure TTest.Execute;
begin
  while not Terminated do
  begin
    <Calculate TData stuff>
  end;
end;

// main thread
procedure TForm1.OnTimer();
begin
  DoPaint(Test.Data);
end;

Threading is always error-prone and you should carefully read the first wiki section "Do you need multi-threading?".
Lazarus 1.7 (SVN) FPC 3.0.0

tomek

  • Jr. Member
  • **
  • Posts: 85
Re: Compile - Threads - Linux - 64bit
« Reply #10 on: April 18, 2012, 09:39:34 pm »
Why do you want to draw threaded?
Yes and No, I've some other procedures in threads, it would be better if everything were performed together. But of course I can put this to non threading execution.
For speed, animation etc. you should use OpenGL. A simple rect is drawn faster than your screen refreshes. If you add a Synchronize()-method and call Plot() from within this method, the thread is waiting until this procedure has finished. Thus, it wouldn't be faster.
 
As I mentioned before I use this for TAChart backend so OpenGL is eliminated, and I don't use drawing on visible component,  I draw on TBitmap.

... Threading is always error-prone and you should carefully read the first wiki section "Do you need multi-threading?".
I just can not understand why it does not work on Linux, thread operations don't touch GUI elements.

circular

  • Hero Member
  • *****
  • Posts: 4195
    • Personal webpage
Re: Compile - Threads - Linux - 64bit
« Reply #11 on: April 18, 2012, 11:55:42 pm »
Try to "Synchronize" the drawing on the TBitmap, to see if it solves your problem.
Conscience is the debugger of the mind

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1927
Re: Compile - Threads - Linux - 64bit
« Reply #12 on: April 19, 2012, 12:23:18 am »
I just can not understand why it does not work on Linux, thread operations don't touch GUI elements.

Afaik X11 is simply not thread-safe. As soon as X is involved your running into problems afair, no matter if a Bitmap is "visible" or not.

kris

  • Jr. Member
  • **
  • Posts: 59
Re: Compile - Threads - Linux - 64bit
« Reply #13 on: September 27, 2016, 08:52:27 am »
I found this topic and although genrally an advice has been given to synchronize drawing onto Canvas, I really think this is only walking away from the problem. I am writing a simple game, and drawing a frame out of background and sprites. It is generally a lot of computation, just compoziting the final frame. I would like all of that to happen in the thread, I would only ever draw on a memory TBitmap, that would never go to the screen. Then, once I have the frame ready, I would Synchronize a method that prints it as one single Cavas.Draw(x,y,MyFrame);
Why I cannot do this on Linux? While I aparently can do this on Windows. I know I can synchronize all the Canvas.something calls, but that defeats the purpose, it would cause a delay in my main thread every time a heavy bitmap compoziting operation is happening, which happens a lot for every frame.
How to go about this? Does anyone know?

circular

  • Hero Member
  • *****
  • Posts: 4195
    • Personal webpage
Re: Compile - Threads - Linux - 64bit
« Reply #14 on: September 27, 2016, 06:21:34 pm »
The computation can be done in a thread on a TBGRABitmap (without accessing the Canvas property and without drawing text) and drawing on the canvas using Synchronize.
Conscience is the debugger of the mind

 

TinyPortal © 2005-2018