/*#include "FXTrade.h" #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using namespace boost; namespace write_namespace { mutex file_mutex; ofstream out; const size_t dimension(10000); const size_t size(10); const unsigned int number_of_threads(10); } queue TradeVector; namespace read_namespace { mutex in_file_mutex; ifstream in; } void write_to_disk(const vector& nVector) { using namespace write_namespace; mutex::scoped_lock lock(file_mutex); for(vector::const_iterator i_ptr=nVector.begin(); i_ptr!=nVector.end(); ++i_ptr) { out << i_ptr->m_TradeID << ";" << i_ptr->m_Name << ";" << i_ptr->m_Amount << endl; } } auto_ptr getTradeFromString(string& c_tmp) { int i_TradeID; string c_Name, c_sub; string::size_type pos; double d_Amount(0.0); pos = c_tmp.find(";"); c_sub = c_tmp.substr(0, pos); i_TradeID = atoi(c_sub.c_str()); c_sub = c_tmp.substr(++pos, 3); c_Name = c_sub; c_sub = c_tmp.substr(pos+4, 3); d_Amount = atof(c_sub.c_str()); d_Amount = 100.; return auto_ptr(new CFXTrade(i_TradeID, c_Name, d_Amount)); } void read_from_disk(queue& nTradeVector) { using namespace write_namespace; using namespace read_namespace; string c_tmp; try { mutex::scoped_lock lock(in_file_mutex); for(size_t i=0; i ThreadVector(number_of_threads); mt19937 mt2; random_number_generator std_rng(mt2); int TradeIDVector[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; char* NameVector[] = {"ABC", "BCD", "CDE", "DEF", "EFG", "FGH", "GHI", "HIJ", "IJK", "JKL"}; double AmountVector[] = {10., 20., 30., 40., 50., 60., 70., 80., 90., 11.}; vector< vector > TradeVector(number_of_threads); for(int i=0; ijoin(); } for(int i=0; i ReadThreadVector(number_of_threads); in.open("file.txt"); try { for(int i=0; ijoin(); } for(int i=0; i #include #include #include #include using namespace boost; mutex io_mutex; class bounded_buffer : private boost::noncopyable { public: typedef boost::mutex::scoped_lock lock; bounded_buffer(int n) : begin(0), end(0), buffered(0), circular_buf(n) { } void send (int m) { lock lk(monitor); while (buffered == circular_buf.size()) buffer_not_full.wait(lk); circular_buf[end] = m; end = (end+1) % circular_buf.size(); ++buffered; buffer_not_empty.notify_one(); } int receive() { lock lk(monitor); while (buffered == 0) buffer_not_empty.wait(lk); int i = circular_buf[begin]; begin = (begin+1) % circular_buf.size(); --buffered; buffer_not_full.notify_one(); return i; } private: int begin, end, buffered; std::vector circular_buf; boost::condition buffer_not_full, buffer_not_empty; boost::mutex monitor; }; bounded_buffer buf(2); void sender() { int n = 0; while (n < 100) { buf.send(n); mutex::scoped_lock l(io_mutex); std::cout << "sent: " << n << std::endl; ++n; } buf.send(-1); } void receiver() { int n; do { n = buf.receive(); mutex::scoped_lock l(io_mutex); std::cout << "received: " << n << std::endl; } while (n != -1); // -1 indicates end of buffer } int main() { boost::thread thrd1(&sender); boost::thread thrd2(&receiver); thrd1.join(); thrd2.join(); return 0; }