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; }
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'; }
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; }