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 6322331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:18:40+00:00 2026-05-24T16:18:40+00:00

I have a class which reads data from port through TcpClient. I’ve created 2

  • 0

I have a class which reads data from port through TcpClient. I’ve created 2 threads.

  1. Packet Reading Thread
  2. Packet Processing Thread

1 Thread continuously reads bytes and once it gets a proper packet it adds that packet in a Queue and initiates the 2nd thread, which then reads packet from that Queue and processes it.

The packet processing should be done sequentially. Therefore each time when the reading thread adds the packet in Queue, it checks that whether that Processing Thread is running or NOT. If NOT then it again starts that thread. This is because we don’t want multiple packet processing threads running simultaneously.

Now my question is actually related to that checking part. What I’ve done is that, I’ve added a simple bool variable in my class. And when I get into the packet processing thread I set that bool = true. While packet reading thread checks this bool variable, if it is true then it knows that the processing thread is running. And when packet processing thread reaches at end, it sets that bool = false.

Is this the correct way? Or is this approach prone to race condition?

Thanks.

Update (a little detail):

delegate void PacketProcessDelegate();
PacketProcessDelegate PacketProcess = new PacketProcessDelegate(this.PacketProcessingThread);

PacketReadingThread() {
    Packet = GetPacket();  // thread blocks until packet is NOT received
    Queue.Synchronized(this.Queue).Enque(Packet);
    if (!this.IsProcessing)
        this.PacketProcess.BeginInvoke(null, null);
}

PacketProcessingThread() {
    this.IsProcessing=true;
    while(true) {
        Queue syncQueue;
        syncQueueu = Queue.Synchronized(this.Queue);
        if(syncQueueu.Count > 0)
            //packet extraction and processing code here
        else
            break;
    }
    this.IsProcessing=false;
}

Goals:
Packet Processing thread either
*) should be terminated and should be restarted, but ThreadPool should be utilized in order to prevent creation of a new thread each time. Thats why I’ve used the BeginInvoke.
OR
*) it should be blocked somehow until another packet is NOT received.

  • 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-24T16:18:41+00:00Added an answer on May 24, 2026 at 4:18 pm

    You’ve definitely got a race condition, without any more locking than has been described. If two packets come in in quick succession, you may see that the flag is false twice and start two different threads. You could avoid this by making the reading thread set the flag to false when it’s starting the thread – so the flag means “a thread is running or starting”.

    It’s not clear why you need to keep starting threads anyway – why not just have one processing thread which blocks waiting for new packets when the queue is empty? If you’re using .NET 4 this is made really easy using BlockingCollection<T>.

    EDIT: If you don’t need the packets to be processed in a particular order, you could just start a new thread pool work item for each packet:

    ThreadPool.QueueUserWorkItem(ProcessPacket, packet);
    
    ...
    
    private void ProcessPacket(object state)
    {
        Packet packet = (Packet) state;
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a junit custom class loader, which reads the test data from
I have a class named ExampleHandler extends from DefaultHandler . This class reads data
I have a python class which reads a config file using ConfigParser: Config file:
I have a class which inherits from QTreeWidgetItem and I intercept the click event.
I have a class which is not thread safe: class Foo { /* Abstract
I'm writing an analogue of DatabaseConfiguration class which reads configuration from database and I
I am having troubles with my class which reads and writes data to a
I have a server in Java which is multithreading, and I've created a thread
I'm writing a J2ME class which reads variable keys and contents from a resource
I have an activity which shows 3 random images from JSON data off the

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.