melt日記

.NETすらまともに扱えないへたれのページ

ホーム 連絡をする 同期する ( RSS 2.0 ) Login
投稿数  111  : 記事  3  : コメント  1439  : トラックバック  41

ニュース

わんくま同盟

わんくま同盟

C# と VB.NET の質問掲示板

iKnow!


Dictation



書庫

Worker Thread パターンは、スレッドをあらかじめ起動しておくことにより、スレッドの起動するコストを削減することと、スレッドの数を制御することが出来るパターンです。

Boost.Function を使ってある程度汎用的にするとこんな感じになりました。


// Worker Thread
class thread_pool
{
private:
    std::queue<boost::function0<void> > queue_;
    boost::thread_group group_;
    boost::mutex mutex_;
    boost::condition condition_;

public:
    thread_pool(int size)
    {
        for (int i = 0; i < size; i++)
        {
            group_.create_thread(boost::bind(&thread_pool::run, this));
        }
    }

    void put(boost::function0<void> f)
    {
        boost::mutex::scoped_lock lock(mutex_);
        queue_.push(f);
        condition_.notify_all();
    }


private:
    boost::function0<void> take()
    {
        boost::mutex::scoped_lock lock(mutex_);

        while (queue_.empty())
        {
            condition_.wait(lock);
        }

        boost::function0<void> f = queue_.front();
        queue_.pop();
        return f;
    }

    void run()
    {
        while (true)
        {
            take()();
        }
    }
};

// test
boost::mutex g_mutex;

void func(const std::string& str, int num, int count)
{
    int n = 0;
    for (int i = 0; i < num; i++)
    {
        n += count;
        {
            boost::mutex::scoped_lock lock(g_mutex);
            std::cout << (boost::format("%1% %2%/%3% (%4%)") % str % i % num % ::GetCurrentThreadId()) << std::endl;
        }
        ::Sleep(1000);
    }
    {
        boost::mutex::scoped_lock lock(g_mutex);
        std::cout << (boost::format("%1% %2%/%2% (%3%) result: %4%") % str % num % ::GetCurrentThreadId() % n) << std::endl;
    }
}


class hoge
{
private:
    int num_;
    int count_;

public:
    hoge(int num, int count) : num_(num), count_(count) { }
    void func(const std::string& str) { ::func(str, num_, count_); }
};


void main()
{
    thread_pool tp(3);
    tp.put(boost::bind(func, "request1:", 5, 10));
    tp.put(boost::bind(func, "request2:", 5, 10));
    ::Sleep(3000);
    hoge h(3, 5);
    tp.put(boost::bind(&hoge::func, &h, "request3:"));
    tp.put(boost::bind(&hoge::func, &h, "request4:"));
    ::Sleep(7000);
}

// 実行結果
request1: 0/5 (3756)
request2: 0/5 (2708)
request1: 1/5 (3756)
request2: 1/5 (2708)
request1: 2/5 (3756)
request2: 2/5 (2708)
request3: 0/3 (1960)             // スレッドプールの空きがないので request4 は開始しない
request1: 3/5 (3756)
request2: 3/5 (2708)
request3: 1/3 (1960)
request1: 4/5 (3756)
request2: 4/5 (2708)
request3: 2/3 (1960)
request1: 5/5 (3756) result: 50
request2: 5/5 (2708) result: 50
request4: 0/3 (3756)             // スレッドプールの空きが出来たので request4 が開始された
request3: 3/3 (1960) result: 15
request4: 1/3 (3756)
request4: 2/3 (3756)
request4: 3/3 (3756) result: 15

ほんとは thread_pool クラスのスレッドを安全に終了するための処理が必要なのですが、難しそうなのでとりあえず保留しています。


発展した考えとしては、スレッドの数を動的に変更したり、キューにためられる数に制限を付けたり、キューに制限がある場合に Guarded Suspension パターンで待つか Balking で待つかを変更したりと、いろんな考えがあると思います。


これらをうまく汎用的に作りたいのですが、なかなかうまい方法が思いつきません。

