我遇到了多線程中的問題,多線程模型是1個生產者 - N個消費者。多線程中對數據的引用跟蹤(多少/誰)
生產者所產生的數據(字符數據周圍200bytes每個),把它在固定大小的高速緩存(即2密耳)。數據與所有線程無關。它應用過濾器(已配置)並確定線程數量是否符合生成的數據。
生產者推動指針數據分成合格的線程隊列(僅指向數據的指針,以避免數據複製)。線程將deque並通過TCP/IP發送給它們的客戶端。
問題:因爲只有指針數據被提供給多個線程,當高速緩存變滿,主要生產要刪除的第一個項目(舊)。任何線程仍然可能引用數據。
可行之路:用原子粒度,當製片人決定了排位賽的線程數,它可以更新計數器和線程ID列表。
class InUseCounter
{
int m_count;
set<thread_t> m_in_use_threads;
Mutex m_mutex;
Condition m_cond;
public:
// This constructor used by Producer
InUseCounter(int count, set<thread_t> tlist)
{
m_count = count;
m_in_use_threads = tlist;
}
// This function is called by each threads
// When they are done with the data,
// Informing that I no longer use the reference to the data.
void decrement(thread_t tid)
{
Gaurd<Mutex> lock(m_mutex);
--m_count;
m_in_use_threads.erease(tid);
}
int get_count() const { return m_count; }
};
主chache
map<seqnum, Data>
|
v
pair<CharData, InUseCounter>
當生產者將刪除它檢查計數器的元件,爲大於0時,它發送動作以釋放設置m_in_use_threads參考到螺紋上。
問題
- 如果在主緩存2MIL記錄,將有相等數量 的InUseCounter,所以互斥可變因素,在一個單一的過程中這個建議有2MIL互斥varible。
- 有大單數據結構來保持InUseCounter將 造成更多的鎖定時間去尋找和減少
- 什麼是對我的方法的最佳替代品,以找出引用和誰 都非常少引用鎖定時間。
提前感謝您的建議。
啊,同樣的想法:)簡潔的參考! – 2012-01-10 08:43:45