Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Indyclient socket to read and write at the same time from different threads


This question is answered.


Permlink Replies: 3 - Last Post: Jul 14, 2015 11:48 AM Last Post By: Remy Lebeau (Te...
Adrian Bors

Posts: 12
Registered: 3/25/13
Indyclient socket to read and write at the same time from different threads  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 13, 2015 10:52 AM
Hi,

I am writing an application to read data from the PLC at every 1 second interval and write data to PLC when user initiated with mouse click event.

I decided to create two threads and they are PlcRead and PlcWrite threads.

PlcRead is non-terminated thread and it is continuously read data from the PLC at every 1 second interval.

PlcWrite is Event based thread, It starts with mouse click event and terminates after write to the PLC.

I would like to use single TIdTCPClient component on both threads with Synchronise(). But, I am not sure whether it is correct or not ?

My doubt is, What happens when both read and write threads socket perform at same instance, ?
Does it have any data mismatch? or Do they act like different sockets?

Looking forward to hear from you.

Thanks in advance.
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Indyclient socket to read and write at the same time from differentthreads
Correct
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 13, 2015 1:23 PM   in response to: Adrian Bors in response to: Adrian Bors
Adrian wrote:

PlcWrite is Event based thread, It starts with mouse click event and
terminates after write to the PLC.

Why terminate it? Why not just put the events into a queue and make the
thread sleep when the queue is empty, and wake up when new events are ready
to be sent?

I would like to use single TIdTCPClient component on both threads

That is fine.

with Synchronise().

You don't need to sync the two threads.

My doubt is, What happens when both read and write threads socket
perform at same instance, ?

That is perfectly fine. A socket has separate buffers for inbound and outbound
data. One thread can handle one buffer, while another thread handles the
other buffer.

Does it have any data mismatch?

No.

or Do they act like different sockets?

Essentially, yes.

You only run into problems when 2+ threads are reading on the same socket
at the same time, or 2+ threads are writing to the same socket at the same
time. In those cases, you have to synchronize the threads to avoid overlap
issues.

--
Remy Lebeau (TeamB)
Adrian Bors

Posts: 12
Registered: 3/25/13
Re: Indyclient socket to read and write at the same time from differentthreads  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 14, 2015 1:12 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Hi Remy Lebeau,

Thank you very much for your answer.


Adrian wrote:

PlcWrite is Event based thread, It starts with mouse click event and
terminates after write to the PLC.

Remy Lebeau (TeamB) wrote:
Why terminate it? Why not just put the events into a queue and make the
thread sleep when the queue is empty, and wake up when new events are ready
to be sent?

If possible, could you please explain more with an example? Sorry for troubling you.


My doubt is, What happens when both read and write threads socket
perform at same instance, ?

That is perfectly fine. A socket has separate buffers for inbound and outbound
data. One thread can handle one buffer, while another thread handles the
other buffer.

On PLCRead thread: write an array of data(Modbus address, function code, no.Of registers etc) to the PLC and then read back requested data from the PLC.
On PLCWrite thread: write an array of data, which contains Modbus address, function code, registers count and Data to write etc.

Does they have data mismatch when both of them run at same instance?
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Indyclient socket to read and write at the same time fromdifferentthreads  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 14, 2015 11:48 AM   in response to: Adrian Bors in response to: Adrian Bors
Adrian wrote:

If possible, could you please explain more with an example? Sorry for
troubling you.

There are tons of tutorials/books/blogs/etc available on how too write efficient
multi-threaded queues.

On PLCRead thread: write an array of data(Modbus address, function
code, no.Of registers etc) to the PLC and then read back requested
data from the PLC. On PLCWrite thread: write an array of data, which
contains Modbus address, function code, registers count and Data to
write etc.

Does they have data mismatch when both of them run at same instance?

Potentially yes, because you have two threads reading, and two threads writing.
The PLCWrite thread should be doing ALL of the writing and nothing else,
and the PCLRead thread should be doing ALL of the reading and nothing else.

--
Remy Lebeau (TeamB)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02