マルチスレッドプログラミング相談室 その6at TECH
マルチスレッドプログラミング相談室 その6 - 暇つぶし2ch39:デフォルトの名無しさん
07/08/18 11:47:32
今度こそ最後、汎用化バージョン(structでもOK)
using System;
using System.Threading;

public class CyclicBuffer<T>
{
private volatile int m_head;
private volatile int m_tail;
private readonly Entry[] m_buffer;

public CyclicBuffer(int capacity)
{
m_buffer = new Entry[capacity + 1];
}

public bool Enqueue(T value)
{
if (value == null) throw new ArgumentNullException("value");
int current;
int next;
do
{
current = m_head;
next = current + 1 == m_buffer.Length ? 0 : current + 1;
if (next == m_tail || m_buffer[current].Stored) return false;
} while (Interlocked.CompareExchange(ref m_head, next, current) != current);
m_buffer[current].Value = value;
m_buffer[current].Stored = true;
return true;
}



次ページ
続きを表示
1を表示
最新レス表示
レスジャンプ
類似スレ一覧
スレッドの検索
話題のニュース
おまかせリスト
オプション
しおりを挟む
スレッドに書込
スレッドの一覧
暇つぶし2ch