Advertisement
olivercromwell

The Honey Problem

Jan 17th, 2025 (edited)
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.12 KB | Jokes | 0 0
  1. #include <iostream>
  2. #include <thread>
  3. #include <chrono>
  4. #define HIS  0
  5. #define HERS  1
  6. uint8_t resolved_disputes = 0;  
  7. uint8_t turn = 0;
  8. void husband() {
  9.     uint8_t disputes;
  10.     for (int i = 0; i < 30;) {  
  11.         if (turn == HIS) {
  12.             disputes = resolved_disputes;
  13.             disputes = disputes + 1;
  14.             std::this_thread::sleep_for(std::chrono::nanoseconds(1));
  15.             resolved_disputes = disputes;
  16.             i++;
  17.             turn = HERS;
  18.  
  19.         }
  20.     }
  21.     turn = HERS;
  22. }
  23.  
  24. void wife() {
  25.     uint8_t disputes;
  26.     for (int i = 0; i < 30;) {  
  27.         if (turn == HERS) {
  28.             disputes = resolved_disputes;
  29.             disputes = disputes + 1;
  30.             std::this_thread::sleep_for(std::chrono::nanoseconds(1));
  31.             resolved_disputes = disputes;
  32.             i++;
  33.             turn = HIS;
  34.         }
  35.     }
  36.     turn = HIS;
  37. }
  38.  
  39. int main() {
  40.     resolved_disputes = 0;
  41.  
  42.     std::thread t1(husband);
  43.     std::thread t2(wife);
  44.  
  45.     t1.join();
  46.     t2.join();
  47.  
  48.     std::cout << "resolved disputes: " << (int)resolved_disputes << std::endl;
  49.  
  50.     return 0;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement