Recent

Author Topic: [twins] TThread vs TProcess  (Read 5108 times)

Fred vS

  • Hero Member
  • *****
  • Posts: 3820
    • StrumPract is the musicians best friend
[twins] TThread vs TProcess
« on: November 12, 2012, 01:39:55 am »
Hello everybody.

I try to use TThread with Linux but when i create a new thread, i get that message :

Quote
Semaphore init failed (possibly too many concurrent threads).

Press OK to ignore and risk data corruption.
Press Cancel to kill the program.

Hum sad.

My question is : If i used a TProsess, is it equal with a TThread ?

When i use that parameters, Tprocess looks like a TThread but i do not get any error messges:

Code: [Select]

TMyProcess = class(TProcess)
  private

  protected

  public
    procedure Execute; override;
  end;     

Procedure initprocess();
begin

myprocess:= TMyprocess.Create(nil);
        myprocessOptions := myprocess.Options + [poNoConsole];
 myprocess.Priority := ppHigh ;
 myprocess.showwindow :=  swonone ;

end;

And for the execute command :

Code: [Select]
procedure TMyprocess.Execute;

begin

///// all i want to do in a independent thread

end; 


And what are the advantage or disadvantage to use a tprocess vs a tthread ?


Thanks
« Last Edit: November 12, 2012, 05:06:14 pm by Fred vS »
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: TThread vs TProcess
« Reply #1 on: November 12, 2012, 01:57:42 am »
You have to add:
Code: [Select]
{$define UseCThreads}
to your *.lpr file.

Complete code should look like this:
Code: [Select]
{$mode objfpc}{$H+}
{$DEFINE UseCThreads}
uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}             

Of course, there may be hundred other reasons why it does not work  :)
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Fred vS

  • Hero Member
  • *****
  • Posts: 3820
    • StrumPract is the musicians best friend
Re: TThread vs TProcess
« Reply #2 on: November 12, 2012, 02:27:43 am »
@ Blaazen : i have already try it, but no luck.

Have you seen my first message, it seems that it was trunked, now, it is ok.
What do you thing about Thread vs Process ?
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: TThread vs TProcess
« Reply #3 on: November 12, 2012, 07:53:34 am »
@ Blaazen : i have already try it, but no luck.
Make sure cthreads is in the uses clause in the main program code as the very first. I have also encountered the semaphore message when I forgot cthreads ;)

The difference between Threads and Processes is just how much is shared (memory, file handles,...) between the tasks. Threads make communicating between tasks faster and easier (except for thread safety) because memory is shared. Processes need inter process communication to exchange data. If needed, processes are easier to kill than threads. A crashing process does not affect the other processes while a crash in a thread can affect the whole process. Performance-wise there is virtually no difference. Setting up a new process has typically more overhead than creating a new thread. Switching tasks can sometimes be faster than switching threads in multi-core systems because of shared data in caches that need to be synced.

To resume: if the tasks are fairly isolated and long lasting, use processes. If the tasks need to communicate a lot between them in various data formats or are very short lived, use threads.

Fred vS

  • Hero Member
  • *****
  • Posts: 3820
    • StrumPract is the musicians best friend
Re: TThread vs TProcess
« Reply #4 on: November 12, 2012, 04:49:35 pm »
@ ludob : Ooops, i do not have seen that my topic exists twice  :-X

http://www.lazarus.freepascal.org/index.php/topic,18864.msg106825/topicseen.html#new

Many thanks for your light.
In my twin topic, i give the code where comes the error:
Is it a fault in it ?
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

 

TinyPortal © 2005-2018