Skip to content

Commit f5a8f78

Browse files
committed
Add manual_reset_event example
1 parent af2860b commit f5a8f78

File tree

6 files changed

+176
-0
lines changed

6 files changed

+176
-0
lines changed

examples/xtd.core.examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@
278278
* [event_wait_handle](threading/event_wait_handle/README.md) shows how to use [xtd::threading::event_wait_handle](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1threading_1_1event__wait__handle.html) class.
279279
* [interlocked](threading/interlocked/README.md) shows hows how to use [xtd::threading::interlocked](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1threading_1_1interlocked.html) class.
280280
* [interlocked_decrement](threading/interlocked_decrement/README.md) shows hows how to use [xtd::threading::interlocked::decrement](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1threading_1_1interlocked.html#a4f4545f0c5952db7df8ff6fc4aa41067) and [xtd::threading::interlocked::increment](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1threading_1_1interlocked.html#acf60d0d23279ede3b23ddee39265aadd) methods.
281+
* [manual_reset_event](threading/manual_reset_event/README.md) 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.
281282
* [mixing_std_and_xtd_threads](threading/mixing_std_and_xtd_threads/README.md) shows how to use and mixing [xtd::threading::thread](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1threading_1_1thread.html) and [std::thread](https://en.cppreference.com/w/cpp/thread/thread) classes.
282283
* [thread](threading/thread/README.md) shows hows how to use [xtd::threading::thread](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1threading_1_1thread.html) class.
283284

examples/xtd.core.examples/threading/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ add_projects(
88
event_wait_handle
99
interlocked
1010
interlocked_decrement
11+
manual_reset_event
1112
mixing_std_and_xtd_threads
1213
thread
1314
)

examples/xtd.core.examples/threading/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* [event_wait_handle](event_wait_handle/README.md) shows how to use [xtd::threading::event_wait_handle](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1threading_1_1event__wait__handle.html) class.
77
* [interlocked](interlocked/README.md) shows hows how to use [xtd::network::interlocked](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1threading_1_1interlocked.html) class.
88
* [interlocked_decrement](interlocked_decrement/README.md) shows hows how to use [xtd::threading::interlocked::decrement](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1threading_1_1interlocked.html#a4f4545f0c5952db7df8ff6fc4aa41067) and [xtd::threading::interlocked::increment](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1threading_1_1interlocked.html#acf60d0d23279ede3b23ddee39265aadd) methods.
9+
* [manual_reset_event](manual_reset_event/README.md) 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.
910
* [mixing_std_and_xtd_threads](mixing_std_and_xtd_threads/README.md) shows how to use and mixing [xtd::threading::thread](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1threading_1_1thread.html) and [std::thread](https://en.cppreference.com/w/cpp/thread/thread) classes.
1011
* [thread](thread/README.md) shows hows how to use [xtd::threading::thread](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1threading_1_1thread.html) class.
1112

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.5)
2+
3+
project(manual_reset_event)
4+
find_package(xtd REQUIRED)
5+
add_sources(README.md src/manual_reset_event.cpp)
6+
target_type(CONSOLE_APPLICATION)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# manual_reset_event
2+
3+
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.
4+
5+
## Sources
6+
7+
[src/manual_reset_event.cpp](src/manual_reset_event.cpp)
8+
9+
[CMakeLists.txt](CMakeLists.txt)
10+
11+
# Build and run
12+
13+
Open "Command Prompt" or "Terminal". Navigate to the folder that contains the project and type the following:
14+
15+
```cmake
16+
xtdc run
17+
```
18+
19+
# Output
20+
21+
```
22+
Start 3 named threads that block on a ManualresetEvent:
23+
24+
Thread_0 starts and calls mre.WaitOne()
25+
Thread_2 starts and calls mre.WaitOne()
26+
Thread_1 starts and calls mre.WaitOne()
27+
28+
When all three threads have started, press Enter to call set()
29+
to release all the threads.
30+
31+
32+
Thread_2 ends.
33+
Thread_0 ends.
34+
Thread_1 ends.
35+
36+
When a ManualresetEvent is signaled, threads that call WaitOne()
37+
do not block. Press Enter to show this.
38+
39+
40+
Thread_3 starts and calls mre.WaitOne()
41+
Thread_4 starts and calls mre.WaitOne()
42+
Thread_3 ends.
43+
Thread_4 ends.
44+
45+
Press Enter to call reset(), so that threads once again block
46+
when they call WaitOne().
47+
48+
49+
Thread_5 starts and calls mre.WaitOne()
50+
51+
Press Enter to call set() and conclude the demo.
52+
53+
Thread_5 ends.
54+
```
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
#include <xtd/threading/manual_reset_event>
2+
#include <xtd/threading/thread>
3+
#include <xtd/console>
4+
#include <xtd/startup>
5+
6+
using namespace xtd;
7+
using namespace xtd::threading;
8+
9+
namespace manual_reset_event_example {
10+
class program {
11+
public:
12+
static void main() {
13+
console::write_line("\nStart 3 named threads that block on a ManualresetEvent:\n");
14+
15+
for(auto i = 0; i <= 2; ++i) {
16+
auto t = thread {thread_start {thread_proc}};
17+
t.name(ustring::format("Thread_{}", i));
18+
t.start();
19+
}
20+
21+
thread::sleep(500);
22+
console::write_line("\nWhen all three threads have started, press Enter to call set()"
23+
"\nto release all the threads.\n");
24+
console::read_line();
25+
26+
mre.set();
27+
28+
thread::sleep(500);
29+
console::write_line("\nWhen a ManualresetEvent is signaled, threads that call WaitOne()"
30+
"\ndo not block. Press Enter to show this.\n");
31+
console::read_line();
32+
33+
for(auto i = 3; i <= 4; ++i) {
34+
auto t = thread {thread_start {thread_proc}};
35+
t.name(ustring::format("Thread_{}", i));
36+
t.start();
37+
}
38+
39+
thread::sleep(500);
40+
console::write_line("\nPress Enter to call reset(), so that threads once again block"
41+
"\nwhen they call WaitOne().\n");
42+
console::read_line();
43+
44+
mre.reset();
45+
46+
// Start a thread that waits on the ManualresetEvent.
47+
auto t5 = thread {thread_start {thread_proc}};
48+
t5.name("Thread_5");
49+
t5.start();
50+
51+
thread::sleep(500);
52+
console::write_line("\nPress Enter to call set() and conclude the demo.");
53+
console::read_line();
54+
55+
mre.set();
56+
57+
// If you run this example in Visual Studio, uncomment the following line:
58+
//console::read_line();
59+
}
60+
61+
private:
62+
// mre is used to block and release threads manually. It is
63+
// created in the unsignaled state.
64+
inline static manual_reset_event mre {false};
65+
66+
static void thread_proc() {
67+
ustring name = thread::current_thread().name();
68+
69+
console::write_line(name + " starts and calls mre.WaitOne()");
70+
71+
mre.wait_one();
72+
73+
console::write_line(name + " ends.");
74+
}
75+
};
76+
}
77+
78+
startup_(manual_reset_event_example::program);
79+
80+
// This example produces output similar to the following:
81+
//
82+
// Start 3 named threads that block on a ManualresetEvent:
83+
//
84+
// Thread_0 starts and calls mre.WaitOne()
85+
// Thread_2 starts and calls mre.WaitOne()
86+
// Thread_1 starts and calls mre.WaitOne()
87+
//
88+
// When all three threads have started, press Enter to call set()
89+
// to release all the threads.
90+
//
91+
//
92+
// Thread_2 ends.
93+
// Thread_0 ends.
94+
// Thread_1 ends.
95+
//
96+
// When a ManualresetEvent is signaled, threads that call WaitOne()
97+
// do not block. Press Enter to show this.
98+
//
99+
//
100+
// Thread_3 starts and calls mre.WaitOne()
101+
// Thread_4 starts and calls mre.WaitOne()
102+
// Thread_3 ends.
103+
// Thread_4 ends.
104+
//
105+
// Press Enter to call reset(), so that threads once again block
106+
// when they call WaitOne().
107+
//
108+
//
109+
// Thread_5 starts and calls mre.WaitOne()
110+
//
111+
// Press Enter to call set() and conclude the demo.
112+
//
113+
// Thread_5 ends.

0 commit comments

Comments
 (0)