melt日記

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

ホーム 連絡をする 同期する ( RSS 2.0 ) Login
投稿数  111  : 記事  3  : コメント  747  : トラックバック  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: [Windows Mobile]USBキーボードを動かす程度の能力 2017/07/22 19:01 meadc
http://www.ray-ban-sunglasses.us ray-ban sunglasses
http://www.coach-factory-outlet.net coach outlet
http://www.puresoie.fr/ Nike Air Max 90
http://www.katespade.org.au Kate Spade - Online Sale
http://www.coachcarter.com.au coach carter
http://www.mizunoshoes.us Wave Prophecy 2 Shoes
http://www.pandora-rings.co.uk pandora uk
http://www.top-sunglasses.org top sunglasses
http://www.pumashoes.us/ puma shoes
http://www.salomon-shoes.com salomon sneakers
http://www.grandregency.fr/ Air Max Enfants
http://www.swatchwatch.us/ swatch watch
http://www.premierjewelry.us Premier Jewelry - Official
http://www.rayban-outlet.co/ ray ban outlet
http://www.pandoracharms.cc/ pandora charms
http://www.meteo-curtafond.fr/ Nike Air Jordan Enfants
http://www.prada-sunglasses.us Prada Sunglasses - Official
http://www.michael-kors.uk michael kors watches
http://www.artsensuel.fr Nike Air Jordan
me adc7.22WERF

# ゼニス時計 2017/10/12 22:25 wgoomyws@hotmail.co.jp
こちらのお店は初めての利用なので、画像だけで判断して注文するのは少し迷いましたが、とても綺麗なダイヤの素敵なネックレスが届きました。
梱包も丁寧で、信頼できるショップだな…と感じました。
また機会がありましたら、よろしくお願いいたします。

