Generic CountdownLatch Synchronization Primitive

The CountdownLatch can be a very useful synchronization primitive for certain threading jobs. Here is my generic version that calls a generic Action<T> after latch reaches zero.

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Diagnostics;

namespace WJS.Threading
{
    /// <summary>
    /// CountdownLatch is a very fast and thin one-time latch that counts down from "count".
    /// Calling Signal from any thread will atomically decrement the count.
    /// When count reaches zero, the passed delegate is called on the calling thread passing optional state.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class CountdownLatch<T>
    {
        private int remain;
        private Action<T> action;
        private T state;

        public CountdownLatch(int count, T state, Action<T> action)
        {
            if (count < 1)
                throw new ArgumentOutOfRangeException("count");
            if (action == null)
                throw new ArgumentNullException("action");
            this.remain = count;
            this.action = action;
            this.state = state;
        }

        /// <summary>
        /// Gets the latch remaining count. If remaining is less than or equal to zero, the latch was opened.
        /// </summary>
        public int Remaining
        {
            get { return remain; }
        }

        /// <summary>
        /// Decrement the count by 1 and call action if count is zero.
        /// </summary>
        public void Signal()
        {
            // The last thread to signal, runs the delegate.
            if (Interlocked.Decrement(ref remain) == 0)
                action(state);
        }

        [Conditional("DEBUG")]
        public static void Test()
        {
            CountdownLatch<string> cLatch = new CountdownLatch<string>(5, "Hello",
                delegate(string s)
                {
                    Console.WriteLine(s);
                });

            for (int i = 0; i < 10; i++)
                cLatch.Signal();
            Console.WriteLine("Done");
        }
    }
}

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