やっぱり用途に応じて作り直した方がいいのかなぁ……って Future の時と同じことを考えてるおいらガイル。

投稿日時 : 2008年2月16日 7:51

コメント

# I'll try to put this to good use immeditealy. 2012/01/28 23:49 Mitchell
I'll try to put this to good use immeditealy.

# I'll try to put this to good use immeditealy. 2012/01/28 23:49 Mitchell
I'll try to put this to good use immeditealy.

# I'll try to put this to good use immeditealy. 2012/01/28 23:49 Mitchell
I'll try to put this to good use immeditealy.

# re: すべての user で扱う file の配置場所 2016/02/23 13:01 leilei3915
2016-2-23 leileiコメント

http://www.airforce1-nike.fr
http://burberry.shopgioia.com
http://www.truereligion-outlet.in.net
http://www.michael--korsoutlet.us.com
http://www.adidas-trainers.me.uk
http://www.ralph-lauren.me.uk
http://www.mcmoutlet.us
http://www.chiflatiron.in.net
http://www.toms-outlet.cc
http://www.adidasgazelle.me.uk
http://www.canadagoosejackets.org.uk
http://www.montblanc-pens.name
http://www.edhardyukclothing.me.uk
http://www.pradaoutlet.name
http://www.fitflops.us
http://www.coach-factoryoutlet.com.co
http://www.jordan13.org
http://burberryoutlet.outletmalls.us.com
http://www.michaelkorsoutletoff.in.net
http://burberry.eshopping.us.com
http://www.tomsoutletstore.in.net
http://www.cheapjordans.com.co
http://www.polooutlet.us.com
http://www.michaelkors-outlet-handbags.com
http://www.nikeblazers.me.uk
http://www.poloralphlauren.net.co
http://www.hermesuk.org.uk
http://www.jordan3.net
http://www.cheap-uggsonsale.in.net
http://www.edhardy-outlet.name
http://www.tomsshoes.us.com
http://www.coachoutletonlinespick.org
http://www.michaelkors-handbag.me.uk
http://www.truereligion-outlet.net.co
http://michaelkors.adsmall.us.com
http://northface.outletmalls.us.com
http://www.michaelkors-mkoutlet.in.net
http://www.true-religionjeans.net.co
http://burberry.outletmalls.us.com
http://www.timberlanduk.me.uk
http://www.louis-vuitton-outlet.us.com
http://www.louboutinshoes.cc
http://www.rolex-replicawatches.com.co
http://tiffany.outletmalls.us.com
http://coach.adsmall.us.com
http://www.cheapwholesaleuggboots.com
http://www.toms.in.net
http://oakley.eshopping.us.com
http://www.jordan4.net
http://www.canadagooseuk.me.uk
http://www.ugg-boots-store.com
http://www.longchampoutlet.net.co
http://www.airmax95.org.uk
http://www.sac-longchamppliage.fr
http://www.niketrainers.com.co
http://www.outlet-toms.cc
http://www.michaelkorsukbag.co.uk
http://abercrombie.shopgioia.com
http://www.gucci-borse.it
http://www.pandoracharms.com.co
http://www.coachouletbtf.com
http://christianlouboutin.outletmalls.us.com
http://www.nike-tn-pascher.fr
http://www.kate-spadeoutlet.us
http://www.jordan-femmepascher.fr
http://michaelkors.eshopping.us.com
http://www.chaussurelouboutin-pascher.fr
http://www.ralph-lauren-pas-cher.fr
http://www.quality-outlet.com
http://www.ghdhairstraighteners.com.co
http://www.michael-kors-handbags.uk
http://www.nikesb.uk
http://www.mcmhandbags.name
http://www.jordan6.net
http://www.instylermax.in.net
http://www.fitflops.in.net
http://www.tomswedges.in.net
http://www.nikeuk.me.uk
http://nike.outletmalls.us.com
http://www.adidas-shoes.me.uk

