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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:58:02+00:00 2026-06-14T02:58:02+00:00

In my attempt to create concurrent Socket operations, I’ve created the following code: ConcurrentQueue<byte[]>

  • 0

In my attempt to create concurrent Socket operations, I’ve created the following code:

ConcurrentQueue<byte[]> messageQueue;
ManualResetEvent resetEvent;
Thread outThread;   // -> new Thread(BeginSending);

public void BeginSending() // invoked by outThread
{
    while (true)
    {
        resetEvent.WaitOne();
        while (messageQueue.Count > 0)
        {
            byte[] msg;
            messageQueue.TryDequeue(out msg);
            // send msg via socket
        }
        resetEvent.Reset();
    }
}

public void QueueMessage(byte[] msg) // invoked by the main thread
{
    messageQueue.Enqueue(msg);
    resetEvent.Set();
}

Is adding items to the ConcurrentQueue while a different thread is iterating/dequeuing it a dangerous thing?

From my understanding many synchronized collections simply have individually synchronized methods, but is the same true for concurrentQueue and similar collections?
(ConcurrentBag, ConcurrentDictionary, ConcurrentStack)

  • 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-06-14T02:58:03+00:00Added an answer on June 14, 2026 at 2:58 am

    The ConcurrentQueue itself is OK, as long as you are not mutating the arrays stored as its elements.

    However, your usage pattern with ManualResetEvent suggests that there is a better solution: if you use BlockingCollection<T>, you would be able to avoid doing manual synchronization.

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

Sidebar

Related Questions

In the following code, I attempt to create an SVG circle on the screen
The following code snippet attempts to create a Tib DaemonManager connecting to a particular
I had an issue with mocking Apache Http client. The following attempt to create
The following code is thread-safe as far as I can tell, with the caveat
When I attempt to create a new adv, I get the following error message:
When I attempt to create the stored procedure below I get the following error:
I have the following CSS in attempt to create a rollover effect: #arrow {
In an attempt to create cleaner code, I am trying to write event handler
I'm after a simple stored procedure to drop tables. Here's my first attempt: CREATE
Continuing on my attempt to create a DateTime class , I am trying to

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.