Advertisement
microwerx

CS202 Thread and Space Ship Operator Example

Apr 23rd, 2020
724
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.21 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <compare>
  4. #include <thread>
  5. #include <mutex>
  6. #include <vector>
  7. #include <chrono>
  8. #include <string>
  9.  
  10. struct SpaceShip {
  11.     int i;
  12.  
  13.     std::strong_ordering operator<=>(SpaceShip const& rhs) {
  14.         return i <=> rhs.i;
  15.     }
  16. };
  17.  
  18. void spaceship(int a, int b) {
  19.     std::cout << "a: " << a << "\n";
  20.     std::cout << "b: " << b << "\n";
  21.     auto result = (a <=> b);
  22.     if (result < 0) std::cout << "a <=> b is negative\n";
  23.     if (result > 0) std::cout << "a <=> b is positive\n";
  24.     if (result == 0) std::cout << "a <=> b is equivalent\n";
  25. }
  26.  
  27. std::mutex cout_mutex;
  28. void print(int taskID, int i, int j) {
  29.     const std::lock_guard<std::mutex> lock(cout_mutex);
  30.     std::cout << "Task i " << taskID << " i: " << i << " " << "j: " << j << "\n";
  31. }
  32.  
  33. void hardTask(int taskID) {
  34.     constexpr int items = 100;
  35.     for (int i = 0; i < items; i++) {
  36.         for (int j = 0; j < items; j++) {
  37.             if (rand() % 1000 < 2)
  38.                 print(taskID, i, j);
  39.             std::sin(i * j);
  40.         }
  41.     }
  42.     const std::lock_guard<std::mutex> lock(cout_mutex);
  43.     std::cout << "Task " << taskID << " done\n";
  44. }
  45.  
  46. void supercomputer() {
  47.     std::vector<std::thread> threadPool;
  48.     for (int i = 0; i < 32; i++)
  49.         threadPool.push_back(std::thread(hardTask, i));
  50.  
  51.     for (auto& t : threadPool) {
  52.         t.join();
  53.     }
  54. }
  55.  
  56. int game_done = 0;
  57. std::string message;
  58. std::mutex game_mutex;
  59.  
  60. void display_thread() {
  61.     while (!game_done) {
  62.         if (!message.empty()) {
  63.             std::cout << message << "\n";
  64.             const std::lock_guard<std::mutex> lock(game_mutex);
  65.             message.clear();
  66.         }
  67.         using namespace std::chrono_literals;
  68.         std::this_thread::sleep_for(1s);
  69.     }
  70. }
  71.  
  72. void input_thread() {
  73.     while (!game_done) {
  74.         std::string input;
  75.         std::getline(std::cin, input);
  76.         if (input == "quit") {
  77.             const std::lock_guard<std::mutex> lock(game_mutex);
  78.             game_done = 1;
  79.         }
  80.         else if (!input.empty()) {
  81.             const std::lock_guard<std::mutex> lock(game_mutex);
  82.             message = input;
  83.         }
  84.     }
  85. }
  86.  
  87. void game() {
  88.     std::thread display(display_thread);
  89.     std::thread input(input_thread);
  90.     input.join();
  91.     display.join();
  92. }
  93.  
  94. int main() {
  95.     //spaceship(1, 3);
  96.     //spaceship(3, 1);
  97.     //spaceship(1, 1);
  98.     //spaceship(0, 0);
  99.  
  100.     //supercomputer();
  101.  
  102.     game();
  103.  
  104.     return 0;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement