-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Integrate atomic exercise as a final task in race exercise.
- Loading branch information
Showing
14 changed files
with
115 additions
and
187 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,17 @@ | ||
PROGRAM_NAME=racing | ||
|
||
all: $(PROGRAM_NAME) | ||
solution: $(PROGRAM_NAME).sol | ||
solution: $(PROGRAM_NAME).sol1 $(PROGRAM_NAME).sol2 | ||
|
||
|
||
clean: | ||
rm -f *o $(PROGRAM_NAME) *~ core $(PROGRAM_NAME).sol | ||
rm -f *o $(PROGRAM_NAME) *~ core $(PROGRAM_NAME).sol? | ||
|
||
$(PROGRAM_NAME) : $(PROGRAM_NAME).cpp | ||
${CXX} ${CXXFLAGS} -g -std=c++17 -O2 -pthread -Wall -Wextra -L. -o $@ $< | ||
|
||
$(PROGRAM_NAME).sol : solution/$(PROGRAM_NAME).sol.cpp | ||
$(PROGRAM_NAME).sol1 : solution/$(PROGRAM_NAME).sol1.cpp | ||
${CXX} ${CXXFLAGS} -g -std=c++17 -O2 -pthread -Wall -Wextra -L. -o $@ $< | ||
|
||
$(PROGRAM_NAME).sol2 : solution/$(PROGRAM_NAME).sol2.cpp | ||
${CXX} ${CXXFLAGS} -g -std=c++17 -O2 -pthread -Wall -Wextra -L. -o $@ $< |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,16 @@ | ||
|
||
## Instructions | ||
|
||
* Compile and run the executable, see if it races | ||
* If you have a bash shell, try `./run ./racing`, which keeps invoking the executable | ||
until a race condition is detected | ||
* (Optional) You can use `valgrind --tool=helgrind ./racing` to prove your assumption | ||
* (Optional) If your operating system supports it, recompile with thread sanitizer. | ||
The program `racing.cpp` is incrementing a shared integer many times, within several threads, which should lead to race conditions if no specific protection is used. The values of the global parameters `nThread`, `nInc` and `nRepeat` can be custommized for your own computer. | ||
|
||
Tasks | ||
- Compile and run the executable, check it races. | ||
- If you have a bash shell, try `./run ./racing`, which keeps invoking the executable until a race condition is detected. | ||
- (Optional) You can use `valgrind --tool=helgrind ./racing` to prove your assumption | ||
- (Optional) If your operating system supports it, recompile with thread sanitizer. | ||
With Makefile, use e.g. `make CXXFLAGS="-fsanitize=thread"` | ||
* Use a mutex to fix the issue | ||
* See the difference in execution time | ||
* (Optional) Check again with `valgrind` or thread sanitizer if the problem is fixed | ||
- Use a `std::mutex` to fix the issue. | ||
- See the difference in execution time, for example with `time ./racing`. | ||
You might have to increase `nRepeat` if it completes too fast, or lower it if it takes too long. | ||
- (Optional) Check again with `valgrind` or thread sanitizer if the problem is fixed. | ||
- Try to use `std::atomic` instead of the mutex, and compare the execution time. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
|
||
#include <iostream> | ||
#include <vector> | ||
#include <thread> | ||
#include <mutex> | ||
|
||
constexpr std::size_t nThread = 10; | ||
constexpr std::size_t nInc = 1000; | ||
constexpr std::size_t nRepeat = 1000; | ||
|
||
int main() { | ||
int nError = 0; | ||
|
||
for (std::size_t j = 0; j < nRepeat; j++) { | ||
int a = 0; | ||
std::mutex aMutex; | ||
|
||
// Increment the variable a 100 times: | ||
auto increment = [&a,&aMutex](){ | ||
for (std::size_t i = 0; i < nInc; ++i) { | ||
std::scoped_lock lock{aMutex}; | ||
a++; | ||
} | ||
}; | ||
|
||
// Start up all threads: | ||
std::vector<std::thread> threads; | ||
for (std::size_t i = 0; i < nThread; ++i) threads.emplace_back(increment); | ||
for (auto & thread : threads) thread.join(); | ||
|
||
// Check | ||
if (a != nThread * nInc) { | ||
std::cerr << "Race detected! Result: " << a << '\n'; | ||
nError++; | ||
} | ||
} | ||
|
||
return nError; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
|
||
#include <iostream> | ||
#include <vector> | ||
#include <thread> | ||
#include <atomic> | ||
|
||
constexpr std::size_t nThread = 10; | ||
constexpr std::size_t nInc = 1000; | ||
constexpr std::size_t nRepeat = 1000; | ||
|
||
int main() { | ||
int nError = 0; | ||
|
||
for (std::size_t j = 0; j < nRepeat; j++) { | ||
std::atomic<int> a{0}; | ||
|
||
// Increment the variable a 100 times: | ||
auto increment = [&a](){ | ||
for (std::size_t i = 0; i < nInc; ++i) { | ||
a++; | ||
} | ||
}; | ||
|
||
// Start up all threads | ||
std::vector<std::thread> threads; | ||
for (std::size_t i = 0; i < nThread; ++i) threads.emplace_back(increment); | ||
for (auto & thread : threads) thread.join(); | ||
|
||
// Check | ||
if (a != nThread * nInc) { | ||
std::cerr << "Race detected! Result: " << a << '\n'; | ||
nError++; | ||
} | ||
} | ||
|
||
return nError; | ||
} |