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
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?