#include #include #include #include #include #include #include using namespace std; const int COUNT_LIMIT{10'000}; int Global_Count; unsigned int Global_Seeds[2]; static void * ThreadFunction (void * arg) { unsigned int id = reinterpret_cast(arg); timespec req; req.tv_sec = 0; req.tv_nsec = rand_r(&(Global_Seeds[id]))% 5 ; nanosleep(&req, nullptr); int i = Global_Count; if (id == 0) { ++i; } else { --i; } Global_Count = i; return nullptr; } int main() { pthread_t t0, t1; int info; int counts[]{0,0,0}; void * res; srand(time(nullptr)); Global_Seeds[0] = rand(); Global_Seeds[1] = rand(); for(int i = 0; i < COUNT_LIMIT; ++i) { Global_Count = 1; info = pthread_create(&t0 , nullptr, ThreadFunction, reinterpret_cast(0)); info = pthread_create(&t1 , nullptr, ThreadFunction, reinterpret_cast(1)); pthread_join(t0, &res); pthread_join(t1, &res); counts[Global_Count] += 1; } cout << "Final Results" << endl; for( int i =0; i < 3; ++i) { cout << i << " " << counts[i] << endl; } return 0; }