Vars in TServerClientThread

Hi,

I have encountered the following problem.
I am write a service for windows NT with Delphi 3 C/S. I use some NTService
unit and create a TServerSocket runtime.
I use a Threadblocking methode to spawn new threads for each new connection
to my server. This seems to work fine,
except for the fact that a global variable decleared within th
TServerClientThread wich I want to give the port of the current
connection seems to overwrite the data for this var in all the open threads.
What I mean is that if a second connection is made the variable in the first
connection gets the same value as the variable
in the second (last) connection.

This problem seems to go away when I assign a value to the variable lateron
in the program but that doesn't make verry much
sense to me, because no paticulair thing happen before it that would be able
to solve the problem.

piece of my code:

Unit ClientThread;

type
  POP3Thread = class(TServerClientThread)
...

protected
    procedure Execute; override;
...
implementation
...
var UserPort : integer;
...
procedure POP3Thread.Execute;
begin
UserPort:=ClientSocket.RemotePort;
...(loop for receiving commands follows)
end.

Thanx for anny help in advance,

Martijn Tieland

mtielandAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
rwilson032697Connect With a Mentor Commented:
Thanks mtieland!

Threadvar is used to declare a variable that is in thread local storage. What this means is that every thread will see a different instance of the variable. Generally these kinds of variables are placed into a TThread derived class but sometimes you need to use threadvar...

Cheers,

Raymond.
0
 
kretzschmarCommented:
hi mtieland,

your global-var should be in the implementation section of your unit, if so?

meikl
0
 
mtielandAuthor Commented:
I have put the vars behind the implementation section...

The strange thing is when I assign a value to these vars lateron
in the thread it some to work propperly...
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
mtielandAuthor Commented:
Edited text of question
0
 
kretzschmarCommented:
hi mtieland,

maybe your var should be initialized
try

var userport : Integer = 0; {or OtherNumber}

meikl
0
 
rwilson032697Commented:
Perhaps the variable being overwritten should be declared with ThreadVar?
0
 
mtielandAuthor Commented:
Thanks for the hint on ThreadVar, it works great,
1 question remains dough, what is a ThreadVar kind of a thing??
0
 
mtielandAuthor Commented:
By the way I am new to this expert-exchange thing but i think rwilson should get the points, because he gave the solution to the problem.

Thanx
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.