# 人気スーパーコピーブランド時計激安通販専門店 2016/02/29 9:01 q0ds3bm953
http://www.msnbrand.com/goods-copy-4805.html
日本超人気のスーパーコピーブランド激安通販専門店!┏━━━━━2016年人気最高品質━━━━━┓ ◎━ 腕時計、バッグ、財布、アクセサリ ━◎最新入荷!主に販売商品:ロレックスコピー、カルティエコピー、エルメスコピー、ブラダコピー、ルイヴィトンコピー、シャネルコピー、グッチコピー等一流のブランドコピー品。商品の数量は多い、品質はよい、高品質のブランドコピーを超激安な価格で販売しています。信用第一、商品は絶対の自信が御座います!驚きと満足を保証致します。ぜひご来店くださいませ!★ 2016年注文割引開催中,全部の商品割引10% ★ 在庫情報随時更新! ★ 実物写真、付属品を完備する。 ★ 100%品質を保証する。 ★ 送料は無料です(日本全国)!★ 経営方針: 品質を重視、納期も厳守、信用第一!税関の没収する商品は再度無料にして発送します!}}}}}}

# re: Boost によるデザインパターン - Worker Thread 2016/10/07 17:47 CC
http://www.rayban-sunglasses.uk
http://www.kobebasketballshoes.net
http://www.nikedunks.us.org
http://www.fitflops.cc
http://www.fitflopsoutlet.in.net
http://www.adidasnmdus.com
http://www.yeezyboost350.uk
http://www.tomsoutletstore.name
http://www.jewellerytiffanyand.co
http://www.michaelkorsoutletonline.uk
http://www.boostyeezy.us.com
http://www.lacostepoloshirts.us.com
http://www.christianlouboutin.in.net
http://www.adidasneo.us.com
http://www.skechers-shoes.us.com
http://www.louboutinshoes.uk
http://www.tiffanyandco-outlet.us.com
http://www.michaelkors-outletfactory.us.org
http://www.yeezy-boost350.us.com
http://www.jordansforcheap.us.com
http://www.fitflops-saleclearance.us.com
http://www.hoganoutletonline.us.com
http://www.linksoflondonus.com
http://www.raybanglasses.in.net
http://www.tiffanyandcojewellery.us.com
http://www.chromehearts.com.co
http://www.linksoflondon.us.com
http://www.verabradleyoutlet.us.com
http://www.ugg.name
http://www.cheapnfljerseysstorechina.com
http://www.raybansunglass.uk
http://www.truereligiononsale.in.net
http://www.adidastubular.co.uk
http://www.cheapauthenticjordans.us.com
http://www.oakley-sunglass.in.net
http://www.tiffanyandcouk.uk
http://www.louis-vuittonhandbags.net
http://www.sketchersoutlet.us
http://www.cheaptiffanyandco.in.net
http://www.michaelkorsonline.net
http://www.jordanshoes.com.co
http://www.cheaptruereligionjeans.us
http://www.authenticjordanscheap.us.com
http://www.michaelkorsonline.name
http://www.oakleyvault.com.co
http://www.airhuarache.uk
http://www.yeezyboost.com.co
http://www.cheapbasketballshoes.us.com
http://www.adidasstansmith.uk
http://www.michaelkors-outletsonline.us.com
http://www.michaelkors-outletstore.us.com
http://www.adidasnmdus.com
http://www.oakleyoutlet-store.com
http://www.kobesneakers.com
http://www.skechersshoes.us.org
http://www.michaelkorshandbagsclearance.com.co
http://www.timberlandoutlet.uk
http://www.huarachesshoes.co.uk
http://www.michael-kors-handbags.org.uk
http://www.airmaxthea.us.com
http://www.raybansunglassesoutlet.org
http://www.adidassuperstar.org.uk
http://www.outlettiffanyandco.us.com
http://www.michaelkorshandbags.net
http://www.cheapairjordan.in.net
http://www.kobe-shoes.us.com
http://www.outlettiffanyand.co
http://www.michaelkors-factoryoutlets.us.com
http://www.oakleystoreonline.us.org
http://www.uggoutlet.uk
http://www.michaelkors-handbags.net
http://www.discount-oakleysunglasses.us.com
http://www.nikezoom.me.uk
http://www.cheaprealjordans.us.com
http://www.yeezy-shoes.us
http://www.nfljerseyscheap.in.net
http://www.jewelrytiffanyand.co
http://www.nikepolo.us
http://www.raybansunglass.us
http://www.adidasnmdrunner.us.com
http://www.cheap-airjordans.us.com
http://www.michaelkors-outletonlines.us.com
http://www.kobebasketballshoes.us.com
http://www.cheap--jordans.us.com
http://www.oakley-sunglasses.co
http://www.lacosteoutlet.us.org
http://www.michaeljordanshoes.us.com
http://www.tiffany.net.co
http://www.adidasnmd.com.co
http://www.skechersshoes.org.uk
http://www.airjordanretro.uk
http://www.chromeheart.us.com
http://www.michaelkors-handbagsclearance.us.com
http://www.cheapralphlaurenonline.uk
http://www.outletonline-michaelkors.us.org
http://www.skechersoutlet.us.org
http://www.nikerosheone.us.com
http://www.zoomkb24.uk
http://www.michaelkorsoutlets-online.eu.com
http://www.kobeshoes.uk
http://www.adidassuperstar.us.com
http://www.airmaxthea.uk
http://www.lacosteoutlet.org.uk
http://www.cheapretro-jordans.com
http://www.outlettimberland.us.com
http://www.zoomkb24.com
http://www.oakleysunglassesoutlet.us
http://www.huaracheshoes.uk
http://www.rosherun.us.com
http://www.nikerosheone.co.uk
http://www.cheapairjordan.uk
http://www.cheaprolexwatchesonline.in.net
http://www.michaelkors-outletonline.co.uk
http://www.nfljerseyswholesale.in.net
http://www.hermes-belt.us.com
http://www.coach-outlet.in.net
http://www.jordanretro.uk
http://www.oakleyglasses.com.co
http://www.adidasstansmith.us.com
http://www.cheapairjordan.us
http://www.airjordan.cc
http://www.mlbjerseyswholesale.in.net
http://www.airhuaraches.org.uk
http://www.nike-airhuarache.org.uk
http://www.nikezoom.us.com
http://www.nfljerseys2015.in.net
http://www.michael-kors-handbags.com.co
http://www.chromehearts.us.com
http://www.adidastubular.us.com
http://www.adidasnmd.uk
http://www.huarachesshoes.uk
http://www.tiffanyand.co.uk
http://www.nike-airhuarache.uk
http://www.mlbjerseysauthentic.in.net
http://www.yeezyshoes.uk
http://www.hermesbelt.org.uk
http://www.cheapralphlauren.in.net
http://www.nmdadidas.us.com
http://www.hoganoutlet.in.net
http://www.michaeljordanshoes.net
http://www.tiffanyandcooutletonline.us.com
JEJE

