Skip to content

Commit 4e014a5

Browse files
committed
Initial synchronization section code.
1 parent a0df79a commit 4e014a5

File tree

6 files changed

+77
-0
lines changed

6 files changed

+77
-0
lines changed

synchs1/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
sample[0-9][0-9]
2+
counting

synchs1/GNUmakefile

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
PROGRAMS = sample01 sample02 sample03 counting
2+
3+
all: $(PROGRAMS)
4+
5+
ALLPROGRAMS = $(PROGRAMS)
6+
CONFIGMK ?= config.mk
7+
8+
O ?= 3
9+
PTHREAD ?= 1
10+
include ../common/rules.mk
11+
-include $(CONFIGMK)
12+
13+
%.o: %.cc $(BUILDSTAMP)
14+
$(CXX) $(CPPFLAGS) $(CXXFLAGS) $(DEPCFLAGS) $(O) -o $@ -c $<
15+
16+
$(PROGRAMS): \
17+
%: %.o
18+
$(CXX) $(CXXFLAGS) $(O) $(LDFLAGS) -o $@ $^ $(LIBS)
19+
20+
21+
clean:
22+
rm -f $(ALLPROGRAMS) *.o
23+
rm -rf $(DEPSDIR) *.dSYM
24+
25+
.PHONY: all always clean

synchs1/counting.cc

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <cstdio>
2+
#include <cassert>
3+
#include <thread>
4+
#include <mutex>
5+
#include <condition_variable>
6+
7+
/* G1 */
8+
9+
void counter(int n) {
10+
/* C1 */
11+
printf("%d\n", n + 1);
12+
/* C2 */
13+
}
14+
15+
int main() {
16+
/* M1 */
17+
std::thread t1(counter, 0);
18+
std::thread t2(counter, 1);
19+
std::thread t3(counter, 2);
20+
/* M2 */
21+
t3.join();
22+
t2.join();
23+
t1.join();
24+
}

synchs1/sample01.cc

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include <cstdio>
2+
#include <thread>
3+
4+
int main() {
5+
for (int i = 0; i != 3; ++i) {
6+
std::thread t(printf, "%d\n", i + 1);
7+
t.join();
8+
}
9+
}

synchs1/sample02.cc

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include <cstdio>
2+
#include <thread>
3+
4+
int main() {
5+
for (int i = 0; i != 3; ++i) {
6+
std::thread t(printf, "%d\n", i + 1);
7+
t.detach();
8+
}
9+
}

synchs1/sample03.cc

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include <cstdio>
2+
#include <thread>
3+
4+
int main() {
5+
for (int i = 0; i != 3; ++i) {
6+
std::thread t(printf, "%d\n", i + 1);
7+
}
8+
}

0 commit comments

Comments
 (0)