Recent

Author Topic: syncobjs.TEventObject class doesn't work in Ubuntu  (Read 1258 times)

felleroff

  • Newbie
  • Posts: 3
syncobjs.TEventObject class doesn't work in Ubuntu
« on: December 10, 2023, 01:19:34 am »
Please help me understand why the TEventObject synchronization object does not work in Ubuntu.

This code works on Windows 10 but doesn't work on Ubuntu 18.04
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, syncobjs;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     procedure Button1Click(Sender: TObject);
  17.   private
  18.     FEventObject: TEventObject;
  19.   end;
  20.  
  21. var
  22.   Form1: TForm1;
  23.  
  24. implementation
  25.  
  26. {$R *.lfm}
  27.  
  28. { TForm1 }
  29.  
  30. procedure TForm1.Button1Click(Sender: TObject);
  31. begin
  32.   FEventObject := TEventObject.Create(nil, {AManualReset:} False, {InitialState:} True, {Name:} 'some_name');
  33.   if FEventObject.WaitFor(INFINITE) <> wrSignaled then
  34.     ShowMessage('Error')
  35.   else
  36.     ShowMessage('Lock')
  37. end;
  38.  
  39. end.
  40.  

** cthreads unit has been added to the project file (.lpr)
** Lazarus 2.2.6, x64
** Windows 10, x64
** Ubuntu 18.04, x64

On Windows
  • Launch two instances of the application
  • Click Button1 in app-instance1 > "Lock" message
  • Click Button1 in app-instance2 > the application will hang as the FEventObject will wait for the event - this is correct

On Ubuntu
  • Launch two instances of the application
  • Click Button1 in app-instance1 > "Lock" message
  • Click Button1 in app-instance2 > "Lock" message - FEventObject successfully fires event - this is not correct

felleroff

  • Newbie
  • Posts: 3
Re: syncobjs.TEventObject class doesn't work in Ubuntu
« Reply #1 on: December 10, 2023, 11:31:45 am »
Let me try to ask the question in a different way.

Does the Name parameter work in the TEventObject constructor for Ubuntu?

Code: Pascal  [Select][+][-]
  1. public constructor TEventObject.Create(
  2.   EventAttributes: PSecurityAttributes;
  3.   AManualReset: Boolean;
  4.   InitialState: Boolean;
  5.   const Name: string
  6. );
  7.  
  8. Arguments
  9.  
  10. EventAttributes: Security attributes (only used on Windows)
  11. AManualReset   : Manual reset allowed
  12. InitialState   : Initial event state
  13. Name           : Name uniquely identifying the event in this process.


AlexTP

  • Hero Member
  • *****
  • Posts: 2488
    • UVviewsoft
Re: syncobjs.TEventObject class doesn't work in Ubuntu
« Reply #2 on: December 10, 2023, 12:02:16 pm »
Name of eventObject is not supported on Unix. I see it in the 'fpcsrc/rtl/unix/cthreads.pp'.

Code: Pascal  [Select][+][-]
  1. function IntBasicEventCreate(EventAttributes : Pointer; AManualReset,InitialState : Boolean;const Name : ansistring):pEventState;

'Name' is not used there.
Free Pascal Compiler version 3.2.3-869-g340913a9db [2023/11/30] for x86_64

« Last Edit: December 10, 2023, 12:14:09 pm by AlexTP »

felleroff

  • Newbie
  • Posts: 3
Re: syncobjs.TEventObject class doesn't work in Ubuntu
« Reply #3 on: December 10, 2023, 01:13:56 pm »
AlexTP,  thank you.
Yes, it looks like you are right: "Name of TEventObject is not supported on Unix".
Unfortunately, I didn't find anything about this in the Lazarus help.

Can you please tell me what object for synchronizing threads in different processes is used in Lazarus for Unix?

Thaddy

  • Hero Member
  • *****
  • Posts: 16199
  • Censorship about opinions does not belong here.
Re: syncobjs.TEventObject class doesn't work in Ubuntu
« Reply #4 on: December 10, 2023, 01:47:44 pm »
You should identify the event by its handle, not its name That is cross platform. Named events are for poor programmers who need holding hands or diapers. Or both.
TEventObject derives from THandleObject which has a readonly property called.... wait for it... Handle...
« Last Edit: December 10, 2023, 01:58:41 pm by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

AlexTP

  • Hero Member
  • *****
  • Posts: 2488
    • UVviewsoft
Re: syncobjs.TEventObject class doesn't work in Ubuntu
« Reply #5 on: December 10, 2023, 01:52:22 pm »
Thaddy,
and how can we create TEventObject by handle? TEventObject.Create don't have a handle.

Thaddy

  • Hero Member
  • *****
  • Posts: 16199
  • Censorship about opinions does not belong here.
Re: syncobjs.TEventObject class doesn't work in Ubuntu
« Reply #6 on: December 10, 2023, 02:01:15 pm »
No, it returns the handle, silly. After the create is called the handle is contained in the created EventObject...... Stupid remark, very grumpy... >:D >:D >:D >:D  :o Haven´ t been grumpy yet in 2023, so here you go... 8)
« Last Edit: December 10, 2023, 02:03:48 pm by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

AlexTP

  • Hero Member
  • *****
  • Posts: 2488
    • UVviewsoft
Re: syncobjs.TEventObject class doesn't work in Ubuntu
« Reply #7 on: December 10, 2023, 02:12:48 pm »
Ok, it returns the handle. User wants to make usage of the same event in 2 processes. He cannot do SetEvent in 1st process so it affects 2nd process. On Unix. He can do it on Windows.

Thaddy

  • Hero Member
  • *****
  • Posts: 16199
  • Censorship about opinions does not belong here.
Re: syncobjs.TEventObject class doesn't work in Ubuntu
« Reply #8 on: December 10, 2023, 06:47:45 pm »
On Nixes, everything is a file as you should know. You can simply use the file API to read or write the event. It is much simpler than you think.
If I smell bad code it usually is bad code and that includes my own code.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5764
  • Compiler Developer
Re: syncobjs.TEventObject class doesn't work in Ubuntu
« Reply #9 on: December 10, 2023, 09:21:29 pm »
You should identify the event by its handle, not its name That is cross platform. Named events are for poor programmers who need holding hands or diapers. Or both.
TEventObject derives from THandleObject which has a readonly property called.... wait for it... Handle...

Across multiple processes you won't have the same handle. If you had read felleroff's post above yours (which you didn't, at least not fully), you'd know that this is what they want. And in that case the name parameter is the one to use. And this functionality is simply not supported on non-Windows.

On Nixes, everything is a file as you should know. You can simply use the file API to read or write the event. It is much simpler than you think.

Not everything is a file. Events for example aren't.

 

TinyPortal © 2005-2018