Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
767 views
in Technique[技术] by (71.8m points)

c# - How to use a mutex

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()
    {
        //...
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...