Newsgroups : Borland : borland.public.delphi.nativeapi.win32 : 2008 Jun : Thread issues ---- Completely stuck

www.cryer.info
Managed Newsgroup Archive

Thread issues ---- Completely stuck

Subject:Thread issues ---- Completely stuck
Posted by:"Anthoni Gardner" (no..@nono.com)
Date:Tue, 24 Jun 2008 17:40:25

Hi,

I am wanting to try and implment a thread safe list of user objects for my
server. Basically users will connect and then can send messages and other
commands so the software does certain things it needs to.

For example 4 users connect:-
'anthoni', 'emerald', 'martin', 'janet'

Now as there is no direct socket comms, all communication goes through the
server and is stored, then user gets the data at set intervals. So when
'anthoni' sends a request to 'martin' via the server, the request will be
stored into user object 'anthoni' (either on disk or in db), then when the
'anthoni's connection pings the server it returns that data.

Now I am having immense trouble getting this so it is threadsafe. I was
using one central list to manage them and this works perfect on a single
core processor, but falls apart on anything greater. Hence my wish to create
a Thread safe user list. Basically what I want ideally is to do the
following pseudo code.

Connection Event for 'martin'
With UserList.User['anthoni'] Do
Begin
    Lock;
    Try
        WriteCommand('mycommand');
    Finally
        Unlock;
    End;
End;

Connection Ping from server for 'anthoni'
With UserList.User['anthoni'] Do
Begin
    Lock;
    Try
        ReadCommand;
    Finally
        Unlock;
    End;
End;

Now I know this is not feasible because User is not a static object and thus
can not be protected from access. I need to have a global list that stores
the currently active users, the thing is I do not have a clue how to stop
one thread from accessing the user object while another has got hold of it.
I know I need to make the thread wait, but dont know how. Inside the user
object I can implement a Producer-Consumer Queue (as written by Martin
James) and I understand the basics of this. What I am struggling with, is
how to prevent concurrent access to the user objects.

Any assitance on this subject, or even suggestions on a better method of
doing it, would be greatly appreciated.

Regards
Anthoni

Replies:

www.cryer.info
Managed Newsgroup Archive