Balking
é©åãªç¶æ
ã«ãªã£ã¦ããªããªãå¦çãä¸æãããã¨ãããã¿ã¼ã³ã
以ä¸ã¯å¢è£æ¹è¨ç Javaè¨èªã§å¦ã¶ãã¶ã¤ã³ãã¿ã¼ã³å ¥é ãã«ãã¹ã¬ããç·¨ã®ãµã³ãã«ã Boost.Thread ã使ã£ã¦æ¸ããã³ã¼ãã
save, doSave ã¡ã½ãã㯠Thread#currentThread ãå¿
è¦ã¨ãã¦ãããã ãã©ãboost::thread ã«ã¯ãããªã®ã¯ç¡ãã®ã§ this ã渡ãã¦ç¡çç¢çååãåå¾ãã¦ãã¾ãã
æå㯠TSS ã使ã£ã¦ã«ã¬ã³ãã¹ã¬ãããåå¾ãã¦ãããã ãã©ãä½ãåé·ã«ãªã£ã¦ããã®ã§ä¸è¨ã®æ¹æ³ã«å¤æ´ãã¦ã¿ã¾ããã
data.h
#ifndef MTDP_BALKING_DATA_H_INCLUDED #define MTDP_BALKING_DATA_H_INCLUDED #include <boost/thread.hpp> #include <string> #include <iostream> #include <fstream> #include "../thread_helper.h" namespace mtdp{ namespace balking { class data { private: const std::string filename_; // ä¿åãããã¡ã¤ã«ã®åå std::string content_; // ãã¼ã¿ã®å 容 bool changed_; // å¤æ´ããå 容ãä¿åããã¦ããªããªã true boost::mutex mutex_; public: data(std::string filename, std::string content) : filename_(filename), content_(content), changed_(true) { } // ãã¼ã¿ã®å 容ãæ¸ãæãã void change(std::string newContent) { boost::mutex::scoped_lock lock(mutex_); content_ = newContent; changed_ = true; } // ãã¼ã¿ã®å 容ãå¤æ´ããã¦ããããã¡ã¤ã«ã«ä¿åãã template<class TThread> void save(TThread* p) { boost::mutex::scoped_lock lock(mutex_); if (!changed_) { return; } doSave(p); changed_ = false; } private: // ãã¼ã¿ã®å 容ãå®éã«ãã¡ã¤ã«ã«ä¿åãã template<class TThread> void doSave(TThread* p) { thread_helper::shared_cout(p->name() + " calls doSave, content = " + content_ + "\n"); std::fstream fs(filename_.c_str(), std::ios_base::out); fs << (content_ + "\n"); } }; }} #endif // MTDP_BALKING_DATA_H_INCLUDED
saver_thread.h
#ifndef MTDP_BALKING_SAVER_THREAD_H_INCLUDED #define MTDP_BALKING_SAVER_THREAD_H_INCLUDED #include <boost/thread.hpp> #include <boost/shared_ptr.hpp> #include <string.h> #include "data.h" #include "../thread_helper.h" namespace mtdp{ namespace balking { class saver_thread { private: const std::string name_; const boost::shared_ptr<data> data_; public: saver_thread(const std::string& name, boost::shared_ptr<data> d) : name_(name), data_(d) { } void run() { while (true) { data_->save(this); thread_helper::sleep(1000); } } std::string name() const { return name_; } }; }} #endif // MTDP_BALKING_SAVER_THREAD_H_INCLUDED
changer_thread.h
#ifndef MTDP_BALKING_CHANGER_THREAD_H_INCLUDED #define MTDP_BALKING_CHANGER_THREAD_H_INCLUDED #include <boost/shared_ptr.hpp> #include <boost/random.hpp> #include <string> #include "data.h" #include "../thread_helper.h" namespace mtdp{ namespace balking { class changer_thread { private: const std::string name_; const boost::shared_ptr<data> data_; const boost::mt19937 mt_; public: changer_thread(std::string name, boost::shared_ptr<data> d) : name_(name), data_(d) { } void run() { boost::variate_generator<boost::mt19937, boost::uniform_int<> > random(mt_, boost::uniform_int<>(0, 1000)); for (int i = 0; true; i++) { data_->change("No." + to_string(i)); // ãã¼ã¿ãå¤æ´ãã thread_helper::sleep(random()); // ä»äºã®ã¤ãã data_->save(this); // æ示çã«ä¿åãã } } std::string name() const { return name_; } }; }} #endif // MTDP_BALKING_CHANGER_THREAD_H_INCLUDED
main.cpp
#include <boost/thread.hpp> #include <boost/shared_ptr.hpp> #include <boost/bind.hpp> #include "data.h" #include "saver_thread.h" #include "changer_thread.h" namespace mtdp{ namespace balking { void main() { boost::shared_ptr<data> d(new data("data.txt", "(empty)")); boost::thread_group group; group.create_thread(boost::bind(&changer_thread::run, boost::shared_ptr<changer_thread>(new changer_thread("ChangerThread", d)))); group.create_thread(boost::bind(&saver_thread::run, boost::shared_ptr<saver_thread>(new saver_thread("SaverThread", d)))); group.join_all(); } }}