|
| 1 | +#include <iostream> |
| 2 | +#include <thread> |
| 3 | +#include <barrier> |
| 4 | +#include <array> |
| 5 | +#include <random> |
| 6 | +#include <string> |
| 7 | + |
| 8 | +// Random number generator setup |
| 9 | +std::random_device rd; |
| 10 | +std::mt19937 gen(rd()); |
| 11 | +std::uniform_int_distribution<> shortTime(1, 7); |
| 12 | +std::uniform_int_distribution<> longTime(5, 14); |
| 13 | + |
| 14 | +// Fixed-size array for random time distributions |
| 15 | +std::array<std::uniform_int_distribution<>, 2> dists{shortTime, longTime}; |
| 16 | + |
| 17 | +// People in each crew |
| 18 | +const int crewSize = 2; |
| 19 | + |
| 20 | +// Function to map crewId to a name for output clarity |
| 21 | +std::string getCrewName(int crewId) { |
| 22 | + return crewId == 0 ? "Crew A" : "Crew B"; |
| 23 | +} |
| 24 | + |
| 25 | +void bakingTask(int crewId, int id, std::barrier<>& barrier) { |
| 26 | + std::string crewName = getCrewName(crewId); |
| 27 | + |
| 28 | + // Phase 1: Prepare Ingredients |
| 29 | + auto seconds = dists[crewId](gen); |
| 30 | + std::cout << crewName << ", Baker " << id << " is preparing ingredients for " << seconds << " seconds...\n"; |
| 31 | + std::this_thread::sleep_for(std::chrono::seconds(seconds)); // Simulate task time |
| 32 | + |
| 33 | + std::cout << crewName << ", Baker " << id << " is done with the ingredients.\n"; |
| 34 | + |
| 35 | + // Wait for crew members to finish preparing ingredients |
| 36 | + barrier.arrive_and_wait(); |
| 37 | + |
| 38 | + // Phase 2: Make Cake |
| 39 | + seconds = dists[crewId](gen); |
| 40 | + std::cout << crewName << ", Baker " << id << " is making the cake for " << seconds << " seconds...\n"; |
| 41 | + std::this_thread::sleep_for(std::chrono::seconds(seconds)); // Simulate task time |
| 42 | + |
| 43 | + std::cout << crewName << ", Baker " << id << " is done making the cake.\n"; |
| 44 | + |
| 45 | + // Wait for crew members to finish making the cake |
| 46 | + barrier.arrive_and_wait(); |
| 47 | +} |
| 48 | + |
| 49 | +int main() { |
| 50 | + // Create a barrier for each crew, since each crew can work independently on their cake |
| 51 | + std::barrier barrierCrewA(crewSize); |
| 52 | + std::barrier barrierCrewB(crewSize); |
| 53 | + |
| 54 | + // Fixed-size arrays for threads |
| 55 | + std::array<std::thread, crewSize> crewAThreads; |
| 56 | + std::array<std::thread, crewSize> crewBThreads; |
| 57 | + |
| 58 | + // Start threads for Crew A |
| 59 | + for (int i = 0; i < crewSize; ++i) { |
| 60 | + crewAThreads[i] = std::thread(bakingTask, 0, i + 1, std::ref(barrierCrewA)); |
| 61 | + } |
| 62 | + |
| 63 | + // Start threads for Crew B |
| 64 | + for (int i = 0; i < crewSize; ++i) { |
| 65 | + crewBThreads[i] = std::thread(bakingTask, 1, i + 1, std::ref(barrierCrewB)); |
| 66 | + } |
| 67 | + |
| 68 | + // Join threads for Crew A |
| 69 | + for (auto& thread : crewAThreads) { |
| 70 | + thread.join(); |
| 71 | + } |
| 72 | + |
| 73 | + // Join threads for Crew B |
| 74 | + for (auto& thread : crewBThreads) { |
| 75 | + thread.join(); |
| 76 | + } |
| 77 | + |
| 78 | + std::cout << "All cakes are done." << std::endl; |
| 79 | + |
| 80 | + return 0; |
| 81 | +} |
0 commit comments