473,385 Members | 1,311 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Is there any reason to avoid a busy while loop?

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
Nov 17 '05 #1
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

Nov 17 '05 #2
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


Nov 17 '05 #3
> 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]

Nov 17 '05 #4
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]

Nov 17 '05 #5

"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.

Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

29
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...
1
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...
3
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...
13
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%...
1
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...
0
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...
0
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...
0
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,...
0
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...
0
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.