Skip to content

Commit

Permalink
Add main_thread property
Browse files Browse the repository at this point in the history
  • Loading branch information
gammasoft71 committed Aug 10, 2023
1 parent 26373eb commit 2b39332
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
2 changes: 2 additions & 0 deletions src/xtd.core/include/xtd/threading/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ namespace xtd {
/// @return thread.
static thread& current_thread();

static thread& main_thread();

/// @brief Represents an invalid native operating system handle. This field is read-only.
/// @remarks Used internally to initialize the xtd::thread::wait_handle::handle property.
static const intptr invalid_handle;
Expand Down
21 changes: 12 additions & 9 deletions src/xtd.core/src/xtd/threading/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,7 @@ thread& thread::current_thread() {
std::lock_guard<std::recursive_mutex> lock {__mutex_for_threads_access__};
intptr id = get_current_thread_id();

if (id == main_thread_id_) {
if (threads_.at(0)->data_->managed_thread_id != main_managed_thread_id) {
threads_.at(0)->data_->handle = get_current_thread_handle();
threads_.at(0)->data_->managed_thread_id = main_managed_thread_id;
threads_.at(0)->data_->state &= ~xtd::threading::thread_state::unstarted;
threads_.at(0)->data_->thread_id = get_current_thread_id();
}
return *threads_.at(0);
}
if (id == main_thread_id_) return main_thread();

for (auto index = 2ul; index < threads_.size(); ++index) {
if (threads_[index]->data_->thread_id == id)
Expand All @@ -88,6 +80,17 @@ thread& thread::current_thread() {
return *threads_.at(1);
}

thread& thread::main_thread() {
std::lock_guard<std::recursive_mutex> lock {__mutex_for_threads_access__};
if (threads_.at(0)->data_->managed_thread_id != main_managed_thread_id) {
threads_.at(0)->data_->handle = get_current_thread_handle();
threads_.at(0)->data_->managed_thread_id = main_managed_thread_id;
threads_.at(0)->data_->state &= ~xtd::threading::thread_state::unstarted;
threads_.at(0)->data_->thread_id = get_current_thread_id();
}
return *threads_.at(0);
}

thread::thread() : data_(std::make_shared<data>()) {
}

Expand Down

0 comments on commit 2b39332

Please sign in to comment.