Skip to content

Clearing up TEvent TRTLEvent and BasicEvent

Summary

Following this forum topic it seems TRTLEvent has limited properties about ManualReset. BasicEvent has the options but documentation notes these are obsolete and should not be used. TEvent seems working fine, but it is based on BasicEvent!

The situation seems in need of clarification, code and/or documentation wise.

Example Project

Here is the original problem: Documentation says "RTLeventSetEvent notifies other threads which are listening, that the event has occurred.", but in my tests, RTLEventSetEvent activates only one other waiting thread, unless that waiting thread calls RTLEventSetEvent again. Is there a way to activate all (waiting threads for an event) at the same time?

program Project1;
 
uses
  Classes,
  SysUtils,
  MTProcs;
 
  procedure Activate(Data: Pointer);
  var
    E: PRTLEvent absolute Data;
  begin
    Sleep(1000);
    RTLEventSetEvent(E);
  end;
 
  procedure Run(Index: PtrInt; Data: Pointer; Item: TMultiThreadProcItem);
  var
    E: PRTLEvent absolute Data;
  begin
    RTLEventWaitFor(E);
    RTLEventSetEvent(E); //Without this line, only 1 will continue to WriteLn
    WriteLn(Index);
  end;
 
var
  E: PRTLEvent;
begin
  E := RTLEventCreate;
  TThread.ExecuteInThread(@Activate, E, nil);
  ProcThreadPool.DoParallel(@Run, 1, 4, E);
  RTLEventDestroy(E);
  ReadLn;
end.                                
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information