int main (int argc, char**argv) { std::cout << "\nwithout lock:\n"; std::thread th1 (print_block,50,'*'); std::thread th2 (print_block,50,'$'); th1.join(); th2.join(); std::cout << "\nwith unique_lock:\n"; std::thread th3 (print_block_lock_guard,50,'*'); std::thread th4 (print_block_lock_guard,50,'$'); th3.join(); th4.join(); std::cout << "\nwith lock_guard:\n"; std::thread th5 (print_block_unique_lock,50,'*'); std::thread th6 (print_block_unique_lock,50,'$'); th5.join(); th6.join(); return 0; }
int main(int argc, char** argv) { std::thread th1(print_this); std::thread th2(print_this); std::thread th3(print_this); std::thread th4(print_this); th1.join(); th2.join(); th3.join(); th4.join(); return 0; }
int main() { std::thread th1(print_block, 20, '*'); std::thread th2(print_block, 20, '#'); std::thread th3(print_block, 20, 'a'); std::thread th4(print_block, 20, '&'); th1.join(); th2.join(); th3.join(); th4.join(); return 0; }
main() { float a,b,c,d,e,f; printf("\nNhap vao a,b,c,d,e,f : "); scanf("%f %f %f %f %f %f",&a,&b,&c,&d,&e,&f); dd = a*e - d*b; dx = b*f - e*c; dy = a*f - d*c; if (dd != 0) th1(); else if ((dx == dy)&&(dy == 0)) th2(); else th3(); getch(); }
int main() { std::thread th1(do_work); std::thread th2(do_work); std::thread th3(do_work); std::thread th4(do_work); std::thread th5(do_work); th1.join(); th2.join(); th3.join(); th4.join(); th5.join(); std::cout << "Result:" << data << '\n'; }
int main(void) { //exemodel::mask_signal(SIGUSR1); /*start fusion splice service*/ std::unique_ptr<thread_test> fs_svr(new thread_test()); std::thread th1([&fs_svr](void){ exemodel::mask_signal(SIGUSR1); fs_svr->run(); }); /*start heat service*/ std::unique_ptr<thread_test> heat_svr(new thread_test()); std::thread th2([&heat_svr](void){ exemodel::mask_signal(SIGUSR1); heat_svr->run(); }); /*start device manage service*/ std::unique_ptr<thread_test> devmng_svr(new thread_test()); std::thread th3([&devmng_svr](void){ exemodel::mask_signal(SIGUSR1); devmng_svr->run(); }); s_sigterm_hdl = [&th1, &th2, &th3](void) { std::cout << "start send sigusr1" << std::endl; ::pthread_kill(th1.native_handle(), SIGUSR1); ::pthread_kill(th2.native_handle(), SIGUSR1); ::pthread_kill(th3.native_handle(), SIGUSR1); std::cout << "stop send sigusr1" << std::endl; }; //exemodel::mask_signal(SIGTERM); std::signal(SIGINT, signal_handler); std::cout << "start wait" << std::endl; th1.join(); std::cout << "start wait1" << std::endl; th2.join(); std::cout << "start wait2" << std::endl; th3.join(); std::cout << "start wait3" << std::endl; return 0; }
Membership::Membership(bool introducer, int port) { logFile.open("logMembership.log"); isIntroducer = introducer; this->port = port; leader.active = false; sockfd = bindSocket(port); if(isIntroducer){ logFile << "I am the introducer! " << std::endl; getAdress("Address.add"); } else{ getAdress("AddrIntro.add"); } roundLock.lock(); roundId = 0; roundLock.unlock(); join(); std::thread th3(&Membership::forJoinThread, this); std::thread th1(&Membership::listeningThread, this); std::thread th2(&Membership::detectThread, this); listening.swap(th1); detecting.swap(th2); joinThread.swap(th3); killListeningThread = false; killDetectingThread = false; //runLeaderElection(); }
void benchmark_suite( int mode, const std::string &name, const TEST &container ) { auto single = [&]() { for (int j=0; j<30000; ++j) { TEST s; for (int i=0; i<100; ++i) s.push_back(i); TEST s2 = s; } }; auto multi = [&]() { std::thread th1( [=] { single(); } ); std::thread th2( [=] { single(); } ); std::thread th3( [=] { single(); } ); std::thread th4( [=] { single(); } ); th1.join(); th2.join(); th3.join(); th4.join(); }; auto creator = []( TEST &s ) { s = TEST(); for (int j=0; j<30000; ++j) for (int i=0; i<100; ++i) s.push_back(i); }; auto deleter = []( TEST &s ) { s = TEST(); }; auto deleter2 = []( TEST::value_type *&s ) { TEST::allocator_type alloc; alloc.deallocate( s, 1 ); }; auto creator2 = []( TEST::value_type *&s ) { TEST::allocator_type alloc; s = alloc.allocate( 1 ); }; std::get<THREAD_SAFE>(feat[ name ]) = false; std::get<SAFE_DELETE>(feat[ name ]) = false; std::get<RESET_MEMORY>( feat[ name ] ) = false; std::get<AVG_SPEED>(feat[name]) = 0; if( mode == 0 ) return; double took = 0; if( mode & 1 ) { std::string id = std::string() + "single: " + name; std::cout << id; double span = timing::now(); single(); took += (timing::now() - span) * 1000000; std::cout << " " << int(took) << "us" << std::endl; #if 1 int *x = 0; creator2(x); if( x == 0 ) throw std::exception("bad test"); if( *x == 0 ) std::get<RESET_MEMORY>( feat[ name ] ) = true; deleter2(x); if (x == 0) std::get<SAFE_DELETE>(feat[name]) = true; #endif } if( mode & 2 ) { { std::string id = std::string() + "multi: " + name; std::cout << id; double span = timing::now(); multi(); took += (timing::now() - span) * 1000000; std::cout << " " << int(took) << "us" << std::endl; } if (1) { std::string id = std::string() + "owner: " + name; std::cout << id; double span = timing::now(); TEST s; std::thread th1( [&](){ creator(s); } ); th1.join(); std::thread th2( [&](){ deleter(s); } ); th2.join(); #if 1 int *x = 0; std::thread th3( [&](){ creator2(x); } ); th3.join(); if( x == 0 ) throw std::exception("bad test"); if( *x == 0 ) std::get<RESET_MEMORY>( feat[ name ] ) = true; std::thread th4( [&](){ deleter2(x); } ); th4.join(); if( x == 0 ) std::get<SAFE_DELETE>( feat[ name ] ) = true; #endif took += (timing::now() - span) * 1000000; std::cout << " " << int(took) << "us" << std::endl; } std::get<THREAD_SAFE>( feat[ name ] ) = true; } /**/ if (mode & 3) took /= 3; else if (mode & 2) took /= 2; else took /= 1; std::get<AVG_SPEED>(feat[name]) = took; ranking[ took ].insert( name ); if( name == "std::allocator" ) default_allocator_time = took; }
int main() { HDC1000 hdc1000_1; MPL115A2 mpl115a2_1; MS5607 ms5607_1; DISPLAY d; LOG l; DB db; hdc1000_1.Init(); mpl115a2_1.Init(); ms5607_1.Init(); std::cout << "Hello\nWorld!\n"; db.Connect(); if( signal(SIGINT, sig_handler) == SIG_ERR) { std::cerr << "sig_handler fail."<<std::endl; } while(1) { std::thread th0(governor); // std::thread th1([&]{hdc1000_1.Get();}); std::thread th2([&]{mpl115a2_1.Get();}); std::thread th3([&]{ms5607_1.Get();}); // l.GetTime();//現在時刻の取得 // th1.join(); th2.join(); th3.join(); // l.Write( hdc1000_1.Read(0),//Temp hdc1000_1.Read(1),//Humi mpl115a2_1.Read(1), mpl115a2_1.Read(0), ms5607_1.Read(1),// ms5607_1.Read(0) //Temp ); // 表示 d.Disp(l); // DBインサート db.Insert(l); // Wait th0.join(); // Terminate if(sig) { std::cout <<"Recieve SIGINT\n"; db.Disconnect(); std::cout <<std::flush; break; } } }