-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
af2860b
commit f5a8f78
Showing
6 changed files
with
176 additions
and
0 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 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
6 changes: 6 additions & 0 deletions
6
examples/xtd.core.examples/threading/manual_reset_event/CMakeLists.txt
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,6 @@ | ||
cmake_minimum_required(VERSION 3.5) | ||
|
||
project(manual_reset_event) | ||
find_package(xtd REQUIRED) | ||
add_sources(README.md src/manual_reset_event.cpp) | ||
target_type(CONSOLE_APPLICATION) |
54 changes: 54 additions & 0 deletions
54
examples/xtd.core.examples/threading/manual_reset_event/README.md
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,54 @@ | ||
# manual_reset_event | ||
|
||
Shows how to use [xtd::threading::manual_reset_event](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1threading_1_1manual_reset_event.html) class. | ||
|
||
## Sources | ||
|
||
[src/manual_reset_event.cpp](src/manual_reset_event.cpp) | ||
|
||
[CMakeLists.txt](CMakeLists.txt) | ||
|
||
# Build and run | ||
|
||
Open "Command Prompt" or "Terminal". Navigate to the folder that contains the project and type the following: | ||
|
||
```cmake | ||
xtdc run | ||
``` | ||
|
||
# Output | ||
|
||
``` | ||
Start 3 named threads that block on a ManualresetEvent: | ||
Thread_0 starts and calls mre.WaitOne() | ||
Thread_2 starts and calls mre.WaitOne() | ||
Thread_1 starts and calls mre.WaitOne() | ||
When all three threads have started, press Enter to call set() | ||
to release all the threads. | ||
Thread_2 ends. | ||
Thread_0 ends. | ||
Thread_1 ends. | ||
When a ManualresetEvent is signaled, threads that call WaitOne() | ||
do not block. Press Enter to show this. | ||
Thread_3 starts and calls mre.WaitOne() | ||
Thread_4 starts and calls mre.WaitOne() | ||
Thread_3 ends. | ||
Thread_4 ends. | ||
Press Enter to call reset(), so that threads once again block | ||
when they call WaitOne(). | ||
Thread_5 starts and calls mre.WaitOne() | ||
Press Enter to call set() and conclude the demo. | ||
Thread_5 ends. | ||
``` |
113 changes: 113 additions & 0 deletions
113
examples/xtd.core.examples/threading/manual_reset_event/src/manual_reset_event.cpp
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,113 @@ | ||
#include <xtd/threading/manual_reset_event> | ||
#include <xtd/threading/thread> | ||
#include <xtd/console> | ||
#include <xtd/startup> | ||
|
||
using namespace xtd; | ||
using namespace xtd::threading; | ||
|
||
namespace manual_reset_event_example { | ||
class program { | ||
public: | ||
static void main() { | ||
console::write_line("\nStart 3 named threads that block on a ManualresetEvent:\n"); | ||
|
||
for(auto i = 0; i <= 2; ++i) { | ||
auto t = thread {thread_start {thread_proc}}; | ||
t.name(ustring::format("Thread_{}", i)); | ||
t.start(); | ||
} | ||
|
||
thread::sleep(500); | ||
console::write_line("\nWhen all three threads have started, press Enter to call set()" | ||
"\nto release all the threads.\n"); | ||
console::read_line(); | ||
|
||
mre.set(); | ||
|
||
thread::sleep(500); | ||
console::write_line("\nWhen a ManualresetEvent is signaled, threads that call WaitOne()" | ||
"\ndo not block. Press Enter to show this.\n"); | ||
console::read_line(); | ||
|
||
for(auto i = 3; i <= 4; ++i) { | ||
auto t = thread {thread_start {thread_proc}}; | ||
t.name(ustring::format("Thread_{}", i)); | ||
t.start(); | ||
} | ||
|
||
thread::sleep(500); | ||
console::write_line("\nPress Enter to call reset(), so that threads once again block" | ||
"\nwhen they call WaitOne().\n"); | ||
console::read_line(); | ||
|
||
mre.reset(); | ||
|
||
// Start a thread that waits on the ManualresetEvent. | ||
auto t5 = thread {thread_start {thread_proc}}; | ||
t5.name("Thread_5"); | ||
t5.start(); | ||
|
||
thread::sleep(500); | ||
console::write_line("\nPress Enter to call set() and conclude the demo."); | ||
console::read_line(); | ||
|
||
mre.set(); | ||
|
||
// If you run this example in Visual Studio, uncomment the following line: | ||
//console::read_line(); | ||
} | ||
|
||
private: | ||
// mre is used to block and release threads manually. It is | ||
// created in the unsignaled state. | ||
inline static manual_reset_event mre {false}; | ||
|
||
static void thread_proc() { | ||
ustring name = thread::current_thread().name(); | ||
|
||
console::write_line(name + " starts and calls mre.WaitOne()"); | ||
|
||
mre.wait_one(); | ||
|
||
console::write_line(name + " ends."); | ||
} | ||
}; | ||
} | ||
|
||
startup_(manual_reset_event_example::program); | ||
|
||
// This example produces output similar to the following: | ||
// | ||
// Start 3 named threads that block on a ManualresetEvent: | ||
// | ||
// Thread_0 starts and calls mre.WaitOne() | ||
// Thread_2 starts and calls mre.WaitOne() | ||
// Thread_1 starts and calls mre.WaitOne() | ||
// | ||
// When all three threads have started, press Enter to call set() | ||
// to release all the threads. | ||
// | ||
// | ||
// Thread_2 ends. | ||
// Thread_0 ends. | ||
// Thread_1 ends. | ||
// | ||
// When a ManualresetEvent is signaled, threads that call WaitOne() | ||
// do not block. Press Enter to show this. | ||
// | ||
// | ||
// Thread_3 starts and calls mre.WaitOne() | ||
// Thread_4 starts and calls mre.WaitOne() | ||
// Thread_3 ends. | ||
// Thread_4 ends. | ||
// | ||
// Press Enter to call reset(), so that threads once again block | ||
// when they call WaitOne(). | ||
// | ||
// | ||
// Thread_5 starts and calls mre.WaitOne() | ||
// | ||
// Press Enter to call set() and conclude the demo. | ||
// | ||
// Thread_5 ends. |