Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5982243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:03:35+00:00 2026-05-22T22:03:35+00:00

I am writing a program that reads data packets from a file, and assigns

  • 0

I am writing a program that reads data packets from a file, and assigns each packet to a specified pipeline for processing. Each pipeline object has a blocking queue and a filter class. There can be several such pipelines in operation simultaneously.

The blocking queue just collects packets on the input side until it reaches maximum capacity, at which time it blocks the Queue method until the filter Dequeue’s some packets. The blocking queue is a simple class that uses Monitor.Wait() and Monitor.Pulse() for synchronization.

Each filter has a Process() method that is executed on its own dedicated thread. It looks something like this:

public void Process()
{
    while (!done)
    {
        var packet = sourceQueue.Dequeue();

        //  Perform some filtering operation on packet here

        targetQueue.Enqueue(sourceQueue);

        // Let other threads do some work
        Thread.Sleep(Timespan.Zero);
    }
}

What are the possible pitfalls of using the Thread.Sleep() method in this way?

The MSDN documentation stipulates that the use of Thread.Sleep(Timespan.Zero) “indicates that this thread should be suspended to allow other waiting threads to execute,” which is what I want.

But there are blog entries like this one that basically state that using Thread.Sleep is evil. Why? Is this a good use of Thread.Sleep, or is there a better way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T22:03:35+00:00Added an answer on May 22, 2026 at 10:03 pm

    Real programs don’t Sleep().

    Your suggestion, Sleep(0) has an additional problem with allowing only threads with the same priority to run. Generally Sleep(1) is considered a little safer. See Joe Duffy.

    But in either case your resolution is ~20ms, which could be way too long.

    Your loop as stated does a tiny bit of work and then induces a context switch, very wasteful.

    It’s a lot better to make the most of a time-slot, so keep working until the Queue blocks you. Blocking is better than sleeping.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a program that reads a stream of data and parses it into
I have written a program that reads in a File object (really an XML
I am writing an application in C# that reads info from a .mdb file
If you are writing a program that is executable from the command line, you
I am maintaining a program that reads records from a Access '97 table using
I am working on a program that reads in from a serial port, then
I'm trying to write a basic Erlang program that reads in a file in
I am writing a C program that will read and write from/to a serial
In a program that I'm writing, I wanted to make a ConfigParser that's read
I'm writing a program that sends an email out at a client's specific local

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.