Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Vars in TServerClientThread

Posted on 1998-10-22
8
Medium Priority
?
502 Views
Last Modified: 2010-04-06
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

0
Comment
Question by:mtieland
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1343862
hi mtieland,

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

meikl
0
 

Author Comment

by:mtieland
ID: 1343863
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
 

Author Comment

by:mtieland
ID: 1343864
Edited text of question
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 27

Expert Comment

by:kretzschmar
ID: 1343865
hi mtieland,

maybe your var should be initialized
try

var userport : Integer = 0; {or OtherNumber}

meikl
0
 
LVL 12

Expert Comment

by:rwilson032697
ID: 1343866
Perhaps the variable being overwritten should be declared with ThreadVar?
0
 

Author Comment

by:mtieland
ID: 1343867
Thanks for the hint on ThreadVar, it works great,
1 question remains dough, what is a ThreadVar kind of a thing??
0
 

Author Comment

by:mtieland
ID: 1343868
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
 
LVL 12

Accepted Solution

by:
rwilson032697 earned 400 total points
ID: 1343869
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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question