セマフォ
Boost.Threadにセマフォが無いのでどうしようかと思いましたが,Boost.Interprocessにあるんですね.
単純にそれを使ってみました.
超シンプルです.
#include <cstddef> #include <iostream> #include <boost/thread/thread.hpp> #include <boost/interprocess/sync/named_semaphore.hpp> const std::size_t create_threads_number = 5; const std::size_t max_running_threads_number = 3; const char* const semaphore_name = "my_semaphore"; void thread_func(){ std::cout << "Created thread: " << boost::this_thread::get_id() << std::endl; boost::interprocess::named_semaphore semaphore(boost::interprocess::open_only_t(), semaphore_name); // ここからクリティカルセクション(3スレッドまで. semaphore.wait(); std::cout << " Processing: " << boost::this_thread::get_id() << std::endl; sleep(1); semaphore.post(); // ここまで. std::cout << "Finished thread: " << boost::this_thread::get_id() << std::endl; } int main(){ // 既にその名前のセマフォが作成されていたら困るので削除する(*1. boost::interprocess::named_semaphore::remove(semaphore_name); // 3までのセマフォを作成. boost::interprocess::named_semaphore(boost::interprocess::create_only_t(), semaphore_name, max_running_threads_number); boost::thread_group threads; for(std::size_t i=0; i<create_threads_number; ++i) threads.create_thread(thread_func); threads.join_all(); boost::interprocess::named_semaphore::remove(semaphore_name); return 0; } // g++ this.cpp -lrt -lboost_thread
実行結果.
見にくい・・・.
Created thread: 0x8b53068
Processing: 0x8b53068
Created thread: 0x8b53200
Processing: 0x8b53200
Created thread: 0x8b533b0
Processing: 0x8b533b0
Created thread: 0x8b53560
Created thread: 0x8b53710
Finished thread: 0x8b53068
Finished thread: 0x8b533b0
Finished thread: 0x8b53200
Processing: 0x8b53560
Processing: 0x8b53710
Finished thread: 0x8b53560
Finished thread: 0x8b53710
同時に3つまでしかprocessingできてないです・・・よね.
googleなどで検索した時には,condition variableとmutexで自作するとかあったので・・・.
でもそっちの方が良いのかなぁと思ってみたりもします.
はたしてこれは大丈夫なのか・・・.
*1:少し乱暴ですがw