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

Write a Multi-Threaded UDP Server that welcomes UDP client
VBa Looping through RecordSets is too slow for my program
Is it possible to force resume a sleeping thread?
C# Win Form - Main thread not responding when using background worker
Not be able to show images in C++/CLI
Scala - multithreading, finish main thread when any child thread finishes
QT - C++ / Running a worker thread within idle phase of a periodic event
Technical Differences Between Service and Web Workers
Elasticsearch 5.2 unit tests
Django plus Celery/RabbitMQ with threading for C socket module
How to stop threads, once there is no more data in Chan to process?
How to wrap multi-threaded callbacks in Rust?
Spawn Expect from a perl thread
How can I return a JoinHandle from a function?
Forcing a message loop to yield
Weird callstack leading up to CreateRemoteThread

Categories

HOME
yii2
openmp
reserved
mfc
iterator
dot
infragistics
pheatmap
contact
applepay
primary-key
user-input
dax
export-to-csv
quickbooks
resize
collectd
google-cloud-spanner
decimal
progressive-web-apps
django-admin
apache-metamodel
solaris-10
lucene.net
autocad-plugin
scriptcs
jasonette
intentfilter
sylius
facebook-access-token
bootstrap-duallistbox
traffic
fopen
typo3-6.2.x
large-file-upload
nouislider
nameservers
javascriptcore
yadcf
siesta-swift
google-sites-2016
perlin-noise
xmlreader
xcode-extension
hot-module-replacement
mixture-model
android-browser
az-application-insights
splice
sas-visual-analytics
ensembles
pdf-reactor
gulp-sourcemaps
angular2-meteor
xml-documentation
azure-application-gateway
paxos
domain-model
hls.js
sqlclient
magma
cudafy.net
jspdf-autotable
hittest
chord-diagram
or-tools
specrun
visual-c++-2008
intrusion-detection
asp.net-4.5
mongocsharpdriver
qgraphicsview
php-parse-error
design-by-contract
sdhc
collapse
interrupted-exception
mono-embedding
processmodel
angular-local-storage
streambase
e10s
camanjs
java-metro-framework
onactivityresult
navigationservice
poller
cloud-connect
terminal-services
oam
ms-project-server-2010
inbox
apc
ruby-datamapper
dotnetnuke-5
html4
sqlperformance
mhtml
path-separator
frameset
manchester-syntax
labwindows
twrequest
parametric-equations
uiviewanimation-curve
dotnethighcharts
adk
genshi
android-sdk-2.1
gin
ctp4
gacutil
data-acquisition

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App