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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:47:33+00:00 2026-05-26T09:47:33+00:00

I have one thread, that is sending data stored in a buffer of type

  • 0

I have one thread, that is sending data stored in a buffer of type List< string> via tcp. Another thread is writing into the buffer. As I am not very familiar with c# I’d like to know how I should use lock or Mutex correctly.

This is the code I’d like to use eventually:

 while(buffer.isLocked()) 
 { 
    buffer.wait();
 }

  buffer.lockBuffer();
  buffer.add(tcpPacket);
  buffer.unlockBuffer();
  buffer.notify();

This is my current code. I hope someone can help me complete it.

public class Buffer
{
    private Mutex mutex; 
    private List<string> buffer; 
    private bool locked = false; 

    public Buffer()
    {
        mutex = new Mutex(false);
        buffer = new List<string>(); 
    }

    public bool isLocked()
    {
        return locked; 
    }

    public void lockBuffer()
    {
        if (!locked)
        {
            //...
           locked = true; 
        }
    }

    public void unlockBuffer()
    {
        if(locked)
        {
            mutex.ReleaseMutex();
            locked = false; 
        }
    }

    public void wait()
    {
        mutex.WaitOne();
    }

    public void notify()
    {
        //...
    }
}
  • 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-26T09:47:34+00:00Added an answer on May 26, 2026 at 9:47 am

    It would be better if you use System.Collections.Concurrent.BlockingCollection. It doesn’t require an external sync.

    For those who don’t use 4.0

    using System;
    using System.Collections.Generic;
    using System.Threading;
    
    namespace MyCollections
    {
        public class BlockingQueue<T> : IDisposable
        {
            Queue<T> _Queue = new Queue<T>();
            SemaphoreSlim _ItemsInQueue = null;
            SemaphoreSlim _FreeSlots = null;
            int _MaxItems = -1;
    
            public BlockingQueue(int maxItems=Int32.MaxValue)
            {
                _MaxItems = maxItems;
                _ItemsInQueue = new SemaphoreSlim(0, maxItems);
                _FreeSlots = new SemaphoreSlim(maxItems, maxItems);
            }
    
            public void Dispose()
            {
                if (_ItemsInQueue != null) _ItemsInQueue.Dispose();
                if (_FreeSlots != null) _FreeSlots.Dispose();
            }
    
            public int Count
            {
                get { return _ItemsInQueue.CurrentCount; }
            }
    
    
            public void Add(T item)
            {
                if(_MaxItems != Int32.MaxValue) _FreeSlots.Wait();
                lock (this)
                {
                    _Queue.Enqueue(item);
                    _ItemsInQueue.Release();
                }
            }
    
    
            public T Take()
            {
                T item = default(T);
                _ItemsInQueue.Wait();
                lock (this)
                {
                     item = _Queue.Dequeue();
                     if (_MaxItems != Int32.MaxValue)  _FreeSlots.Release();
                }
                return item;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one thread that writes results into a Queue. In another thread (GUI),
I have one thread that is receiving data over a socket like this: while
Let's say that I have the following code that's run in one thread of
I have an application that has two threads. The first one (the main thread)
I have one std::list<> container and these threads: One writer thread which adds elements
I am writing code in VS2005 using its STL. I have one UI thread
I have 2 threads and global Queue, one thread (t1) push the data and
I have a thread running in the background that will sleep and pull data
I have a Perl script that launches 2 threads,one for each processor. I need
I have two threads, one thread processes a queue and the other thread adds

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.