multithreading


How to access thread and its components?


I create a thread
type
ss_thread = class;
ss_thread = class(TThread)
protected
Fff_id : string;
Fff_cmd : string;
Fff_host : string;
Fff_port : TIdPort;
procedure Execute; override;
public
constructor Create(const ff_id, ff_cmd: string; ff_host: string; ff_port: TIdPort);
end;
constructor ss_thread.Create(const ff_id, ff_cmd: string; ff_host: string; ff_port: TIdPort);
begin
inherited Create(False);
Fff_id := ff_id;
Fff_cmd := ff_cmd;
Fff_host := ff_host;
Fff_port := ff_port;
end;
...
id := 123;
...
nst_ss_thread.Create(id, cmd, host, port);
and doing something on
procedure ss_thread.Execute;
var
ws : TIdTCPClient;
data : TIdBytes;
i : integer;
list : TList;
begin
ws := TIdTCPClient.Create(nil);
ws.Host := Fff_host;
ws.Port := Fff_port;
....
How to access this thread 'ws' variable thru another thread using id:=123 of thread ?
Thanks
It cannot.
You've declared ws as a local variable inside ss_thread.execute, which means it's only visible there. It can't be seen outside ss_thread.execute, even by other parts of ss_thread.
If you want it visible from other places or threads, you need to move it to a more visible scope. For instance, if you want it visible from other places in ss_thread, move it to the interface declaration in private or protected sections, and if you want it visible from outside ss_thread move it to the published or public sections.
You'd better not. Thread objects are exactly made to insulate its variables from other threads.
Otherwise all kind of random non-reproducible errors would appear - http://en.wikipedia.org/wiki/Heisenbug
Parallel programming should have very clear separation and insulation. Because You can never predict the timing of execution and which statement would run earlier and which one later.
Imagine that easy scenario:
ws := TIdTCPClient.Create(nil);
ws.Host := Fff_host;
// at this point another thread gets access to ws variable,
// as You demanded - and changes it, so WS gets another value!
ws.Port := Fff_port;
How would you detect such a bug, if it happens only on client multi-processor computer under heavy load once a month ? In your workstation during debug sessions or simulation it would not be reproduced ever! How would you catch it and fix ?
As a rule of thumb, when doing parallel programming the data should be spleat into "shared immutable" and "private mutable" pieces, and when doing inter-thread communication you should - similar to inter-process communications - make some events/messages queue and pass commands and replies to/from threads, like it is done in Windows GDI or like in MPI
Then you thread would fetch "change ws variable" command from queue - in the proper moment when the change is allowed - and change it from inside. Thus you would assume control and assure that variables are only changed in that point and in that manner, that would not derail the code flow.
I suggest you to read OTL examples to see how inter-thread communication is done in more safe way that direct access to objects. http://otl.17slon.com/tutorials.htm

Related Links

what is the best way to stop a thread in java?
Does Linux drop into the kernel on all cores?
Performance counters for current threads
winsock 2. thread safety for simultaneous send's. tcp
How to implement cancellable worker thread
Do I need to lock object when reading from it?
A Thread Scheduler for an Real Time Embeded Operating System
What does threadsafe mean?
Threads on WindowsXP
Reading log from Log4Net generated by multiple threads
Heavy weight and light weight thread
What operations are thread-safe on std::map?
Analyzing output of !threadpool and !threads in windbg
Copying files which the main thread adds to a stringlist using a thread
what is the difference between synchronous apis and asynchronous apis?
How to define threadsafe?

Categories

HOME
cloud
server
push-notification
smarty
objectgears
cplex
programming-languages
directx
maude-system
communication
qt-creator
fallback
quickfix
hapi
caml
workload-scheduler
worldwind
django-admin
normalizr
sms-gateway
riot.js
orleans
csrf-protection
dcevm
sqlcipher
social-media
publish
replaceall
wtx
jspm
jaxb2
objectanimator
procdump
srcset
bpel
madlib
bcd
javascriptcore
catch-all
webtest
mpmediaquery
serve
swisscomdev
hockeyapp
document.write
streamreader
mapzen
outlook-api
websphere-mq-fte
cubic-spline
avconv
flickr-api
scrollable
youcompleteme
font-size
flow-control
nbconvert
redundancy
watchconnectivity
android-textview
plottable.js
multiple-regression
skype4py
hill-climbing
dlna
dataview
independentsoft
energy
xna-4.0
execute
collapse
security-testing
dukescript
drawbitmap
ideamart
csquery
device-manager
bitcoinj
heisenbug
comexception
kgdb
balanced-payments
monomac
dexterity
listings
back-stack
qt-faststart
orchardcms-1.7
ember-app-kit
free-variable
angularjs-controller
gdata-api
first-responder
multipage
windows-phone-7.1.1
postgresql-performance
hgsubversion
file-comparison
yui-datatable
cxxtest
microsoft-virtualization
xetex
simpletest
spec#
signal-handling

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App