xtd 0.2.0
mixing_std_and_xtd_threads.cpp

Shows how to use and mixing xtd::threading::thread and std::thread classes.

#include <xtd/threading/thread>
#include <xtd/console>
#include <xtd/startup>
#include <thread>
using namespace xtd;
using namespace xtd::threading;
namespace mixing_std_and_xtd_threads_example {
class mixing_std_and_xtd_threads_class {
private:
static string to_string(const thread& thread) {
return string::format(" thread={{name={}, managed_thread_id={}, priority={}, thread_id=0x{:x}, state={}}}", thread.name(), thread.managed_thread_id(), thread.priority(), thread.thread_id(), thread.thread_state());
}
public:
static auto main() {
console::write_line("threads {");
thread::current_thread().name("main");
thread::sleep(20_ms);
}};
t1.name("xtd_thread_1");
t1.start();
std::thread t2 {[] {
thread::current_thread().name("std_thread_2");
}};
}};
t3.name("xtd_thread_3");
t3.is_background(true);
t3.start();
std::thread t4 {[] {
thread::sleep(10_ms);
thread::current_thread().name("std_thread_4");
}};
if (t1.joinable()) t1.join();
if (t2.joinable()) t2.join();
if (t3.joinable()) t3.join();
if (t4.joinable()) t4.join();
}
};
}
startup_(mixing_std_and_xtd_threads_example::mixing_std_and_xtd_threads_class::main);
// This code produces the following output :
//
// threads {
// thread={name=main, managed_thread_id=1, priority=normal, thread_id=0x20245e080, state=running}
// thread={name=xtd_thread_3, managed_thread_id=3, priority=normal, thread_id=0x16ff9f000, state=background}
// thread={name=std_thread_2, managed_thread_id=0, priority=highest, thread_id=0x16ff13000, state=background}
// thread={name=std_thread_4, managed_thread_id=0, priority=highest, thread_id=0x16ff13000, state=background}
// thread={name=xtd_thread_1, managed_thread_id=2, priority=below_normal, thread_id=0x16fe87000, state=running}
// }
static void write_line()
Writes the current line terminator to the standard output stream using the specified format informati...
Creates and controls a thread, sets its priority, and gets its status.
Definition thread.hpp:45
xtd::threading::thread_priority priority() const noexcept
Gets a value indicating the scheduling priority of a thread.
intptr thread_id() const noexcept
Gets the native operating system thread id.
static thread & current_thread() noexcept
Gets the currently running thread.
static void sleep(int32 milliseconds_timeout)
Suspends the current thread for a specified time.
string name() const noexcept
Gets the name of the thread.
int32 managed_thread_id() const noexcept
Gets a unique identifier for the current managed thread.
xtd::threading::thread_state thread_state() const noexcept
Gets a value containing the states of the current thread.
#define startup_(main_method)
Defines the entry point to be called when the application loads. Generally this is set either to the ...
Definition startup.hpp:168
@ below_normal
The system::threading::thread can be scheduled after threads with thread_priority::normal priority an...
Definition thread_priority.hpp:28
@ highest
The system::threading::thread can be scheduled before threads with any other priority.
Definition thread_priority.hpp:34
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
string to_string() const noexcept override
Returns the string representation of this xtd::read_only_span <type_t> object.
Definition read_only_span.hpp:375