diff options
author | Mattes D <github@xoft.cz> | 2016-06-26 15:51:12 +0200 |
---|---|---|
committer | worktycho <work.tycho@gmail.com> | 2016-06-26 15:51:12 +0200 |
commit | 1462e4f70e6bc8e42f6a5b5e959b7e34f2774a73 (patch) | |
tree | b94e203e895c25d7724c3db71564fcbf4027fb9e /tests/OSSupport/StressEvent.cpp | |
parent | Merge pull request #3240 from cuberite/grownvisualizer (diff) | |
download | cuberite-1462e4f70e6bc8e42f6a5b5e959b7e34f2774a73.tar cuberite-1462e4f70e6bc8e42f6a5b5e959b7e34f2774a73.tar.gz cuberite-1462e4f70e6bc8e42f6a5b5e959b7e34f2774a73.tar.bz2 cuberite-1462e4f70e6bc8e42f6a5b5e959b7e34f2774a73.tar.lz cuberite-1462e4f70e6bc8e42f6a5b5e959b7e34f2774a73.tar.xz cuberite-1462e4f70e6bc8e42f6a5b5e959b7e34f2774a73.tar.zst cuberite-1462e4f70e6bc8e42f6a5b5e959b7e34f2774a73.zip |
Diffstat (limited to 'tests/OSSupport/StressEvent.cpp')
-rw-r--r-- | tests/OSSupport/StressEvent.cpp | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/tests/OSSupport/StressEvent.cpp b/tests/OSSupport/StressEvent.cpp new file mode 100644 index 000000000..05c47b599 --- /dev/null +++ b/tests/OSSupport/StressEvent.cpp @@ -0,0 +1,60 @@ + +// StressEvent.cpp + +// Stress-tests the cEvent implementation + +#include "Globals.h" +#include <thread> + + + + + +/** Number of repetitions of the thread loops. */ +const int NUM_REPETITIONS = 5000; + + + + +// Forward declarations are needed for clang +void runThread(cEvent * a_Event1, cEvent * a_Event2, const char * a_ThreadName); + + + + + +/** Function that runs in a separate thread, notifies event1 and waits for event2, in a loop, NUM_REPETITIONS times. +This basically simulates a producer / consumer pattern with 2 events, one for "queue empty", the other for "queue full". */ +void runThread(cEvent * a_Event1, cEvent * a_Event2, const char * a_ThreadName) +{ + LOG("Thread %s started", a_ThreadName); + for (int i = 0; i < NUM_REPETITIONS; ++i) + { + // LOGD("%s: Waiting for event %p (%d)", a_ThreadName, a_Event2, i); + a_Event2->Wait(); + // LOGD("%s: Setting event %p (%d)", a_ThreadName, a_Event1, i); + a_Event1->SetAll(); + } + LOG("Thread %s finished", a_ThreadName); +} + + + + + +int main() +{ + LOG("Test started"); + cEvent event1, event2; + event1.Set(); + std::thread thread1(&runThread, &event1, &event2, "A"); + std::thread thread2(&runThread, &event2, &event1, "B"); + thread1.join(); + thread2.join(); + LOG("Test finished"); + return 0; +} + + + + |