I have a real-time simulation where the logic flows most easily with a busy
while loop...
while (continueFlagSet)
{
if (EventDue)
ProcessNextEvent
}
Now, is there any thing to be concerned about here? With older versions of
Windows, I wouldn't have trusted the operating system to share the processor,
so I would have put an "else Sleep(0)" at the bottom of the loop, but this is
2005, and shouldn't I leave it to the operating system to manage processes,
and threads, without any hints from me?
When i'm running the application, there isn't any obvious impact on my other
Windows apps, or other threads in the same app, but I haven't tested this
thoroughly.
I am mindful of an excellent essay I read where the author listed "Sleep" as
one of the common sources of problems in windows apps, and which is almost
never needed. http://www.flounder.com/badprogram.htm 5 2242
wait on event, then I don't see an issue. continueFlagSet should be a
another event (i.e. reset event) or syncd when set and tested if set from
another thread.
--
William Stacey [MVP]
"Javaman59" <Ja*******@discussions.microsoft.com> wrote in message
news:BD**********************************@microsof t.com... I have a real-time simulation where the logic flows most easily with a busy while loop...
while (continueFlagSet) { if (EventDue) ProcessNextEvent }
Now, is there any thing to be concerned about here? With older versions of Windows, I wouldn't have trusted the operating system to share the processor, so I would have put an "else Sleep(0)" at the bottom of the loop, but this is 2005, and shouldn't I leave it to the operating system to manage processes, and threads, without any hints from me?
When i'm running the application, there isn't any obvious impact on my other Windows apps, or other threads in the same app, but I haven't tested this thoroughly.
I am mindful of an excellent essay I read where the author listed "Sleep" as one of the common sources of problems in windows apps, and which is almost never needed. http://www.flounder.com/badprogram.htm
Thanks...
A sloppy example on my part. "if (EventDue)" is referring to a logical
event, not a system event which I can wait on. eg, in a movie playing system,
it would be "if (next frame due)". I'm processing up to 200 events per
second. I don't think I can afford the overhead of creating system events,
and waitng on them.
Yes, continueFlagSet is handled with Interlocked, which I think does the job
of synchronization.
Javaman
"William Stacey [MVP]" wrote: wait on event, then I don't see an issue. continueFlagSet should be a another event (i.e. reset event) or syncd when set and tested if set from another thread.
-- William Stacey [MVP]
"Javaman59" <Ja*******@discussions.microsoft.com> wrote in message news:BD**********************************@microsof t.com...I have a real-time simulation where the logic flows most easily with a busy while loop...
while (continueFlagSet) { if (EventDue) ProcessNextEvent }
Now, is there any thing to be concerned about here? With older versions of Windows, I wouldn't have trusted the operating system to share the processor, so I would have put an "else Sleep(0)" at the bottom of the loop, but this is 2005, and shouldn't I leave it to the operating system to manage processes, and threads, without any hints from me?
When i'm running the application, there isn't any obvious impact on my other Windows apps, or other threads in the same app, but I haven't tested this thoroughly.
I am mindful of an excellent essay I read where the author listed "Sleep" as one of the common sources of problems in windows apps, and which is almost never needed. http://www.flounder.com/badprogram.htm
> Yes, continueFlagSet is handled with Interlocked, which I think does the job of synchronization.
Yes it does. As to locking (side issue), on my 1.86GH PMobil laptop, I can
get about 27 Million locks per second including an increment operation (per
test below). In most cases, the lock overhead is not going to be a
bottleneck for your app. It is too bad that the documentation seems to make
a big deal out of lock overhead, because if done right, is not really an
issue in most cases. So in terms of your 200 work items per second
requirement, locking overhead is almost nothing. Naturally, you could have
locking bugs, but that is another issue. Interlocked should be fine for
your int however.
object sync = new object();
Stopwatch sw = new Stopwatch();
sw.Start();
for ( int i = 0; i < 27000000; )
{
lock ( sync )
{
i++;
}
}
sw.Stop();
Console.WriteLine("Sec:{0} ms:{1}", sw.Elapsed.Seconds,
sw.Elapsed.Milliseconds);
--
William Stacey [MVP]
Thanks William, that's very interesting. I'll never worry about the time for
getting a lock again.
"William Stacey [MVP]" wrote: Yes, continueFlagSet is handled with Interlocked, which I think does the job of synchronization.
Yes it does. As to locking (side issue), on my 1.86GH PMobil laptop, I can get about 27 Million locks per second including an increment operation (per test below). In most cases, the lock overhead is not going to be a bottleneck for your app. It is too bad that the documentation seems to make a big deal out of lock overhead, because if done right, is not really an issue in most cases. So in terms of your 200 work items per second requirement, locking overhead is almost nothing. Naturally, you could have locking bugs, but that is another issue. Interlocked should be fine for your int however.
object sync = new object(); Stopwatch sw = new Stopwatch(); sw.Start(); for ( int i = 0; i < 27000000; ) { lock ( sync ) { i++; } } sw.Stop(); Console.WriteLine("Sec:{0} ms:{1}", sw.Elapsed.Seconds, sw.Elapsed.Milliseconds);
-- William Stacey [MVP]
"William Stacey [MVP]" <st*****@mvps.org> wrote in message
news:eC**************@TK2MSFTNGP15.phx.gbl... Yes, continueFlagSet is handled with Interlocked, which I think does the job of synchronization.
Yes it does. As to locking (side issue), on my 1.86GH PMobil laptop, I can get about 27 Million locks per second including an increment operation (per test below). In most cases, the lock overhead is not going to be a bottleneck for your app. It is too bad that the documentation seems to make a big deal out of lock overhead, because if done right, is not really an issue in most cases. So in terms of your 200 work items per second requirement, locking overhead is almost nothing. Naturally, you could have locking bugs, but that is another issue. Interlocked should be fine for your int however.
object sync = new object(); Stopwatch sw = new Stopwatch(); sw.Start(); for ( int i = 0; i < 27000000; ) { lock ( sync ) { i++; } } sw.Stop(); Console.WriteLine("Sec:{0} ms:{1}", sw.Elapsed.Seconds, sw.Elapsed.Milliseconds);
-- William Stacey [MVP]
While I agree that taking a lock is not that expensive when there is no
contention, it's still not the right thing to do when all you need is
perform an atomic thread-safe operation like incrementing or decrementing a
variable.
Taking a lock is still 20 times slower than a Interlocked.Increment(ref i);
Willy. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Paul L. Du Bois |
last post by:
Has anyone written a Queue.Queue replacement that avoids busy-waiting?
It doesn't matter if it uses os-specific APIs (eg
WaitForMultipleObjects). I did some googling around and haven't found...
|
by: Philipp Lenssen |
last post by:
Using Firefox and this Reversi script
http://gamesforthebrain.com/game/reversi/ , I often get "A script on
this page may be busy ..." warning message. I suspect to be the
recursive min-max...
|
by: vul |
last post by:
I need to copy the file (OutboxLog.txt produced by Fax service) right after
it was updated. I'm using:
System.IO.File.Copy(strSource, strDestination, True) to do that, but if the
source file is...
|
by: mike3 |
last post by:
Hi.
Busy-waiting is a known anti-pattern that should be avoided. However,
in C,
there is no standard alternative, so when a wait is required and it's
not busy,
the program becomes 100%...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |