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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:35:55+00:00 2026-05-30T06:35:55+00:00

I am writing a multi threaded application and I have concerns with 2 threads

  • 0

I am writing a multi threaded application and I have concerns with 2 threads access a queue

Thread 1 puts items in a queue for processing
Thread 2 removes items from the queue to process

Thread 1 runs once a minute due to the nature of the data it is pulling.
Thread 2 is always running, it removes an item from the queue and sleeps for 100ms. I have to do this to ensure that I don’t overwhelm a service it calls when it dequeues an item.

I assume both threads should place a lock on the queue when adding or removing items from it. Are there any further considerations? For instance say Thread 1 has a lock and Thread 2 tries to access it. Does Thread 2 simply know to wait and resume once the lock is removed?

Would it be preferable to use a ConcurrentQueue and just TryDequeue and if it fails just go about its 100 ms sleep?

Thanks in advance

  • 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-30T06:35:56+00:00Added an answer on May 30, 2026 at 6:35 am

    It’s even easier if you use a BlockingCollection<T> like I do in NuGet’s VS console dispatcher for my PostKey/WaitKey implementation. The consuming thread calls Take(...) which will block until another thread calls Add(...). There is no need to poll. Additionally, you may wish to pass a cancellation token to the Take method so another thread can stop the consumer thread if it’s currently waiting for an Add that will never come. Here are the relevant methods:

    private readonly BlockingCollection<VsKeyInfo> _keyBuffer = 
          new BlockingCollection<VsKeyInfo>();
    private CancellationTokenSource _cancelWaitKeySource;
    
    // place a key into buffer
    public void PostKey(VsKeyInfo key)
    {
        if (key == null)
        {
            throw new ArgumentNullException("key");
        }
        _keyBuffer.Add(key);
    }
    
    // signal thread waiting on a key to exit Take
    public void CancelWaitKey()
    {
        if (_isExecutingReadKey && !_cancelWaitKeySource.IsCancellationRequested)
        {
            _cancelWaitKeySource.Cancel();
        }
    }
    
    // wait for a key to be placed on buffer
    public VsKeyInfo WaitKey()
    {
        try
        {
            // raise the StartWaitingKey event on main thread
            RaiseEventSafe(StartWaitingKey);
    
            // set/reset the cancellation token
            _cancelWaitKeySource = new CancellationTokenSource();
            _isExecutingReadKey = true;
    
            // blocking call
            VsKeyInfo key = _keyBuffer.Take(_cancelWaitKeySource.Token);
    
            return key;
        }
        catch (OperationCanceledException)
        {
            return null;
        }
        finally
        {
            _isExecutingReadKey = false;
        }
    }
    

    See http://nuget.codeplex.com/SourceControl/changeset/view/45e353aca7f4#src%2fVsConsole%2fConsole%2fConsole%2fConsoleDispatcher.cs for more details.

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

Sidebar

Related Questions

I'm writing a multi-threaded application. My worker threads get connection from an environment object
I'm writing a multi threaded console application which downloads pdf files from the web
I am writing a multi-threaded c++ application. When thread A has a very computationally
I'm writing a multi-threaded Java web crawler. From what I understand of the web,
I've been writing a multi-threaded DLL for database access using ADO/ODBC for use with
I am on MacOSX. I am writing a multi threaded program. One thread does
I have a multi-threaded application that is using pthreads. I have a mutex() lock
I am writing a multi-threaded application in Java in order to improve performance over
I have a rather simple multi-threaded VCL gui application written with Delphi 2007. I
For example, I'm writing a multi-threaded time-critical application that processes and streams audio in

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.