東方算程譚

Oriental Code Talk ── επιστημηが与太をこく、弾幕とは無縁のシロモノ。

目次

Blog 利用状況

ニュース

著作とお薦めの品々は

著作とお薦めの品々は
東方熱帯林へ。

あわせて読みたい

わんくま

  1. 東京勉強会#2
    C++/CLI カクテル・レシピ
  2. 東京勉強会#3
    template vs. generics
  3. 大阪勉強会#6
    C++むかしばなし
  4. 東京勉強会#7
    C++むかしばなし
  5. 東京勉強会#8
    STL/CLRによるGeneric Programming
  6. TechEd 2007 @YOKOHAMA
    C++・C++/CLI・C# 適材適所
  7. 東京勉強会#14
    Making of BOF
  8. 東京勉強会#15
    状態遷移
  9. 名古屋勉強会#2
    WinUnit - お気楽お手軽UnitTest

CodeZine

  1. Cで実現する「ぷちオブジェクト指向」
  2. CUnitによるテスト駆動開発
  3. SQLiteで組み込みDB体験(2007年版)
  4. C++/CLIによるCライブラリの.NET化
  5. C# 1.1からC# 3.0まで~言語仕様の進化
  6. BoostでC++0xのライブラリ「TR1」を先取りしよう (1)
  7. BoostでC++0xのライブラリ「TR1」を先取りしよう (2)
  8. BoostでC++0xのライブラリ「TR1」を先取りしよう (3)
  9. BoostでC++0xのライブラリ「TR1」を先取りしよう (4)
  10. BoostでC++0xのライブラリ「TR1」を先取りしよう (5)
  11. C/C++に対応した、もうひとつのUnitTestFramework ─ WinUnit
  12. SQLiteで"おこづかいちょう"
  13. STL/CLRツアーガイド
  14. マージ・ソート : 巨大データのソート法
  15. ヒープソートのアルゴリズム
  16. C++0xの新機能「ラムダ式」を次期Visual Studioでいち早く試す
  17. .NETでマンデルブロ集合を描く
  18. .NETでマンデルブロ集合を描く(後日談)
  19. C++/CLI : とある文字列の相互変換(コンバージョン)
  20. インテルTBBによる選択ソートの高速化
  21. インテルTBB3.0 によるパイプライン処理
  22. Visual C++ 2010に追加されたSTLアルゴリズム
  23. Visual C++ 2010に追加されたSTLコンテナ「forward_list」
  24. shared_ptrによるObserverパターンの実装
  25. .NETでマンデルブロ集合を描く(番外編) ── OpenCLで超並列コンピューティング
  26. StateパターンでCSVを読む
  27. 状態遷移表からStateパターンを自動生成する
  28. 「ソートも、サーチも、あるんだよ」~標準C++ライブラリにみるアルゴリズムの面白さ
  29. インテルTBBの同期メカニズム
  30. なぜsetを使っちゃいけないの?
  31. WPFアプリケーションで腕試し ~C++でもWPFアプリを
  32. C++11 : スレッド・ライブラリひとめぐり
  33. Google製のC++ Unit Test Framework「Google Test」を使ってみる
  34. メールでデータベースを更新するココロミ
  35. Visitorパターンで遊んでみたよ
  36. Collection 2題:「WPFにバインドできる辞書」と「重複を許す検索set」
  37. Visual C++ 2012:stateless-lambdaとSQLiteのぷち拡張
  38. 「Visual C++ Compiler November 2012 CTP」で追加された6つの新機能

@IT

  1. Vista時代のVisual C++の流儀(前編)Vista到来。既存C/C++資産の.NET化を始めよう!
  2. Vista時代のVisual C++の流儀(中編)MFCから.NETへの実践的移行計画
  3. Vista時代のVisual C++の流儀(後編) STL/CLRによるDocument/Viewアーキテクチャ
  4. C++開発者のための単体テスト入門 第1回 C++開発者の皆さん。テスト、ちゃんとしていますか?
  5. C++開発者のための単体テスト入門 第2回 C++アプリケーションの効率的なテスト手法(CppUnit編)
  6. C++開発者のための単体テスト入門 第3回 C++アプリケーションの効率的なテスト手法(NUnit編)

AWARDS


Microsoft MVP
for Visual Developer - Visual C++


Wankuma MVP
for いぢわる C++


Nyantora MVP
for こくまろ中国茶

Xbox

Links

記事カテゴリ

書庫

日記カテゴリ

TBBで遊んでみたよ(2)

ネタ元 → 並列プログラミングの効率的なデバッグを実現する「Parallel Inspector」

いいかインドリ、まぢもんの「生産者/消費者のイディオム」っちゅーのはこぉやるんだよ!


