gpt4 book ai didi

C# One Writer Many Readers 只读一次

转载 作者:太空狗 更新时间:2023-10-30 01:21:27 25 4
gpt4 key购买 nike

我有 4 个线程。一个是从网络中读取一些信息,将其写入变量中,并且应该在每条信息之后发出信号。其中 3 个正在读取这个变量,应该只读取一次。当前的解决方案是编写器在写入后设置事件并等待读者事件。读者等待事件,然后阅读并设置他们的事件(意味着他们阅读)。问题是读者可以阅读不止一次,而我有重复的。如何实现读者只读一次的规则?

最佳答案

下面是一种实现方式

数据以单链表的形式在线程之间共享。列表中的每个节点都可以是标记或具有数据。该列表以单个节点开始,该节点被键入到标记中。读取数据时,会形成一个新列表,其中包含一系列数据节点,后跟一个标记。该列表附加到添加到列表中的最新标记。

每个读者线程都以对原始标记节点和 AutoResetEvent 的引用开始。每当一条新数据进入编写器时,它都会为每个读取器线程发出 AutoResetEvent 信号。然后,读取器线程将简单地遍历,直到它找到一个没有 Next 节点的标记。

该方案确保所有读者只看到一次数据。最大的复杂是构建列表,以便可以以无锁方式写入和读取它。这对于 Interlocked.CompareExchange 来说非常简单

链表类型

class Node<T> { 
public bool IsMarker;
public T Data;
public Node<T> Next;
}

示例编写器类型

class Writer<T> {
private List<AutoResetEvent> m_list;
private Node<T> m_lastMarker;

public Writer(List<AutoResetEvent> list, Node<T> marker) {
m_lastMarker = marker;
m_list = list;
}

// Assuming this can't overlap. If this can overload then you will
// need synchronization in this method around the writing of
// m_lastMarker
void OnDataRead(T[] items) {
if (items.Length == 0) {
return;
}

// Build up a linked list of the new data followed by a
// Marker to signify the end of the data.
var head = new Node<T>() { Data = items[0] };
var current = head;
for (int i = 1; i < items.Length; i++) {
current.Next = new Node<T>{ Data = items[i] };
current = current.Next;
}
var marker = new Node<T> { IsMarker = true };
current.Next = marker;

// Append the list to the end of the last marker node the writer
// created
m_lastMarker.Next = head;
m_lastMarker = marker;

// Tell each of the readers that there is new data
foreach (var e in m_list) {
e.Set();
}
}
}

示例阅读器类型

class Reader<T> { 
private AutoResetEvent m_event;
private Node<T> m_marker;

void Go() {
while(true) {
m_event.WaitOne();
var current = m_marker.Next;
while (current != null) {
if (current.IsMarker) {
// Found a new marker. Always record the marker because it may
// be the last marker in the chain
m_marker = current;
} else {
// Actually process the data
ProcessData(current.Data);
}
current = current.Next;
}
}
}
}

关于C# One Writer Many Readers 只读一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15624786/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com