xtd 0.2.0
jthread.cpp

Shows how to use xtd::threading::jthread class.

#include <xtd/threading/jthread>
#include <xtd/block_scope>
#include <xtd/console>
using namespace xtd;
using namespace xtd::threading;
auto main() -> int {
auto thread_proc = [] {
thread::current_thread().name("thread_proc");
for (auto index = 0; index < 10; ++index, thread::sleep(100_ms))
console::write_line(" ({}) index = {}", thread::current_thread().name(), index);
};
thread::current_thread().name("main");
block_scope_(auto thread1 = jthread::start_new(thread_proc)) {
// Remarks :
// * The `thread1` will be joined when the scope of the current block is closed.
// * If you create `thread1` with xtd::threading::thread, `thread1` will be joined when the end of main.
// * If you create `thread1` with std::jthread, `thread1` will be joined when the scope of the current block is closed too.
// * If you create `thread1` with std::thread, there will be a runtime error.
// You can try it yourself to see the different scenarios.
}
}
// This code can produce the following output if `thread1` is created with xtd::threading::jthread :
//
// (main) begin
// (thread_proc) index = 0
// (thread_proc) index = 1
// (thread_proc) index = 2
// (thread_proc) index = 3
// (thread_proc) index = 4
// (thread_proc) index = 5
// (thread_proc) index = 6
// (thread_proc) index = 7
// (thread_proc) index = 8
// (thread_proc) index = 9
// (main) end
// This code can produce the following output if `thread1` is created with xtd::threading::thread :
//
// (main) begin
// (main) end
// (thread_proc) index = 0
// (thread_proc) index = 1
// (thread_proc) index = 2
// (thread_proc) index = 3
// (thread_proc) index = 4
// (thread_proc) index = 5
// (thread_proc) index = 6
// (thread_proc) index = 7
// (thread_proc) index = 8
// (thread_proc) index = 9
// This code can produce the following output if `thread1` is created with std::jthread :
//
// (main) begin
// (thread_proc) index = 0
// (thread_proc) index = 1
// (thread_proc) index = 2
// (thread_proc) index = 3
// (thread_proc) index = 4
// (thread_proc) index = 5
// (thread_proc) index = 6
// (thread_proc) index = 7
// (thread_proc) index = 8
// (thread_proc) index = 9
// (main) end
// This code can produce the following output if `thread1` is created std::thread :
//
// (main) begin
// libc++abi: terminating
static void write_line()
Writes the current line terminator to the standard output stream using the specified format informati...
static jthread start_new(const xtd::threading::thread_start &start)
Create and immedialtely start a xtd::threading::jthread with specified method.
static thread & current_thread() noexcept
Gets the currently running thread.
static void sleep(int32 milliseconds_timeout)
Suspends the current thread for a specified time.
#define block_scope_(...)
The specified expression is cleared automatically when the scope is ended.
Definition block_scope.hpp:25
The xtd::threading namespace provides classes and interfaces that enable multithreaded programming....
Definition abandoned_mutex_exception.hpp:10
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8