Threading safety question

Giganews Newsgroups
Subject: Threading safety question
Posted by:  Simon A. (saberu…@tecnar-automation.com)
Date: Wed, 14 Sep 2005

I have a client/server application that I want to use to exchange record
between the client and server ). I want both the client and server being
able to send a record the the other side at anytime (bidirectionnal
communication).

This is how I'm doing it :

Execute method of server

try
    AThread.Connection.ReadBuffer(rcvBuffer, sizeof (rcvBuffer));
    //process data
except
    ...
end;

Thread object Exceute method of the client

try
    AThread.Connection.ReadBuffer(rcvBuffer, sizeof (rcvBuffer));
    //process data
except
    ...
end;

For sending data, the server side

    lList := server .Threads.LockList;
    try
      TIdPeerThread(lList.Items[0]).Connection.Writebuffer(rcvBuffer,
sizeof (rcvBuffer));
    finally
        server.Threads.UnlockList;
    end;

For the client I use only

client.WriteBuffer(rcvBuffer, sizeof (rcvBuffer));

This thread object is owned  by another thread.  It seems very simple and
straightforward, but my principal question if this is safe to use directly
the writebuffer method of the client from another thread (with readbuffer
method called from the thread that owned the client at the same time).  Is
there any problem possible?

I'm testing it right now, and it seems to work but I want to be sure this is
a correct way to do it. This will be part a distributed application that try
to be the more real-time as possible, so it is very important that I do not
get side effects. If it is not, can you tell me what is the best way of
implementing it?

Thank you

Simon

Replies