c 執行緒實現生產者消費者

2021-06-06 06:07:34 字數 1885 閱讀 4629

public inte***ce ithreadworker : idisposable

public class threadcontroller

public threadcontroller(int maxthreadcount)

: this(maxthreadcount, string.empty)

public threadcontroller(int maxthreadcount, string name)

startrun();

}public void addjobqueue(ithreadworker info)

//if (!started)//}

private void startrun()

}public bool completed()

private void realwork(object obj)

, set", sync.i);

sync.customerfinishevent.set();

break;

}else}}

else

, false", sync.i);

lock (((icollection)workqueue).syncroot)

else}}

using (instance)

, ", sync.i, info.info);

instance.realworker();}}

catch (exception e)

finally

, set", sync.i);

sync.customersyncevent.set();}}

}internal class customersynceventcontroller

public manualresetevent customerfinishevent

public manualresetevent producterfinishevent

public customersynceventcontroller()

public customersynceventcontroller(manualresetevent customersync, manualresetevent customerfinish, manualresetevent producterfinish)

}}

public class implementclass : ithreadworker

public implementclass(string content)

#region ithreadworker members

public void realworker()

#endregion

#region idisposable members

public void dispose()

#endregion

}

static void main(string args)

threadcontroller monitor = new threadcontroller(32);

foreach (implementclass ic in list)

monitor.producterfinishevent.set();

if (monitor.completed())

}

實現自己的ithreadworker,

不要忘記呼叫

monitor.producterfinishevent.set(),證明佇列中不會再增加新的內容

monitor.completed()保證所有的消費者都完成了工作。

生產者消費者執行緒

include include include includeusing namespace std typedef int semaphore 訊號量是一種特殊的整型變數 const int size of buffer 5 緩衝區長度 const unsigned short producers...

生產者消費者執行緒

該簡單生產者 消費者執行緒,屬於本人學習過程中的一段練習 如有不足,請指點 package com.lanqiao.demo3 author 大廣子 類說明 簡單的生產者,消費者執行緒 public class threadptcs catch interruptedexception e 退出 s...

生產者消費者 生產者與消費者模式

一 什麼是生產者與消費者模式 其實生產者與消費者模式就是乙個多執行緒併發協作的模式,在這個模式中呢,一部分執行緒被用於去生產資料,另一部分執行緒去處理資料,於是便有了形象的生產者與消費者了。而為了更好的優化生產者與消費者的關係,便設立乙個緩衝區,也就相當於乙個資料倉儲,當生產者生產資料時鎖住倉庫,不...