Skip to content

Commit

Permalink
Add manual_reset_event example
Browse files Browse the repository at this point in the history
  • Loading branch information
gammasoft71 committed Aug 13, 2023
1 parent af2860b commit f5a8f78
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/xtd.core.examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@
* [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.
* [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.
* [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.
* [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.
* [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.
* [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.

Expand Down
1 change: 1 addition & 0 deletions examples/xtd.core.examples/threading/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ add_projects(
event_wait_handle
interlocked
interlocked_decrement
manual_reset_event
mixing_std_and_xtd_threads
thread
)
1 change: 1 addition & 0 deletions examples/xtd.core.examples/threading/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* [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.
* [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.
* [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.
* [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.
* [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.
* [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.

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 examples/xtd.core.examples/threading/manual_reset_event/README.md
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.
```
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.

0 comments on commit f5a8f78

Please sign in to comment.