Skip to content

Commit 5f83501

Browse files
committed
Add cake_baking_using_barriers
1 parent 24d09bd commit 5f83501

File tree

3 files changed

+86
-1
lines changed

3 files changed

+86
-1
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
assemble_teams_using_latch
22
bank_account_using_mutex_locks
3+
cake_baking_using_barriers

Makefile

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@ SRC = $(wildcard *.cc)
55
BIN = $(SRC:.cc=)
66

77
# Rules
8-
default: assemble_teams_using_latch bank_account_using_mutex_locks
8+
default: assemble_teams_using_latch bank_account_using_mutex_locks cake_baking_using_barriers
99

1010
assemble_teams_using_latch: assemble_teams_using_latch.cc
1111
$(CXX) $(CXXFLAGS) -o assemble_teams_using_latch assemble_teams_using_latch.cc
1212

1313
bank_account_using_mutex_locks: bank_account_using_mutex_locks.cc
1414
$(CXX) $(CXXFLAGS) -o bank_account_using_mutex_locks bank_account_using_mutex_locks.cc
1515

16+
cake_baking_using_barriers: cake_baking_using_barriers.cc
17+
$(CXX) $(CXXFLAGS) -o cake_baking_using_barriers cake_baking_using_barriers.cc
18+
1619
clean:
1720
@$(RM) $(BIN)
1821

cake_baking_using_barriers.cc

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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

Comments
 (0)