void my_search::process(int proc_num){ vector<int> cur_ch; vector<int> words; int i=0,word_size; while(argv[proc_num+1][i]!=0)i++; word_size=i; words.resize(argc-4); cur_ch.resize(argc-4); while(counter_file<max_file){ boost::lock_guard<boost::mutex> lock(m); while((counter==0)||(thr[proc_num]==false)){ if(counter_file>=max_file)return; cv.wait(m); } pantheios::log_NOTICE(PSTR("Reading thread id: ["), pantheios::threadId, PSTR("]")); for(unsigned int c=0;c<memblock.size();c++){ char i=memblock[c]; if(argv[proc_num+1][cur_ch[file_num]]==i) cur_ch[file_num]++; else cur_ch[file_num]=0; if(cur_ch[file_num]==word_size){ words[file_num]++;sum++;cur_ch[file_num]=0; } } counter--; thr[proc_num]=false; if(comp_size[file_num]==0){ cout<<argv[file_num+4]<<" "<<argv[proc_num+1]<< " "<<words[file_num]<<endl; counter_file++; } cv.notify_all(); } return; }
int main() { { L1 lk(m0); boost::thread t(f); BOOST_TEST(test1 == 0); while (test1 == 0) cv.wait(lk); BOOST_TEST(test1 != 0); test2 = 1; lk.unlock(); cv.notify_one(); t.join(); } test1 = 0; test2 = 0; { L1 lk(m0); boost::thread t(f); BOOST_TEST(test1 == 0); while (test1 == 0) cv.wait(lk); BOOST_TEST(test1 != 0); lk.unlock(); t.join(); } return boost::report_errors(); }
void myUserHandler15( const CanMsgStruct/*&*/ message) { _canmodule_slowDown( 15 ); boost::unique_lock<boost::mutex> lock{reception_mtx15}; _connection_v[ 15 ].reception.newArrived = true; _connection_v[ 15 ].reception.receivedMessageBuffer = message; reception_cond15.notify_all(); }
// イベントが来るまで待つ。(最大1秒) bool wait_for_event() { scoped_lock lk(guard_); // 現在の1秒後の時間を計算 boost::xtime xt; boost::xtime_get(&xt, boost::TIME_UTC); xt.sec += 1; return wakeup_.timed_wait(lk, xt); }
void put(int x) { { boost::mutex::scoped_lock lock(mu); while (is_full()) { { boost::mutex::scoped_lock lock(io_mu); cout << "full waiting..." << endl; } cond_put.wait(mu); } stk.push(x); ++un_read; } cond_get.notify_one(); }
void get(int *x) { { boost::mutex::scoped_lock lock(mu); while (is_empty()) { { boost::mutex::scoped_lock lock(io_mu); cout << "empty waiting..." << endl; } cond_get.wait(mu); } --un_read; *x = stk.top(); stk.pop(); } cond_put.notify_one(); }
/** * The user-handlerX gets invoked each time there is a new can message on connectionX, according to HW sync. * We put this message into a buffer, protect it from the wait, and notify the wait that * the new msg can be picked up. Using boost for sync. * * this handler must be connected to the CanModule access point by this wrapper in the canmodule_init call, * and this is hardcoded. We can have only static mutexes and cond_vars, therefore we have to decide BEFORE * compile-time how many reception threads we want. While in principle we can have a big number easily, let's * limit the number of connections ( PC port or ip-number && CAN port ) we can have PER TASK to 16. * This corresponds to 16 CAN buses, using i.e. one systec16 module or 8 anagate-duos. * * The user can conveniently pick up the new message from the waitForNewMessage call which * blocks until new reception. Just a separate thread or whatever sequence is needed for the * blocking reception call, the user can implement whatever scheme she likes. */ void myUserHandler0( const CanMsgStruct/*&*/ message) { int connectionIndex = 0; LOG(Log::TRC) << __FUNCTION__ << " received a message [id= " << message.c_id << " data0= " << (int) message.c_data[ 0 ] << "] connectionIndex= " << connectionIndex; _canmodule_slowDown( connectionIndex ); { boost::unique_lock<boost::mutex> lock{reception_mtx0}; _connection_v[ connectionIndex ].reception.newArrived = true; _connection_v[ connectionIndex ].reception.receivedMessageBuffer = message; } LOG(Log::TRC) << __FUNCTION__ << " new message copied, notify_all connectionIndex= " << connectionIndex; reception_cond0.notify_all(); }
void f() { typedef boost::chrono::system_clock Clock; typedef boost::chrono::milliseconds milliseconds; L1 lk(m0); BOOST_TEST(test2 == 0); test1 = 1; cv.notify_one(); Clock::time_point t0 = Clock::now(); bool r = cv.wait_for(lk, milliseconds(250), Pred(test2)); Clock::time_point t1 = Clock::now(); if (runs == 0) { BOOST_TEST(t1 - t0 < milliseconds(250)); BOOST_TEST(test2 != 0); } else { BOOST_TEST(t1 - t0 - milliseconds(250) < milliseconds(250+5)); BOOST_TEST(test2 == 0); } ++runs; }
void my_search:: loadfile(int fl_num){ file[fl_num].open(argv[fl_num+4], ios::in|ios::binary|ios::ate); if (file[fl_num].is_open()) { comp_size[fl_num] = file[fl_num].tellg(); file[fl_num].seekg (0, ios::beg); while(comp_size[fl_num]>0){ boost::lock_guard<boost::mutex> lock(m); while(counter!=0){ cv.wait(m); } pantheios::log_NOTICE(PSTR("Writing thread id: ["), pantheios::threadId, PSTR("]")); if(comp_size[fl_num]<1024){ file[fl_num].read (mem, comp_size[fl_num]); memblock=mem; memblock.resize(comp_size[fl_num]); comp_size[fl_num]=0; }else { file[fl_num].read (mem, 1024); memblock=mem; memblock.resize(1024); comp_size[fl_num]-=1024; } counter=3; file_num=fl_num; for (int cc=0;cc<3;cc++)thr[cc]=true; cv.notify_all(); } file[fl_num].close(); }else { boost::lock_guard<boost::mutex> lock(m); cout << "Unable to open file "<<argv[fl_num+4]<<endl; counter_file+=3; cv.notify_all(); } return; }
count_type wait() { all_futures_lock lk(waiters_); for (;;) { for (count_type i = 0; i < waiters_.size(); ++i) { if (waiters_[i].future_->is_ready(lk.locks[i])) { return waiters_[i].index; } } cv.wait(lk); } }
void f() { L1 lk(m0); BOOST_TEST(test2 == 0); test1 = 1; cv.notify_one(); Clock::time_point t0 = Clock::now(); Clock::time_point t = t0 + Clock::duration(250); bool r = cv.wait_until(lk, t, Pred(test2)); Clock::time_point t1 = Clock::now(); if (runs == 0) { BOOST_TEST(t1 - t0 < Clock::duration(250)); BOOST_TEST(test2 != 0); BOOST_TEST(r); } else { BOOST_TEST(t1 - t0 - Clock::duration(250) < Clock::duration(250+2)); BOOST_TEST(test2 == 0); BOOST_TEST(!r); } ++runs; }
void f() { L1 lk(m0); BOOST_TEST(test2 == 0); test1 = 1; cv.notify_one(); Clock::time_point t0 = Clock::now(); Clock::time_point t = t0 + milliseconds(250); while (test2 == 0 && cv.wait_for(lk, t - Clock::now()) == boost::cv_status::no_timeout) {} Clock::time_point t1 = Clock::now(); if (runs == 0) { ns d = t1 - t0; BOOST_THREAD_TEST_IT(d, ns(max_diff)); BOOST_TEST(test2 != 0); } else { ns d = t1 - t0 - ms(250); BOOST_THREAD_TEST_IT(d, ns(max_diff)); BOOST_TEST(test2 == 0); } ++runs; }
void notify_event(const Ctrl& ctrl, uint32_t devnum=0) { scoped_lock lock(guard_); shared_ptr<event_ctrl<Ctrl> > p(new event_ctrl<Ctrl>(ctrl, devnum)); eventQueue_.push(p); wakeup_.notify_one(); }
// listenerを中断する void stop_listener() { quit_ = true; wakeup_.notify_one(); }