Is this record queue necessary for TThread? lazarus / 21-General ---------------------------------------------------------------------- cpicanco · 2024-01-27 13:20 I have a zmq PUB-SUB pattern setup to get realtime gaze events from an IPC pipeline. I can receive events with the following TThread.Execute: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TZMQSubThread.Execute;var  zmq_message : zmq_msg_t;   LTopic : string;  LGazeEvent : TGazeEventRecord;   function GazeEventFromPayload(APayload : TSimpleMsgPack) : TGazeEventRecord;  begin    // Result := GetGazeEvent(APayload); // simple pseucode  end; begin  while not Terminated do begin    zmq_message := Default(zmq_msg_t);    zmq_msg_init(zmq_message);    try      // frame 1 is a string      zmq_msg_recv(zmq_message, FSubscriber, 0);      SetString(LPupilTopic, PAnsiChar(zmq_msg_data(zmq_message)), zmq_msg_size(zmq_message));       // frame 2 is MsgPack serialization      if zmq_msg_more(zmq_message) = 1 then      begin        zmq_msg_close(zmq_message);        zmq_msg_init(zmq_message);        zmq_msg_recv(zmq_message, FSubscriber, 0);        FSerializedPayload.Clear; // TMemoryStream        FSerializer.Clear; // TSimpleMsgPack         FSerializedPayload.WriteBuffer(zmq_msg_data(zmq_message)^, zmq_msg_size(zmq_message));        FSerializedPayload.Position := 0;      FSerializer.DecodeFromStream(FPupilPayload);      end;       case LTopic of        'notify' : begin          Queue(procedure begin          FOnNotifyEvent(Self);          end);        end;         'gaze' : begin           // I am considering to make a TThreadSafeQueue for LGazeEvent          // because I am not sure how to pass it by value to Queue          // for example,           LGazeEvent := GazeEventFromPayload(FSerializer);          // FQueue.Enqueue(LGazeEvent);          Queue(procedure begin            FGazeEvent(Self, LGazeEvent);             // or using FQueue          // FGazeEvent(Self, FQueue.Dequeue;);           end);          end;  finally      zmq_msg_close(zmq_message);    end;  end;end;  So, I am considering to make a TThreadSafeQueue for LGazeEvent because I am not sure how to pass it by value to Queue. Is it possible to pass LGazeEvent by value as parameter to Queue? ---------------------------------------------------------------------- Thaddy · 2024-01-27 13:27 TThreadList from generics.collections will do the job. (i.e. package rtl-generics) As far as I understand you correctly. ---------------------------------------------------------------------- cpicanco · 2024-01-27 13:54 Thanks for tip Thaddy. So, I think I could implement the TThreadSafeQueue with TThreadList. But is it really necessary? I am assuming it is because Sven said in the annoucement of anonymous/reference funtions that captured values are passed by reference, not by value. So, I would expect that LGazeEvent could be overriden before consumption. ---------------------------------------------------------------------- Thaddy · 2024-01-27 14:05 See my other answer in a previous thread from today. I expect a single connection to be served without too much if any connections to other connections: i.e. webserver threads should be designed autonomous. If you achieve that, you do not need to bother where it is started, how it is started or when it finishes ( except on intentional program termination) And so you do not need the queue. ---------------------------------------------------------------------- cpicanco · 2024-01-27 14:22 I am not sure which post you are refering to. Would you mind providing a link? Currently, I am using the mentioned TThread.Execute to receive and distribute TGazeEvents. It is really autonomous in the message receive/transport part, ZMQ is excellent for that (for instance, it is an acronym for zero message queue). However, I am not sure how to use zmq to avoid queues for the TGazeEvent distribution/consumption part. I mean, to enqueue events in a non-blocking fashing to be consumed by the main thread. ---------------------------------------------------------------------- cpicanco · 2024-02-01 16:07 So, I end up removing the FQueue.List and using just doing LCurrentData := FCurrentData with critical sections. I found that Queue was enough for the time frame I have.