7 Responses to Generic CountdownLatch Synchronization Primitive

  1. Unknown says:

    http://www.global-e-world.com/Touch-Screen-LCD-Monitor/ Touch Screen LCD Monitorhttp://www.global-e-world.com/Touch-Screen-LCD-Monitor/ Touch Screen LCD Monitorshttp://www.global-e-world.com/Touch-Screen-LCD-Monitor/ Touch Screen Monitorshttp://www.global-e-world.com/Touch-Screen-LCD-Monitor/ Touch Screen TFT LCD Monitorhttp://www.global-e-world.com/Touch-Screen-LCD-Monitor/ PC monitorhttp://www.global-e-world.com/Touch-Screen-LCD-Monitor/ VGA Monitorhttp://www.global-e-world.com/Touch-Screen-LCD-Monitor/ LCD monitorhttp://www.global-e-world.com/Touch-Screen-LCD-Monitor/ Touch Screen LCDhttp://www.global-e-world.com/Touch-Screen-LCD-Monitor/ Touchscreen Monitorhttp://www.global-e-world.com/Touch-Screen-LCD-Monitor/ Touch Screen Panelhttp://www.global-e-world.com/Touch-Screen-LCD-Monitor/ Touch Screenhttp://www.global-e-world.com/Touch-Screen-LCD-Monitor/ Touchscreen LCDhttp://www.global-e-world.com/Touch-Screen-LCD-Monitor/ LCD Monitorshttp://www.global-e-world.com/Touch-Screen-LCD-Monitor/ Touch Screen LCD Panelhttp://www.global-e-world.com/Touch-Screen-LCD-Monitor/ LCD flat Panel Monitorhttp://www.global-e-world.com/Touch-Screen-LCD-Monitor/ TFT LCD Monitorhttp://www.global-e-world.com/Touch-Screen-LCD-Monitor/ TFT Monitorhttp://www.global-e-world.com/Touch-Screen-LCD-Monitor/7-inch-Touch-Screen-LCD-Monitor.php 7 Inch Touch Screen LCDhttp://www.global-e-world.com/Touch-Screen-LCD-Monitor/7-inch-Touch-Screen-LCD-Monitor.php 7 Inch Touch screen VGA Monitorhttp://www.global-e-world.com/Touch-Screen-LCD-Monitor/7-inch-Touch-Screen-LCD-Monitor.php 7 Inch Touch screen Monitorhttp://www.global-e-world.com/Touch-Screen-LCD-Monitor/7-inch-Touch-Screen-LCD-Monitor.php 7 Inch Touch Screen LCD Monitorhttp://www.global-e-world.com/Touch-Screen-LCD-Monitor/7-inch-Touch-Screen-LCD-Monitor.php 7 Inch TFT LCD Touch Screen Monitorhttp://www.global-e-world.com/Touch-Screen-LCD-Monitor/7-inch-Touch-Screen-LCD-Monitor.php 7 Inch LCD Touch Screen Monitorshttp://www.global-e-world.com/Touch-Screen-LCD-Monitor/7-inch-Touch-Screen-LCD-Monitor.php 7 Inch LCD Touch Screen Monitorhttp://www.global-e-world.com/Touch-Screen-LCD-Monitor/7-inch-Touch-Screen-LCD-Monitor.php 7 Inch VGA Monitorhttp://www.global-e-world.com/Touch-Screen-LCD-Monitor/7-inch-Touch-Screen-LCD-Monitor.php 7 Inch Touch Screen LCD Panelhttp://www.global-e-world.com/Touch-Screen-LCD-Monitor/7-inch-Touch-Screen-LCD-Monitor.php 7 Inch TFT LCD Monitorhttp://www.global-e-world.com/Touch-Screen-LCD-Monitor/8-inch-Touch-Screen-LCD-Monitor.php 8 Inch Touch Screen LCDhttp://www.global-e-world.com/Touch-Screen-LCD-Monitor/8-inch-Touch-Screen-LCD-Monitor.php 8 Inch Touch screen VGA Monitorhttp://www.global-e-world.com/Touch-Screen-LCD-Monitor/8-inch-Touch-Screen-LCD-Monitor.php 8 Inch Touch screen Monitorhttp://www.global-e-world.com/Touch-Screen-LCD-Monitor/8-inch-Touch-Screen-LCD-Monitor.php 8 Inch Touch Screen LCD Monitorhttp://www.global-e-world.com/Touch-Screen-LCD-Monitor/8-inch-Touch-Screen-LCD-Monitor.php 8 Inch TFT LCD Touch Screen Monitorhttp://www.global-e-world.com/Touch-Screen-LCD-Monitor/8-inch-Touch-Screen-LCD-Monitor.php 8 Inch LCD Touch Screen Monitorhttp://www.global-e-world.com/Touch-Screen-LCD-Monitor/8-inch-Touch-Screen-LCD-Monitor.php 8 Inch LCD Touch Screen Monitorshttp://www.global-e-world.com/Touch-Screen-LCD-Monitor/8-inch-Touch-Screen-LCD-Monitor.php 8 Inch VGA Monitorhttp://www.global-e-world.com/Touch-Screen-LCD-Monitor/8-inch-Touch-Screen-LCD-Monitor.php 8 Inch Touch Screen LCD Panelhttp://www.global-e-world.com/Touch-Screen-LCD-Monitor/8-inch-Touch-Screen-LCD-Monitor.php 8 Inch TFT LCD Monitorhttp://www.global-e-world.com/Touch-Screen-LCD-Monitor/10.4-inch-Touch-Screen-LCD-Monitor.php 10.4 Inch Touch Screen LCDhttp://www.global-e-world.com/Touch-Screen-LCD-Monitor/10.4-inch-Touch-Screen-LCD-Monitor.php 10.4 Inch Touch screen VGA Monitorhttp://www.global-e-world.com/Touch-Screen-LCD-Monitor/10.4-inch-Touch-Screen-LCD-Monitor.php 10.4 Inch Touch screen Monitorhttp://www.global-e-world.com/Touch-Screen-LCD-Monitor/10.4-inch-Touch-Screen-LCD-Monitor.php 10.4 Inch Touch Screen LCD Monitorhttp://www.global-e-world.com/Touch-Screen-LCD-Monitor/10.4-inch-Touch-Screen-LCD-Monitor.php 10.4 Inch TFT LCD Touch Screen Monitorhttp://www.global-e-world.com/Touch-Screen-LCD-Monitor/10.4-inch-Touch-Screen-LCD-Monitor.php 10.4 Inch LCD Touch Screen Monitorhttp://www.global-e-world.com/Touch-Screen-LCD-Monitor/10.4-inch-Touch-Screen-LCD-Monitor.php 10.4 Inch LCD Touch Screen Monitorshttp://www.global-e-world.com/Touch-Screen-LCD-Monitor/10.4-inch-Touch-Screen-LCD-Monitor.php 10.4 Inch VGA Monitorhttp://www.global-e-world.com/Touch-Screen-LCD-Monitor/10.4-inch-Touch-Screen-LCD-Monitor.php 10.4 Inch Touch Screen LCD Panelhttp://www.global-e-world.com/Touch-Screen-LCD-Monitor/10.4-inch-Touch-Screen-LCD-Monitor.php 10.4 Inch TFT LCD Monitor

  2. Unknown says:

    http://www.batterylaptoppower.com/dell/inspiron-9200.htm dell inspiron 9200 battery http://www.batterylaptoppower.com/dell/inspiron-9300.htm dell inspiron 9300 battery http://www.batterylaptoppower.com/dell/inspiron-9400.htm dell inspiron 9400 battery http://www.batterylaptoppower.com/dell/e1705.htm dell e1705 battery http://www.batterylaptoppower.com/dell/inspiron-1520.htm dell inspiron 1520 battery http://www.batterylaptoppower.com/dell/inspiron-1720.htm dell inspiron 1720 battery http://www.batterylaptoppower.com/dell/vostro-1500.htm dell vostro 1500 battery http://www.batterylaptoppower.com/dell/vostro-1700.htm dell vostro 1700 battery http://www.batterylaptoppower.com/dell/0xr693.htm dell 0xr693 battery http://www.batterylaptoppower.com/dell/312-0625.htm dell 312-0625 battery http://www.batterylaptoppower.com/dell/312-0633.htm dell 312-0633 battery http://www.batterylaptoppower.com/dell/gw240.htm dell gw240 battery http://www.batterylaptoppower.com/dell/hp297.htm dell hp297 battery http://www.batterylaptoppower.com/dell/rn873.htm dell rn873 battery http://www.batterylaptoppower.com/dell/xr693.htm dell xr693 battery http://www.batterylaptoppower.com/dell/inspiron-1525.htm dell inspiron 1525 battery http://www.batterylaptoppower.com/dell/inspiron-1526.htm dell inspiron 1526 battery http://www.batterylaptoppower.com/dell/m1210.htm dell m1210 battery http://www.batterylaptoppower.com/dell/nf343.htm dell nf343 battery http://www.batterylaptoppower.com/dell/1210.htm dell 1210 battery http://www.batterylaptoppower.com/dell/cg036.htm dell cg036 battery http://www.batterylaptoppower.com/dell/d044h.htm dell d044h battery http://www.batterylaptoppower.com/dell/w953g.htm dell w953g battery http://www.batterylaptoppower.com/dell/312-0831.htm dell 312-0831 battery http://www.batterylaptoppower.com/dell/inspiron-910.htm dell inspiron 910 battery http://www.batterylaptoppower.com/dell/inspiron-mini-9.htm dell inspiron mini 9 battery http://www.batterylaptoppower.com/dell/xd187.htm dell xd187 battery http://www.batterylaptoppower.com/dell/inspiron-1300.htm dell inspiron 1300 battery http://www.batterylaptoppower.com/dell/inspiron-b120.htm dell inspiron b120 battery http://www.batterylaptoppower.com/dell/inspiron-b130.htm dell inspiron b130 battery http://www.batterylaptoppower.com/gateway/12msbg.htm gateway 12msbg battery http://www.batterylaptoppower.com/gateway/4s2p.htm gateway 4s2p battery http://www.batterylaptoppower.com/gateway/8msb.htm gateway 8msb battery http://www.batterylaptoppower.com/gateway/8msbg.htm gateway 8msbg battery http://www.batterylaptoppower.com/gateway/s62044l.htm gateway s62044l battery http://www.batterylaptoppower.com/gateway/m320.htm gateway m320 battery http://www.batterylaptoppower.com/gateway/m325.htm gateway m325 battery http://www.batterylaptoppower.com/gateway/4000.htm gateway 4000 battery http://www.batterylaptoppower.com/gateway/s62066l.htm gateway s62066l battery http://www.batterylaptoppower.com/gateway/m680.htm gateway m680 battery http://www.batterylaptoppower.com/gateway/m360.htm gateway m360 battery http://www.batterylaptoppower.com/gateway/m460.htm gateway m460 battery http://www.batterylaptoppower.com/gateway/s62066l.htm gateway s62066l battery http://www.batterylaptoppower.com/gateway/squ-412.htm gateway squ-412 battery http://www.batterylaptoppower.com/gateway/squ-413.htm gateway squ-413 battery

  3. Unknown says:

    http://www.adapterlist.com/dell/latitude-d531.htm dell latitude d531 battery http://www.adapterlist.com/dell/precision-m65.htm dell precision m65 battery http://www.adapterlist.com/dell/d820.htm dell d820 battery http://www.adapterlist.com/dell/inspiron-6000.htm dell inspiron 6000 battery http://www.adapterlist.com/dell/inspiron-9200.htm dell inspiron 9200 battery http://www.adapterlist.com/dell/inspiron-9300.htm dell inspiron 9300 battery http://www.adapterlist.com/dell/inspiron-9400.htm dell inspiron 9400 battery http://www.adapterlist.com/dell/e1705.htm dell e1705 battery http://www.adapterlist.com/dell/inspiron-1520.htm dell inspiron 1520 battery http://www.adapterlist.com/dell/inspiron-1720.htm dell inspiron 1720 battery http://www.adapterlist.com/dell/vostro-1500.htm dell vostro 1500 battery http://www.adapterlist.com/dell/vostro-1700.htm dell vostro 1700 battery http://www.adapterlist.com/dell/0xr693.htm dell 0xr693 battery http://www.adapterlist.com/dell/312-0625.htm dell 312-0625 battery http://www.adapterlist.com/dell/312-0633.htm dell 312-0633 battery http://www.adapterlist.com/dell/gw240.htm dell gw240 battery http://www.adapterlist.com/dell/hp297.htm dell hp297 battery http://www.adapterlist.com/dell/rn873.htm dell rn873 battery http://www.adapterlist.com/dell/xr693.htm dell xr693 battery http://www.adapterlist.com/dell/inspiron-1525.htm dell inspiron 1525 battery http://www.adapterlist.com/dell/inspiron-1526.htm dell inspiron 1526 battery http://www.adapterlist.com/dell/m1210.htm dell m1210 battery http://www.adapterlist.com/dell/nf343.htm dell nf343 battery http://www.adapterlist.com/dell/1210.htm dell 1210 battery http://www.adapterlist.com/dell/cg036.htm dell cg036 battery http://www.adapterlist.com/dell/d044h.htm dell d044h battery http://www.adapterlist.com/dell/w953g.htm dell w953g battery http://www.adapterlist.com/dell/312-0831.htm dell 312-0831 battery http://www.adapterlist.com/dell/inspiron-910.htm dell inspiron 910 battery http://www.adapterlist.com/dell/inspiron-mini-9.htm dell inspiron mini 9 battery http://www.adapterlist.com/dell/xd187.htm dell xd187 battery http://www.adapterlist.com/dell/inspiron-1300.htm dell inspiron 1300 battery http://www.adapterlist.com/dell/inspiron-b120.htm dell inspiron b120 battery http://www.adapterlist.com/dell/inspiron-b130.htm dell inspiron b130 battery http://www.adapterlist.com/gateway/12msbg.htm gateway 12msbg battery http://www.adapterlist.com/gateway/4s2p.htm gateway 4s2p battery http://www.adapterlist.com/gateway/8msb.htm gateway 8msb battery http://www.adapterlist.com/gateway/8msbg.htm gateway 8msbg battery http://www.adapterlist.com/gateway/s62044l.htm gateway s62044l battery http://www.adapterlist.com/gateway/m320.htm gateway m320 battery http://www.adapterlist.com/gateway/m325.htm gateway m325 battery http://www.adapterlist.com/gateway/4000.htm gateway 4000 battery http://www.adapterlist.com/gateway/s62066l.htm gateway s62066l battery http://www.adapterlist.com/gateway/m680.htm gateway m680 battery http://www.adapterlist.com/gateway/m360.htm gateway m360 battery http://www.adapterlist.com/gateway/m460.htm gateway m460 battery http://www.adapterlist.com/gateway/s62066l.htm gateway s62066l battery http://www.adapterlist.com/gateway/bat0016.htm gateway bat0016 battery http://www.adapterlist.com/gateway/m1200.htm gateway m1200 battery http://www.adapterlist.com/gateway/m1300.htm gateway m1300 battery http://www.adapterlist.com/gateway/m500.htm gateway m500 battery http://www.adapterlist.com/gateway/m505.htm gateway m505 battery http://www.adapterlist.com/gateway/btp-68b3.htm gateway btp-68b3 battery http://www.adapterlist.com/gateway/btp-51b3.htm gateway btp-51b3 battery http://www.adapterlist.com/hp/hstnn-db11.htm hp hstnn-db11 battery

  4. Unknown says:

    http://www.shopgogo.ca/acer/aspire-3020.htm acer aspire 3020 battery , http://www.shopgogo.ca/acer/aspire-3610.htm acer aspire 3610 battery , http://www.shopgogo.ca/acer/squ-401.htm acer squ-401 battery , http://www.shopgogo.ca/acer/lcbtp03003.htm acer lcbtp03003 battery , http://www.shopgogo.ca/acer/travelmate-2300.htm acer travelmate 2300 battery , http://www.shopgogo.ca/acer/travelmate-4000.htm acer travelmate 4000 battery , http://www.shopgogo.ca/acer/travelmate-3002wtci.htm acer travelmate 3002wtci battery , http://www.shopgogo.ca/acer/travelmate-3002wtmi.htm acer travelmate 3002wtmi battery , http://www.shopgogo.ca/acer/travelmate-3200.htm acer travelmate 3200 battery , http://www.shopgogo.ca/acer/lip6179qupcsy6.htm acer lip6179qupcsy6 battery , http://www.shopgogo.ca/acer/batbl50l6.htm acer batbl50l6 battery , http://www.shopgogo.ca/acer/travelmate-4200.htm acer travelmate 4200 battery , http://www.shopgogo.ca/acer/aspire-5100.htm acer aspire 5100 battery , http://www.shopgogo.ca/acer/aspire-3690.htm acer aspire 3690 battery , http://www.shopgogo.ca/acer/aspire-5680.htm acer aspire 5680 battery , http://www.shopgogo.ca/acer/aspire-9120.htm acer aspire 9120 battery , http://www.shopgogo.ca/acer/um08a71.htm acer um08a71 battery , http://www.shopgogo.ca/acer/black-aspire-one-a110l-series.htm acer black aspire one a110l series battery , http://www.shopgogo.ca/acer/black-aspire-one-a150l-series.htm acer black aspire one a150l series battery , http://www.shopgogo.ca/acer/black-aspire-one-a150x-series.htm acer black aspire one a150x series battery , http://www.shopgogo.ca/acer/black-aspire-one-zg5.htm acer black aspire one zg5 battery , http://www.shopgogo.ca/acer/um08a72.htm acer um08a72 battery , http://www.shopgogo.ca/acer/blue-aspire-one-a110l-series.htm acer blue aspire one a110l series battery , http://www.shopgogo.ca/acer/blue-aspire-one-a150l-series.htm acer blue aspire one a150l series battery , http://www.shopgogo.ca/acer/blue-aspire-one-a150x-series.htm acer blue aspire one a150x series battery , http://www.shopgogo.ca/acer/blue-aspire-one-zg5.htm acer blue aspire one zg5 battery , http://www.shopgogo.ca/acer/um08a73.htm acer um08a73 battery , http://www.shopgogo.ca/acer/white-aspire-one-a110l-series.htm acer white aspire one a110l series battery , http://www.shopgogo.ca/acer/white-aspire-one-a150l-series.htm acer white aspire one a150l series battery , http://www.shopgogo.ca/acer/white-aspire-one-a150x-series.htm acer white aspire one a150x series battery , http://www.shopgogo.ca/acer/white-aspire-one-zg5.htm acer white aspire one zg5 battery , http://www.shopgogo.ca/acer/um08a74.htm acer um08a74 battery , http://www.shopgogo.ca/acer/black-aspire-one-a110l-series.htm acer black aspire one a110l series battery , http://www.shopgogo.ca/acer/black-aspire-one-a150l-series.htm acer black aspire one a150l series battery , http://www.shopgogo.ca/acer/black-aspire-one-a150x-series.htm acer black aspire one a150x series battery , http://www.shopgogo.ca/acer/black-aspire-one-zg5.htm acer black aspire one zg5 battery , http://www.shopgogo.ca/acer/um08b72.htm acer um08b72 battery , http://www.shopgogo.ca/acer/blue-aspire-one-a110l-series.htm acer blue aspire one a110l series battery , http://www.shopgogo.ca/acer/blue-aspire-one-a150l-series.htm acer blue aspire one a150l series battery , http://www.shopgogo.ca/acer/blue-aspire-one-a150x-series.htm acer blue aspire one a150x series battery , http://www.shopgogo.ca/acer/blue-aspire-one-zg5.htm acer blue aspire one zg5 battery , http://www.shopgogo.ca/acer/um08b73.htm acer um08b73 battery , http://www.shopgogo.ca/acer/white-aspire-one-a110l-series.htm acer white aspire one a110l series battery , http://www.shopgogo.ca/acer/white-aspire-one-a150l-series.htm acer white aspire one a150l series battery ,

  5. Unknown says:

    Serena Williams and the season of her contentwow gold,world of warcraft gold,buy wow gold,wow power leveling,cheap wow gold,world of warcraft power
    leveling,world of warcraft gold,buy wow
    gold,ffxi gil,buy
    wow gold,wow power
    leveling,ffxi gil,world
    of warcraft power leveling,sell wow gold,wow power
    level,wow gold for
    sale,power leveling,wow
    power level,wow power level,power leveling,wow gold for
    sale,buy cheap wow gold.Indian brides confront gold bears, again

  6. Unknown says:

    wow gold wow gold wow gold Silkroad gold wow power leveling wow power leveling wow power leveling warhammer gold warhammer gold warhammer gold buy warhammer gold buy warhammer gold buy warhammer gold warhammer gold warhammer gold warhammer gold buy warhammer gold buy warhammer gold buy warhammer gold Lotro gold Lotro gold Lotro gold Ffxi gil Ffxi gil Ffxi gil Maple story mesos Maple story mesos Maple story mesos buy wow gold buy wow gold buy wow gold cheap wow gold cheap wow gold cheap wow gold Lotr gold Lotr gold Lotr gold Aoc gold Aoc gold Aoc gold Age of conan gold Age of conan gold Age of conan gold Aoc power leveling Aoc power leveling Aoc power leveling cheap ffxi gil cheap ffxi gil cheap ffxi gil Final fantasy xi gil Final fantasy xi gil Final fantasy xi gil Flyff penya Flyff penya Flyff penya Maple story meso Maple story meso Maple story meso Maplestory mesos Maplestory mesos Maplestory mesos Runescape gold Runescape gold Runescape gold Runescape money Runescape money Runescape money Silkroad gold Silkroad gold Silkroad gold Lotro gold Maple story mesos Aoc gold warhammer gold Lotro gold Maple story mesos

Comments are closed.