0

I just learned that creating and using a Vcl.TTimer from a worker thread is not thread safe. My worker thread works fine 'most of the time' using the Vcl.TTimer. I found a class TTimerThread based on events which works fine also, but I don't know how to combine both classes that TTimerThread has access to the methods of my worker thread.

ExecuteTimed() will be called when the event occurs (3000ms).

Pseudo code:

uses TimerThread;

type
  TMyTimerThread = class( TTimerThread )
  protected
  procedure ExecuteTimed; override;
[...]



TMQTTClient = class(TThread)
private
  [...]
  fKeepAliveTimer:TTimer;                     // Works, but is ugly.
  fKeepAliveTimerThread:TMyTimerThread;       //  
  [...]


procedure TMyTimerThread.ExecuteTimed;
begin
  beep;   // Works fine!
end;

[...]

constructor TMQTTClient.Create(aHostname: string; aPort: integer; ClientID:String;aKeepAliveSeconds : word);
begin
  //init some stuff...
  fKeepAliveTimer:=TTimer.Create(nil); // ugly
  fKeepAliveTimer.Interval:=fKeepAliveSeconds * 1000;
  fKeepAliveTimer.OnTimer:=DoKeepAlive; // The function pointer which should be called ~ every xxxx ms. Works fine, but no thread safe.

  fKeepAliveTimerThread:=TMyTimerThread.Create();
  fKeepAliveTimerThread.Interval:=3000;
  //fKeepAliveTimerThread.ExecuteTimed := ?; //here I wish to assign a pointer to the ExecuteTimed method of MyTimerThread.

I could imagine solving it using TThread.Synchronize() in ExecuteTimed() which calls the instance of the worker thread like frmMain.myMqttWorkerClass.DoKeepAlive, but I am in doubt if this is the right way.

6
  • Perhaps you should explain a bit about your overall goal with this. I'm not familiar with TTimerThread, but I think it can be assumed that ExecuteTimed and Execute of your TMQTTClient thread are distinct threads, so if you want to access the same data structures from both you will need sync locking of some kind, e.g. critical sections, to guard access to the shared resources. Commented Aug 6 at 17:34
  • TThread.Synchronize() is used for execute code in the main thread actually. Commented Aug 6 at 17:43
  • TTimerThread has indeed a call to criticalSection and it works. It is more a basic question about how to tell TMqttClient that the TTimerThread event has occurred.
    – Sascha Ott
    Commented Aug 6 at 17:45
  • Did you consider TThread.Sleep method or GetTime polling ? Commented Aug 6 at 17:51
  • @Stas No. sleep(60000) and polling feels ugly, too.
    – Sascha Ott
    Commented Aug 6 at 17:55

1 Answer 1

2

You can add a public event to TMyTimerThread and have TMQTTClient assign your DoKeepAlive handler to it, just like you did with the TTimer, eg:

type
  TMyTimerThread = class(TTimerThread)
  private
    fOnTimer: TNotifyEvent;
  protected
    procedure ExecuteTimed; override;
  public
    property OnTimer: TNotifyEvent read fOnTimer write fOnTimer;
  end;

  TMQTTClient = class(TThread)
  private
    [...]
    fKeepAliveTimerThread: TMyTimerThread;
    procedure DoKeepAlive(Sender: TObject);
    [...]
  end;

...

procedure TMyTimerThread.ExecuteTimed;
begin
  if Assigned(fOnTimer) then fOnTimer(Self);
end;

...

constructor TMQTTClient.Create(aHostname: string; aPort: integer; ClientID:String;aKeepAliveSeconds : word);
begin
  //init some stuff...
  fKeepAliveTimerThread := TMyTimerThread.Create();
  fKeepAliveTimerThread.Interval := 3000;
  fKeepAliveTimerThread.OnTimer := DoKeepAlive;
end;

Just keep in mind that DoKeepAlive will be called in the context of the timer thread, so make sure that anything it does is thread-safe.

5
  • Yes, using TNotifyEvent somehow feels the right way. DoKeepAlive() just checks if TCPClient.Connected, do some TBytes stuff and finally calls FTCPClient.IOHandler.write(). Think this is safe
    – Sascha Ott
    Commented Aug 6 at 18:11
  • Works like a charm! :) Thx Remy!
    – Sascha Ott
    Commented Aug 6 at 18:21
  • @SaschaOtt Connected performs a read operation, so you have to be very careful with it, if you have other threads that are also reading from the same socket. Same with Write, for that matter. A thread can safely write while another thread reads, but if 2+ threads do reads, or 2+ threads do writes, then you need to synchronize access to the socket accordingly. If TMQTTClient.Execute does any writing, then I would suggest letting TMQTTClient.Execute do ALL reads and writes. Have DoKeepAlive push a notification into a queue that TMQTTClient.Execute polls from and writes when needed. Commented Aug 6 at 18:38
  • Indeed TMQTTClient.Execute writes/reads to/from the socket. In fact all read/write operations executed there using a signaling variable. Except the keep alive msg which is fired from the timer. Thx for the hint.
    – Sascha Ott
    Commented Aug 6 at 18:59
  • @SaschaOtt Then it may make sense to have the timer signal another variable that Execute looks at and then writes the keep alive msg when signaled Commented Aug 6 at 19:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.