Jump to content
Sonjli

Unknown thread inside IOmniTask

Recommended Posts

Hi,

I have this case where one OmniTask run some external code from a delphi library\Component that uses internally a TThread. This thread can't be accessed from outside.

This external library have a class which listen for an incoming request and when this happens then it run a "onReceive" event. Something like a listening socket.

 

One OmniTask "run" one and only one of these objects.

 

Have I to pay attention when the "inside-thread" have to use write-resources of the Omnitask?

 

For example when the "inside-thread" need to use the logger of the OmniTask?

 

Some pseudo code:

   CreateTask(
      procedure(const task: IOmniTask)
      var
         lBroadcaster: IBroadcastManager;
         lLogger: ILogWriter;
      begin
      [...]
      task.Lock.Acquire;
      try
          lLogger.Log(TLogType.Info, 'Testing...', 'TEST');
      finally
          task.Lock.release;
      end;
[...]
      // lBroadcaster is the object using an internal tthread
      lBroadcaster.OnReceive := procedure(ABroadcastMessage: IBroadcastMessage)
                  begin
                     task.Lock.Acquire;
                     try
                        try
                           lLogger.Log(TLogType.Info, 'Parsing...', 'TEST');
                           [...]
                        except
                           on E: Exception do
                           begin
                              lLogger.Log(TLogType.Error, 'Exception (%s): "%s"', [E.ClassName, E.Message], 'TEST');
                           end;
                        end;
                     finally
                        task.Lock.release;
                     end;
                  end;
               lBroadcaster.Connect('127.0.0.1', 'guest', 'guest', True);
).SetParameter([...])  // All my stuff injiections
.WithLock(CreateOmniCriticalSection)
.Run;

 

Is the locking mechanism used in the right way?

 

Thanks

Eddy

 

 

 

Share this post


Link to post

It depends on how lBroadcaster's OnReceive callback is implemented. If that event is called in the context of the "inside thread", then yes - this locking is necessary and it is correctly implemented.

  • Thanks 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×