# re: ASP.NET MVCはフロントコントローラー方式 2017/10/16 15:21 dongdong8
http://www.montblanc-pens.name
http://www.adidasyeezy-boost.us.com
http://www.uggclearancesale.in.net
http://www.nikehuarache.net
http://www.michaelkors-handbags.us
http://www.christianlouboutinsale.co.uk
http://www.outletugg.com.co
http://www.nikerunningshoes.co.uk
http://www.newmichaelkorsoutlet.us.com
http://www.cheap-mlb-jerseys.us.com
http://www.adidastore.co.uk
http://www.uggoutletstore.net.co
http://www.longchamp-outletstore.us.com
http://www.poloshirt.in.net
http://www.nikerosherunsale.co.uk
http://www.uggbootsoutlets.in.net
http://www.northfaceoutletstore.us.org
http://www.cheapjerseysnflwholesale.us
http://www.hermesoutlets.us.com
http://www.jordans-fusion.com
http://www.air-jordanpascher.fr
http://www.pandora-jewelry.name
http://www.vansshoesoutlet-store.com
http://www.ugg-australia.com.co
http://www.ugg.ind.in
http://www.nmdadidas.co.uk
http://www.ugg-boots.name
http://www.uggoutlet.nom.co
http://www.converseshoesoutlet.us.com
http://www.adidastrainersuk.uk
http://www.pandorajewelryoutlet.us.org
http://www.raybanpascher.fr
http://www.uggfactoryoutlet.in.net
http://www.hollisterclothing.us.org
http://www.truereligion-outletjeans.us.com
http://dolceandgabbana.sunglassescheap.us.com
http://www.canadagooseclearance.us.com
http://www.ukuggboots.co.uk
http://www.coachoutletstoreonlineofficial.us.com
http://www.nike-airmaxuk.me.uk
http://www.thenorthfacecanada.ca
http://www.pandora-jewelry.me.uk
http://www.uggbootsclearances.com.co
http://www.michaelkorsoutletonline80off.us.com
http://www.wwwuggaustralia.co.uk
http://www.ray-bansunglasses2017.us.com
http://www.cheaplouboutinshoesuk.org.uk
http://www.outletuggboots.com.co
http://www.michaelkors-outletonline.eu.com
http://www.cheapraybansunglasses.net.co
http://www.canadagoosecanada.com.co
http://www.katespadehandbagsoutlet.us.org
http://www.canadagoosecanada.ca
http://www.nikefootballbootsuk.org.uk
http://www.ugg.org.uk
http://www.hermesbags.me.uk
http://www.cheap-uggboots.com.co
http://www.uggbootsonsale70off.com.co
http://www.fitflops.us.com
http://www.uggbootsclassic.ca
http://www.niketrainers.org.uk
http://www.uggboots70off.in.net
http://www.wholesale-nfl-jerseys.us
http://www.suprashoesclearance.us.com
http://www.chaussureschristianlouboutin.fr
http://www.north-face-outlet.in.net
http://www.manoloblahnik.us
http://www.katespade-outlet.in.net
http://www.thenorthface-outlet.in.net
http://www.doudounecanada-goose.fr
http://www.cheapmlbjerseysshop.us.com
http://www.katespadeoutletsale.us.com
http://www.jordanshoes.us.org
http://www.cheapmichaelkorshandbags.us.org
http://www.uggfed.com
http://www.burberryoutletoff.us.com
http://www.cheapnfljerseysshop.us.com
http://www.burberry.us.org
http://www.coachoutletonline70off.us.com
http://www.coach-outlet.nom.co
http://www.cheapjordansshoes.us.com
http://www.uggbootsclearances.com.co
http://www.christianlouboutinoutletuk.org.uk
http://www.poloralphlaurenoutletoff.us.com
http://www.canadagoosesale.com.co
http://www.pradahandbagsoutlet.us.com
http://www.ralphlauren.eu.com
http://www.ugg-italia.it
http://www.coachoutletonlineshopping.us.org
http://www.uggbootsoutletcanada.ca
http://www.burberryscarfsale.in.net
http://www.poloralphlaurenoutletstores.us.com
http://www.cheap-jordan-shoes.us.com
http://www.longchamp-handbags.us.org
http://dior.sunglassescheap.us.com
http://www.christianlouboutin.name
http://www.michaelkorsoutletoff.us.com
http://www.northfacejacket.us.com
http://www.newcoachoutlet.us.com
http://www.uggbootsonsale70off.us.org
http://www.stansmithadidas.us.com
http://www.pradaoutletonlinestore.us.com
http://www.nikeoff.com
http://www.uggbootsclearance.net.co
http://www.outletpolo-ralphlauren.us.com
http://www.cheapjordansshoes.in.net
http://www.michaelkorshandbags.nom.co
http://www.uggsoutlet.org
http://www.coachcom.us.com
http://www.timberlandoutlet.name
http://armani.sunglassescheap.us.com
http://www.outletugg.ca
http://www.ralphlaurenoutlet-poloshirts.us.com
http://www.ray-bansunglassesoutlet.com.co
http://www.nikeairforce1.net
http://www.uggoutletsonline.us.com
http://www.truereligionjeans-outlet.us.com
http://www.cheap-ugg-boots.us.org
http://www.pololaurenshirts.com
http://www.katespadehandbagssale.us.com
http://www.coachoutlet-factory.us.com
http://www.fitflopsoutlet.us.com
http://www.adidasshoesoutletstore.us.com
http://www.pandoracharmjewelry.us.com
http://www.canadagoose.com.co
http://www.outletthenorthface.us.com
http://www.northfaceclearance.us.com
http://www.cheapnfljerseyswholesale.us.com
http://www.beatsbydreheadphones.in.net
http://www.michaelkors--outlet.us.org
http://www.uggoutlet70off.com.co
http://www.uggsoutletstores.in.net
http://www.converse-outletstore.com
http://www.michael--korshandbags.us.com
http://www.uggslippers.us.org
http://www.juicycoutureoutlet.us.com
http://www.ray-bansunglassesoutlets.us.com
http://www.ugg.com.co
http://www.uggonsale.in.net
http://www.truereligion-jeans.com.co
http://www.michaelkors--outlet.us.com
http://www.coachoutlet-online.eu.com
http://www.adidaswings.name
http://www.adidasshoessale.in.net
http://www.hermesbirkinhandbags.us.com
http://www.ray-ban-sunglasses.com.au
http://www.louisvuittonhandbags.nom.co
http://www.montblanc.com.co
http://www.louisvuitonnoutlet.com
http://www.truereligionsale.com.co
http://www.vibramfivefingerssale.com
http://www.uggsaustraliaboots.us.com
http://www.coachfactoryoutletonlinesale.us.com
http://prada.sunglassescheap.us.com
http://www.pololaurenralphlauren.us.com
http://www.underarmouroutletclearance.com
http://www.nikefactory-outlet.us.com
http://www.supremeclothing.us.com
http://www.hollistercooutlet.us.com
http://www.uggbootsonsale70off.net.co
http://www.nfljerseysfromchina.us.com
http://www.coachoutlet70off.us.com
http://www.katespadeoutletstore.us.org
http://www.canadagoosecanada.name
http://www.wholesale-nfljerseys.us.com
http://www.blackhawksjersey.com
http://www.uggbootsoutletclearance.us.com
http://www.superdry.me.uk
http://www.coachoutletfactoryon.com
http://www.uggsadd.com
http://carrera.sunglassescheap.us.com
http://www.christianlouboutinoutletusa.us.com
http://www.salvatoreferragamo.in.net
http://www.cheapairjordans.us.com
http://cartier.sunglassescheap.us.com
http://www.oakleysunglassessport.us.com
http://www.redbottoms.org.uk
http://www.ralphlaurenpolooutlet.us.org
http://www.coach-factoryoutletusa.com
http://www.nike-factoryoutlet.us.com
http://www.truereligionuk.org.uk
http://www.fitflops.org
http://www.burberryoutlet-canada.ca
http://www.uggsoutletstore.org
http://www.fitflopssaleclearance.us.org
http://www.michaelkorsoutlet70off.us.com
http://www.pandorajewelrycanada.ca
http://www.canadagoosesale.name
http://www.nikeairjordans.co.uk
http://www.snapbackshatswholesale.us.com
http://www.toryburchhandbagsoutlet.us.com
http://www.uggsclassicboots.us.com
http://www.saleuggboots.co.uk
http://www.uggsonline.in.net
http://www.outletlongchamp.us.org
http://www.clearancemichaelkorsoutlet.us.com
http://www.airjordan-14.us
http://www.cheapraybans.ca
http://www.montblanc-pens.name
http://www.adidasyeezy-boost.us.com
http://www.uggclearancesale.in.net
http://www.nikehuarache.net
http://www.michaelkors-handbags.us
http://www.christianlouboutinsale.co.uk
http://www.outletugg.com.co
http://www.nikerunningshoes.co.uk
201710.16wengdongdong


Post Feedback

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