Watch, Follow, &
Connect with Us

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


Welcome, Guest
Guest Settings
Help

Thread: I need two of the same actions to happen at the same millisecond


This question is answered.


Permlink Replies: 5 - Last Post: Nov 10, 2017 9:23 AM Last Post By: John Gray
Kevin Morris

Posts: 52
Registered: 1/8/13
I need two of the same actions to happen at the same millisecond  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 9, 2017 8:28 AM
I need to build a scientific app (Windows .exe) that has to perform the same action, more than once, at the exact same time. Let's say the "same action" is that I want to display a count of 1 to 50, incrementing once per second, and display that on-going count in a TLabel. I'm using Delphi XE3 and Delphi XE7.

There is no limitation how to do this. I can use multiple running instances of the same .exe, or I can use one instance of the .exe with multiple-threads.

I think that if multi-threading in one .exe is used, then there would have to be some way to tell the first thread to use CPU Core 1 and the second thread to use CPU core 2, to avoid the situation where CPU Core 1 is used for both threads and then the execution of the threads would be sequential.

If multiple instances of the same .exe is used, I have the same CPU Core usage problem/situation as above, plus, I have to ensure somehow that both instances of the .Exe are triggering their execution at the same time (exact millisecond). Meaning, each instance displays "1" at the same time, then "2" at the same time. I don't want the count to start when the .exe is started, or when I happen to click a "Start" button. If I start one instance, it will already be on "2" by the time I start the second instance, so if a timer is used then the second instance will always be a second or two BEHIND the first instance, so they'll be forever out of sequence.

Assume I'm a beginner so I'll need code snippets as examples.

Maybe this is not possible due to the way the Windows OS works, or the way the CPU marshals requests.
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: I need two of the same actions to happen at the same millisecond
Correct
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 9, 2017 9:47 AM   in response to: Kevin Morris in response to: Kevin Morris
Kevin Morris wrote:

I need to build a scientific app (Windows .exe) that has to perform
the same action, more than once, at the exact same time.

Good luck with that. Windows is not a real-time system.

Let's say the "same action" is that I want to display a count of 1 to
50, incrementing once per second, and display that on-going count in a
TLabel. I'm using Delphi XE3 and Delphi XE7.

Any given thread can only do one thing at a time. That includes the
main UI thread, which must be the only thread that touches the UI
directly. You can have multiple threads doing the actual counting, but
they will have to delegate their results to the main UI thread for
display.

There is no limitation how to do this. I can use multiple running
instances of the same .exe

That doesn't really solve anything, since multiple processes still have
to be scheduled with CPU time, just as multiple threads in a single
process do. It doesn't gain you anything to use multiple processes.

or I can use one instance of the .exe with multiple-threads.

I would probably opt for this. But it really depends on the nature of
the app and the work it has to do.

I think that if multi-threading in one .exe is used, then there would
have to be some way to tell the first thread to use CPU Core 1 and
the second thread to use CPU core 2, to avoid the situation where CPU
Core 1 is used for both threads and then the execution of the threads
would be sequential.

The Win32 API has many functions for that very purpose:

GetSystemInfo()
GetLogicalProcessorInformation/Ex()
GetActiveProcessorCount()
(Get|Set)ThreadAffinityMask()
(Get|Set)SetTheadGroupAffinity()
(Get|Set)ThreadIdealProcessor/Ex()

Just to name a few...

But even if you manage to make both threads run in true parallelism on
the hardware, they still have to serialize access to the UI.

If multiple instances of the same .exe is used, I have the same CPU
Core usage problem/situation as above, plus, I have to ensure somehow
that both instances of the .Exe are triggering their execution at the
same time (exact millisecond).

You are not likely to get millisecond precision in anything you come up
with. Windows simply doesn't operate that fast.

If I start one instance, it will already be on "2" by the time I
start the second instance

Unless you synchronize them.

so if a timer is used then the second instance will always be a second
or two BEHIND the first instance, so they'll be forever out of
sequence.

Does it really matter? In the example you described, the main UI
thread needs time to fire timer events, or process notification
messages, so as long as the two counting threads provide their current
results to the main UI thread at roughly the same time, within the
period between UI events, there will be no perceived difference in
time.

Even if you have multiple threads in a single process, you are still
going to have timing issues. Creating new threads takes time and
resources. By the time you start the second thread, the first thread
will already be running.

You could try creating an event object (Win32 API CreateEvent()
function, or Delphi's TEvent class) that is initially unsignaled, and
then start all of the threads and have them wait on that event, and
then signal the event once all of the threads are waiting. But even
then, Windows still has to schedule time to release each thread
individually from its wait, so the threads are not going to run at
EXACTLY the same time.

And then there is the issue of task switching between threads on the
same CPU core. You can't monopolize an entire CPU core just for
yourself. Most apps don't modify thread affinities, so Windows is
going to use every CPU core available to it to run other threads in the
system, no matter what you do in your app's code. And task switches
introduce delays.

The only way (I know of) to circumvent the thread scheduler is to use
fibers, which are like user-scheduled threads. But you will still have
task switches in the thread that manages the fiber, so you are not
really gaining anything towards the level of precision you want.

Assume I'm a beginner

But you are asking about a very advanced topic.

--
Remy Lebeau (TeamB)
Kevin Morris

Posts: 52
Registered: 1/8/13
Re: I need two of the same actions to happen at the same millisecond  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 9, 2017 9:56 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Thanks Remy!

Very detailed answer!
Alex Belo

Posts: 626
Registered: 10/8/06
Re: I need two of the same actions to happen at the same millisecond
Helpful
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 10, 2017 6:53 AM   in response to: Kevin Morris in response to: Kevin Morris
Kevin Morris wrote:

I need to build a scientific app (Windows .exe) that has to perform
the same action, more than once, at the exact same time.

Define "exact same time". What about quantum limit (Heisenberg
uncertainty relation) ? ;-)

Let's say the "same action" is that I want to display a count of 1 to
50, incrementing once per second, and display that on-going count in a
TLabel.

Why not create offscreen bmp with all needed details and show it in
TImage as a whole? But even in this case simultaneity will not be
perfect: deflection of display is not momentary so different parts of
the image will be displayed at different moments.

--
Alex
Kevin Morris

Posts: 52
Registered: 1/8/13
Re: I need two of the same actions to happen at the same millisecond  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 10, 2017 6:58 AM   in response to: Alex Belo in response to: Alex Belo
Good points Alex. Thanks.
John Gray

Posts: 15
Registered: 10/3/99
Re: I need two of the same actions to happen at the same millisecond
Helpful
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 10, 2017 9:23 AM   in response to: Kevin Morris in response to: Kevin Morris
Kevin Morris wrote:
I need to build a scientific app (Windows .exe) that has to perform the same action, more than once, at the exact same time. Let's say the "same action" is that I want to display a count of 1 to 50, incrementing once per second, and display that on-going count in a TLabel. I'm using Delphi XE3 and Delphi XE7.
[...]

Your minimalist description is a bit confusing. However the words "same action" and "same time" suggest you might be dealing with events/actions in the outside world in some way. Does this fit?

If this is the case, you want external hardware that's capable of accurate multi-channel timing and counting. Configuring and controlling the hardware could be done from the computer. The hardware would feed the data back to the computer for display and recording.
A couple of sources: Mikroelectronica -- www.mikroe.com and National Instruments -- www.ni.com

hth,
John

Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02