# re: Boost によるデザインパターン - Worker Thread 2016/10/29 11:44 20161029xyw

http://www.nikeairmaxoutletsite.com
http://www.converseshoessaleclearance.com
http://www.guess--store.com
http://www.coachoutletonlinecoachfactoryoutlet.net
http://www.coachbags.in.net
http://www.columbia-sportswear.org
http://www.2016michaelkorsoutlet.com
http://www.michaelkorsoutletonlinestore4900outlet.com
http://www.cheapcoachoutletwebsite.com
http://www.clarks-shoes.in.net
http://www.coachdiaperbag.us
http://www.nikeairmax90.name
http://www.coach-factory--outlet.name
http://www.coachfactory-outlet.eu.com
http://www.newbalanceshoes-outlet.net
http://www.nike-outlet-store.com
http://www.cheapmichaelkorshandbags.name
http://www.cheapnfljerseyssite.com
http://www.uggbootsoutlet-70off.in.net
http://www.michaelkorsoutlet.net.in
http://www.yeezyboost-350.net
http://www.jordan-shoes.net.co
http://www.columbiaoutlet-store.com
http://www.north-face-outlet.name
http://www.fitflopoutletonline-inc.com
http://www.coachfactoryoutletonlinestore.us.com
http://www.fitflopshoessite.com
http://www.salvatore-ferragamo-outlet.in.net
http://www.uggbootsonsale-70off.in.net
http://www.nikebasketball-shoes.com
http://www.outlet-guess.com
http://www.newbalance-shoesforwomen.com
http://www.michaelkors-bags.name
http://www.christianlouboutinsale.com.co
http://www.adidasshoes-outlet-store.com
http://burberryoutletonline.officialoutletstore.com
http://www.salomon-shoes.in.net
http://www.nike-free-run.com
http://www.michaelkorsoutletonlinesale.us
http://www.coachfactoryoutletonlinewebsite.com
http://www.clarksoutlet.in.net
http://www.jordan14-shoes.com
http://www.shoes-clark.net
http://www.prada--outlet.com
http://www.newbalance-outlet.us.com
http://www.outletcoachfactory.us.com
http://www.celine--outlet.com
http://www.fitflopfootwearoutlet.com
http://www.michaelkors-sac.fr
http://www.vans-shoes.name
http://www.juicycouture-handbags-outlet.com
http://www.mbt-shoes.in.net
http://www.nikeoutlet-stores.com
http://www.coachwallets.in.net
http://www.nikehuarache.name
http://www.joesnewbalance.in.net
http://www.christian-louboutinshoes.net
http://www.coachsunglasses-purse.com
http://www.coachdiaperbag-outletstores.com
http://www.outlet-tiffany.in.net
http://www.northfaceoutlet.net.co
http://www.saclongchamp.name
http://www.ferragamoshoes.us.com
http://www.jimmychooshoes.name
http://www.columbia-jackets.in.net
http://www.cheap-nikeshoes-clearance.com
http://www.coachoutletstoreonlineclearances.net
http://www.ferragamobelt.us.com
http://www.cheap--uggs.com
http://www.pandora-jewelry.name
http://www.burberry--outlet.name
http://www.celine--handbags.com
http://www.burberryscarf-outlet.in.net
http://www.christianlouboutinredbottomshoessite.com
http://www.jordans-for-women.com
http://www.handbags-michaelkors.us.com
http://www.coachoutlet.net.in
http://www.nikeairforce1.name
http://www.michael--kors--outlet.co.uk
http://www.timberlandboots-forwomen.com
http://www.uggbootsonsale70off.name
http://www.juicycoutureoutletonline-inc.com
http://www.ralphlaurenoutlet--online.com
http://www.airjordanofficialsite.com
http://www.coachfactory.xyz
http://www.airjordan-6.us
http://www.christianlouboutin-shoes.net.co
http://www.louboutinoutlet.eu.com
http://www.airjordan-shoes.us.com
http://www.swarovskioutlet.in.net
http://www.coach-wallets.com
http://www.canadagoosesale.com.co
http://www.northfacejacket.com.co
http://www.sac-longchamppas-cher.fr
http://www.michaelkorswallets.us
http://www.lacosteoutlet.in.net
http://www.airjordan-retro6.com
http://www.michaelkorsoutlet70off.org
http://www.hermesbirkinbag.us.com
http://www.columbiasportswearoutlet-inc.com
http://www.clarksshoesoutlet-online.com
http://www.rolexwatchesoutlet-inc.com
http://www.christianlouboutinshoes-outlet.com
http://www.nike-high-heels-online.com
http://www.michael--kors--handbags.co.uk
http://www.michaelkors-outlet.site
http://www.airjordan-13retro.com
http://www.cheap-jordans-shoes.org
http://www.cheapmichaelkorshandbags-clearance.com
http://www.coachfactoryoutlet85off.com
http://www.fitflopssandalsforwomen.com
20161029xyw


Post Feedback

タイトル
名前
Url:
コメント