/*
 cl -EHsc -openmp -MD -I<TBB_INC_DIR> prodcons.cpp -link -libpath:<TBB_LIB_DIR>
 */

#include <iostream>  // std::cout,endl
#include <cstdlib>   // std::rand
#include <tbb/tbb.h> // tbb::*

/*
 * 生産者
 */
class Producer : public tbb::task {
  tbb::concurrent_bounded_queue<int>& stock;
  tbb::mutex& mutex;
public:
  Producer(tbb::concurrent_bounded_queue<int>& s, tbb::mutex& m) : stock(s), mutex(m) {}
  tbb::task* execute() {
    int remains = 1000; // 残り生産量
    while ( remains > 0 ) {
      int produced = rand()%100; // 生産量
      if ( remains < produced ) produced = remains; // 作りすぎないように
      remains -= produced;
      {
        tbb::mutex::scoped_lock lock(mutex); // 出力中、mutexでロック
        std::cout << "produce " << produced << ", "
                  << remains << " remains." << std::endl;
      }
      stock.push(produced);
    }
    stock.push(-1); // terminator
    std::cout << "end of produce.\n";
    return 0;
  }
};

class Consumer : public tbb::task {
  tbb::concurrent_bounded_queue<int>& stock;
  tbb::mutex& mutex;
public:
  Consumer(tbb::concurrent_bounded_queue<int>& s, tbb::mutex& m) : stock(s), mutex(m) {}
  tbb::task* execute() {
    int consumed = 0; // 消費総量
    int onstore = 0; // 店頭在庫
    int n = 0;
    while ( n >= 0 ) {
      int tobeconsume = rand()%100; // 消費量
      while ( onstore < tobeconsume ) { // 店頭に十分な在庫が確保できるまで
        stock.pop(n); // 在庫から取り出す
        if ( n < 0 ) { // 生産終了したならおしまい
          tobeconsume = onstore;
          break;
        }
        onstore += n; // 店頭在庫を増やす
      }
      onstore -= tobeconsume; // 販売
      consumed += tobeconsume;
      {
        tbb::mutex::scoped_lock lock(mutex); // 出力中、mutexでロック
        std::cout << "consume " << tobeconsume << ", "
                  << consumed << " consumed so far." << std::endl;
      }
    }
    std::cout << "end of consume.\n";
    return 0;
  }
};

class Market : public tbb::task {
  tbb::concurrent_bounded_queue<int> stock;
  tbb::mutex mutex;
public:
  Market(int stock_capacity) {
    stock.set_capacity(stock_capacity);
  }
  tbb::task* execute() {
    // 生産者と消費者を生成し、並列実行して双方の終了を待つ
    Producer& p = *new( allocate_child() ) Producer(stock,mutex);
    Consumer& c = *new( allocate_child() ) Consumer(stock,mutex);
    set_ref_count(3);
    spawn(c);
    spawn_and_wait_for_all(p);
    std::cout << "market closed." << std::endl;
    return 0;
  }
};

int main() {
  Market& market = *new(tbb::task::allocate_root()) Market(16);
  tbb::task::spawn_root_and_wait(market);
  std::cout << "demonstration over." << std::endl;
}


うわー、TBBスゲー!
ロクにマニュアル読まんでもexamplesの真似っこしてたらできちゃったー♪

投稿日時 : 2009年10月6日 23:31

コメントを追加

# I got this web site from my friend who shared with me on the topic of this website and at the moment this time I am visiting this site and reading very informative posts here. 2021/08/31 22:14 I got this web site from my friend who shared with

I got this web site from my friend who shared with
me on the topic of this website and at the moment this time I am visiting this site and reading
very informative posts here.

# I got this web site from my friend who shared with me on the topic of this website and at the moment this time I am visiting this site and reading very informative posts here. 2021/08/31 22:15 I got this web site from my friend who shared with

I got this web site from my friend who shared with
me on the topic of this website and at the moment this time I am visiting this site and reading
very informative posts here.

# I got this web site from my friend who shared with me on the topic of this website and at the moment this time I am visiting this site and reading very informative posts here. 2021/08/31 22:16 I got this web site from my friend who shared with

I got this web site from my friend who shared with
me on the topic of this website and at the moment this time I am visiting this site and reading
very informative posts here.

# I got this web site from my friend who shared with me on the topic of this website and at the moment this time I am visiting this site and reading very informative posts here. 2021/08/31 22:17 I got this web site from my friend who shared with

I got this web site from my friend who shared with
me on the topic of this website and at the moment this time I am visiting this site and reading
very informative posts here.

# Asking questions are in fact fastidious thing if you are not understanding anything fully, except this article provides good understanding yet. 2021/09/02 2:45 Asking questions are in fact fastidious thing if y

Asking questions are in fact fastidious thing if
you are not understanding anything fully, except this
article provides good understanding yet.

