#include #include #include #include #include //#define RWLOCK int const n = 1000000; int const n_readers = 16; int const n_writers = 1; std::vector v; #ifdef RWLOCK ACE_SYNCH_RW_MUTEX mtx; #else ACE_SYNCH_MUTEX mtx; #endif void* reader_thread (void*) { int m = 0; for( int i = 0; i < n; ++i ) { #ifdef RWLOCK ACE_Read_Guard guard (mtx); #else ACE_Guard guard (mtx); #endif m += v[ i ]; } std::cout << m << std::endl; return 0; } void* writer_thread (void*) { for( int i = 0; i < n; ++i ) { #ifdef RWLOCK ACE_Write_Guard guard (mtx); #else ACE_Guard guard (mtx); #endif v[ i ] += i; } return 0; } int main() { v.resize( n ); clock_t t = clock(); int writers = ACE_Thread_Manager::instance()->spawn_n (n_writers, writer_thread); if (writers == -1) { cerr << "Unable to spawn writer threads\n"; exit (EXIT_FAILURE); } int readers = ACE_Thread_Manager::instance()->spawn_n (n_readers, reader_thread); if (readers == -1) { cerr << "Unable to spawn reader threads\n"; exit (EXIT_FAILURE); } ACE_Thread_Manager::instance()->wait_grp (writers); ACE_Thread_Manager::instance()->wait_grp (readers); t = clock() - t; std::cout << static_cast( t ) / CLOCKS_PER_SEC << '\n'; }