Wednesday, November 05, 2008

BarrierHandle

Did you ever have a case where you have a big dataset which can be processed in parallel, so long as all threads finish Step 1 before any thread starts Step 2? Thing is, there's no built in class to handle this case.

AutoResetEvent won't do it because it because it only signals *one* of the waiting threads, not *all* of them. What you need is a manual reset event and some complex handling.

I give you BarrierHandle:


using System;
using System.Threading;

class SpectralNorm
{
public class BarrierHandle : System.Threading.WaitHandle
{
int current;
int threads;
ManualResetEvent handle = new ManualResetEvent (false);

public BarrierHandle (int threads)
{
this.current = threads;
this.threads = threads;
}

public override bool WaitOne()
{
ManualResetEvent h = handle;
if (Interlocked.Decrement (ref current) > 0) {
h.WaitOne ();
}
else {
handle = new ManualResetEvent (false);
Interlocked.Exchange (ref current, threads);
h.Set ();
h.Close ();
}

return true;
}
}

public static void Main(String[] args)
{
int threadCount = 5;//Environment.ProcessorCount;

// Lets assume that there's 20 units of data for each thread
int[] dataset = new int [threadCount * 20];

// This is the handle we use to ensure all threads complete the current step
// before continuing to the next step
BarrierHandle barrier = new BarrierHandle(threadCount);

// Fire up the threads
for (int i = 0; i < threadCount; i++) {
ThreadPool.QueueUserWorkItem (delegate {
int jjj = i;
try {
Step1 (dataset, jjj * 20, 20);
barrier.WaitOne ();
Step2 (dataset, jjj * 20, 20);
barrier.WaitOne ();
Step3 (dataset, jjj * 20, 20);
} catch (Exception ex) {
Console.WriteLine (ex);
}
});
}

System.Threading.Thread.Sleep (3000);
}

private static void Step1 (int[] array, int offset, int count)
{
Console.WriteLine ("Step1: {0} - {1}", offset, offset + count);
Thread.Sleep (500);
}
private static void Step2 (int[] array, int offset, int count)
{
Console.WriteLine ("Step2: {0} - {1}", offset, offset + count);
Thread.Sleep (500);
}
private static void Step3 (int[] array, int offset, int count)
{
Console.WriteLine ("Step3: {0} - {1}", offset, offset + count);
Thread.Sleep (500);
}
}

5 comments:

Jérémie Laval said...

There is CountdownEvent class in ParallelFx which can be used for that.

Anonymous said...

免費視訊,煙火,煙火工廠,視訊,正妹視訊,真愛密碼,真愛密碼,煙火秀,煙火圖片,衣蝶,衣蝶,AV女優,AV女優,
煙火批發,小泉彩,小泉彩,情趣用品,鞭炮,live119論壇,蜂炮,視訊

梁爵 said...

2020.03.04不管妳是什麼原因想要進入酒店工作,這個環境的確能讓妳快速的累積財富,相對的,也容易使人迷失! 在這裡大家都是過客,共同的目標就是致富!先回答妳的問題,到酒店上班別心急,要小心踏出妳的第一步,不要跟陌生人簽約,也不要繳保證金,那些都是這個行業沒有規定,有很多騙人的徵人廣告都會要不知情的應徵者簽約繳錢,這是首先要注意的,再來就是應徵地點要約在公共場所,千萬不要貿然的衝去應徵辦公室,別以為女生接電話就是安全的!在密閉空間內沒人可以保證妳的人身安全!。最佳場合見面..開放式公共場所 例如;麥當勞/星巴克之類的比較安全!。
梁曉尊在教妳 怎麼確認經紀人是否有公信力也保護自己未來酒店打工的保障! Google搜尋:經紀人的名字,是否有相關事業…如果搜尋結果沒有!我建議妳再找找別人吧~。如果只搜尋經紀公司網站,也別輕易相信..因為不肖詐騙人士會藉由經紀公司網站欺騙無知女孩們。總而言之: 酒店兼差要懂得保護自己,酒店兼職才能安心賺錢。

梁爵 said...

2020.05.03酒店工作因武漢肺炎(COVID-19、新冠肺炎)新冠肺炎連續6天零確診,讓停業快3周的酒店舞廳業者見到曙光,期盼政府讓他們重新開門做生意。業者估計,全台至少20萬人靠八大行業酒店S維持生計,其中有數萬人是依賴店家的清潔阿姨、賣口香糖、香腸這種「艱苦人」,呼籲政府慎重考慮,在防疫得當的情況下,盡快讓八大行業酒店上班復業,給他們一條生路。台北市八大行業張姓酒店業者表示,酒店歇業,酒店PT小姐雖還能兼差傳播飯局妹,但酒店打工毫無保障,私接案子還被搶、乾洗白嫖的慘狀;另泊車少爺、幹部、經紀,這些人也都各有出路,但總不如原來穩定,至於原本薪水就不高、替代性更高的清潔阿姨,更是頓時少了一份穩定薪水;還有收保特瓶、紙箱的回收阿伯,也大受影響。張姓業者說,這些依附酒店養家餬口的艱苦人,全台少說至少20萬人,如今因政府防疫停業標準不一,酒店、舞廳歇業,他們連兼差機會都沒,只能在家喝西北風「防疫」。他自嘲,「政府的作為,是寧讓人民餓死,也不能得肺炎死。」業者有苦難言。另一劉姓舞廳業者說,會配合停業的店家,都是繳稅合法業者,八大行業若轉地下化,等同「保障非法,取締合法」,政府應讓合法業者在確保防疫規範前提下復業,反而能有效控管,若都轉入地下化,風險更大。造成全台酒店舞廳停業的北市高檔酒店女公關,疫情指揮中心日前已宣布,她的感染源不明,且沒傳染給任何人。不少業者看到這消息都納悶,「小姐結案了,我們為何歇業?何時能重開?」這是攸關20多萬八大行業從業人員共同疑惑,政府應盡速給個答案。

梁爵 said...

2020.05.11中央流行疫情指揮中心在4月9日下令,全台八大行業酒店工作有女陪侍場所無限期停業,讓倚賴八大經濟鏈的酒店上班女公關、酒店經紀、幹部、泊車小弟頓失經濟收入。本刊訪問多名在北市酒店上班的女公關,發現俗稱「酒店S」的她們,為了求生,轉攻各大KTV,跨界過起如「傳播妹」、「飯局妹」的日子。之前在「便服店」酒店上班的「艾可」(化名)說:「八大行業沒辦法上班後,酒店經紀(安排酒店PT酒女工作事宜者)就開始跟各大連鎖KTV訂包廂,我們上班的地點就從酒店變成各大KTV。」「可是KTV跟酒店打工不一樣,客人也不多,收入大概從原本每天八千元到一萬二千元,腰斬成四千元到六千元,如果有接到飯局或私人招待所的攤,收入才會跟原本的差不多。」艾可大嘆。此外,由於台北市林森北路「條通區」上百家的「日式酒店」,營業登記為「餐廳」,因此不受影響可以繼續做生意,外傳台北市的酒店停業後,許多酒店女公關轉進「日式酒店」。艾可卻狠狠打臉這個說法,「疫情蔓延後,日式酒店很多早就關門了,本地酒客根本不可能去那邊消費!」本刊日前實地走訪,果然發現以往晚間九點至十二點間擠滿人潮的「六條通」至「八條通」,大部分「日式酒店」都沒點亮招牌,連路人都寥寥無幾。

Hit Counter