Skip to content

How to create a thread ?

Gammasoft edited this page Nov 13, 2023 · 22 revisions

| xtd | News | Gallery | Examples | Downloads | Documentation | Wiki | Support | Sources | Project | Gammasoft |

There are many method to create a simple thread with xtd.

Using thread class instantiation

The usual first method is to create an instance of the xtd::threading::thread class and associate a delegate with it to run in the thread. You can start, join, detach, control the priority, get and set properties and manage the created thread.

#include <xtd/threading/thread>
#include <xtd/console>

using namespace xtd;
using namespace xtd::threading;

auto main()->int {
  auto value = 0;
  console::write_line("Start : value = {}", value);
  
  auto some_thread = thread {[&] {
    // Do some work in the thread...
    console::write_line("(thread {}) Incrementing value...", thread::current_thread().managed_thread_id());
    for (auto index = 0; index < 300; ++index) {
      ++value;
      thread::sleep(1_ms);
    }
  }};
  
  // Running some_thread.
  some_thread.start();
  
  // Do some work in the main thread...
  console::write_line("(main thread {}) do something...", thread::current_thread().managed_thread_id());
  thread::sleep(100_ms);

  // Wait for the thread to end.
  some_thread.join();

  console::write_line("Stop : value = {}", value);
}

Using the begin_invoke and end_invoke methods of the delegate class

The delegate class contains begin_invoke and end_invoke methods that allow you to easily execute a delegate in asynchronous mode. These methods use the thread_pool class to distribute asynchronous tasks over a pool of threads managed for you.

#include <xtd/console>

using namespace xtd;

auto main()->int {
  auto value = 0;
  console::write_line("Start : value = {}", value);
  
  auto some_delegate = delegate<void()> {[&] {
    // Do some work in the thread...
    console::write_line("(thread {}) Incrementing value...", xtd::threading::thread::current_thread().managed_thread_id());
    for (auto index = 0; index < 300; ++index) {
      ++value;
      xtd::threading::thread::sleep(1_ms);
    }
  }};
  
  // Running some_thread.
  auto async_result = some_delegate.begin_invoke();
  
  // Do some work in the main thread...
  console::write_line("(main thread {}) do something...", xtd::threading::thread::current_thread().managed_thread_id());
  xtd::threading::thread::sleep(100_ms);

  // Wait for the delegate to end.
  some_delegate.end_invoke(async_result);

  console::write_line("Stop : value = {}", value);
}

Using the methods of the thread pool class

#include <xtd/threading/thread_pool>
#include <xtd/threading/manual_reset_event>
#include <xtd/console>

using namespace xtd;
using namespace xtd::threading;

auto main()->int {
  auto value = 0;
  console::write_line("Start : value = {}", value);
  auto event = manual_reset_event {};
  
  thread_pool::queue_user_work_item([&] {
    // Do some work in the thread...
    console::write_line("(thread {}) Incrementing value...", thread::current_thread().managed_thread_id());
    for (auto index = 0; index < 300; ++index) {
      ++value;
      thread::sleep(1_ms);
    }
    
    // Signal the end of task.
    event.set();
  });
  
  // Do some work in the main thread...
  console::write_line("(main thread {}) do something...", thread::current_thread().managed_thread_id());
  thread::sleep(100_ms);

  // Wait for the thread_pool ask to end.
  event.wait_one();

  console::write_line("Stop : value = {}", value);
}

Remarks

You can of course use std::thread or any other third-party thread library.

See