# Asking questions are in fact fastidious thing if you are not understanding anything fully, except this article provides good understanding yet. 2021/09/02 2:46 Asking questions are in fact fastidious thing if y

Asking questions are in fact fastidious thing if
you are not understanding anything fully, except this
article provides good understanding yet.

# Asking questions are in fact fastidious thing if you are not understanding anything fully, except this article provides good understanding yet. 2021/09/02 2:47 Asking questions are in fact fastidious thing if y

Asking questions are in fact fastidious thing if
you are not understanding anything fully, except this
article provides good understanding yet.

# Asking questions are in fact fastidious thing if you are not understanding anything fully, except this article provides good understanding yet. 2021/09/02 2:48 Asking questions are in fact fastidious thing if y

Asking questions are in fact fastidious thing if
you are not understanding anything fully, except this
article provides good understanding yet.

# Right here is the right website for everyone who wishes to find out about this topic. You understand so much its almost hard to argue with you (not that I really would want to…HaHa). You definitely put a fresh spin on a subject that has been discussed f 2021/09/02 18:09 Right here is the right website for everyone who w

Right here is the right website for everyone who wishes to find out about this
topic. You understand so much its almost hard to argue with you
(not that I really would want to…HaHa). You definitely put a fresh spin on a subject that has been discussed for ages.
Wonderful stuff, just wonderful!

# Right here is the right website for everyone who wishes to find out about this topic. You understand so much its almost hard to argue with you (not that I really would want to…HaHa). You definitely put a fresh spin on a subject that has been discussed f 2021/09/02 18:10 Right here is the right website for everyone who w

Right here is the right website for everyone who wishes to find out about this
topic. You understand so much its almost hard to argue with you
(not that I really would want to…HaHa). You definitely put a fresh spin on a subject that has been discussed for ages.
Wonderful stuff, just wonderful!

# Right here is the right website for everyone who wishes to find out about this topic. You understand so much its almost hard to argue with you (not that I really would want to…HaHa). You definitely put a fresh spin on a subject that has been discussed f 2021/09/02 18:12 Right here is the right website for everyone who w

Right here is the right website for everyone who wishes to find out about this
topic. You understand so much its almost hard to argue with you
(not that I really would want to…HaHa). You definitely put a fresh spin on a subject that has been discussed for ages.
Wonderful stuff, just wonderful!

# Right here is the right website for everyone who wishes to find out about this topic. You understand so much its almost hard to argue with you (not that I really would want to…HaHa). You definitely put a fresh spin on a subject that has been discussed f 2021/09/02 18:13 Right here is the right website for everyone who w

Right here is the right website for everyone who wishes to find out about this
topic. You understand so much its almost hard to argue with you
(not that I really would want to…HaHa). You definitely put a fresh spin on a subject that has been discussed for ages.
Wonderful stuff, just wonderful!

# Thanks for sharing your info. I really appreciate your efforts and I will be waiting for your further post thanks once again. 2021/09/05 17:45 Thanks for sharing your info. I really appreciate

Thanks for sharing your info. I really appreciate your efforts
and I will be waiting for your further post thanks once again.

# Wow! This blog looks exactly like my old one! It's on a totally different topic but it has pretty much the same page layout and design. Outstanding choice of colors! 2021/09/06 17:21 Wow! This blog looks exactly like my old one! It's

Wow! This blog looks exactly like my old one! It's on a totally different topic but it
has pretty much the same page layout and design. Outstanding choice of colors!

# Thanks for sharing your thoughts about C#. Regards quest bars http://bitly.com/3jZgEA2 quest bars 2021/09/10 21:26 Thanks for sharing your thoughts about C#. Regards

Thanks for sharing your thoughts about C#.
Regards quest bars http://bitly.com/3jZgEA2 quest
bars

# tadalafil online $114 2021/09/18 19:07 can priligy cure pe

Terazosin Overdose

# tadalafil retailers 2021/09/25 11:58 abobbergy

http://buylasixshop.com/ - hydrochlorothiazide vs lasix

# I got this web site from my friend who informed me concerning this web page and now this time I am visiting this website and reading very informative content at this time. 2021/10/26 5:21 I got this web site from my friend who informed me

I got this web site from my friend who informed me concerning this web
page and now this time I am visiting this website and reading very informative content at this time.

# May I simply say what a comfort to find an individual who truly knows what they're discussing on the net. You actually realize how to bring a problem to light and make it important. More people ought to check this out and understand this side of the sto 2021/12/12 18:42 May I simply say what a comfort to find an individ

May I simply say what a comfort to find an individual who
truly knows what they're discussing on the net. You
actually realize how to bring a problem to light and make it important.

More people ought to check this out and understand this side of the story.
I can't believe you are not more popular given that you certainly possess
the gift.

タイトル
名前
URL
コメント