Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8148975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:45:23+00:00 2026-06-06T14:45:23+00:00

Another SO thread explains how to use thread::native_handle to do things that are outside

  • 0

Another SO thread explains how to use thread::native_handle to do things that are outside the C++ threading API (e.g., set the priority of a thread). The gist is:

std::thread t(functionToRun);
auto nh = t.native_handle());
// configure t using nh           

The problem with this approach is that functionToRun may execute for an arbitrary amount of time (including to completion) before the code to configure t is finished.

I believe we can prevent that from happening as follows (untested):

std::atomic<bool> configured(false);

std::thread t([&]{ while (!configured);   // spin until configured is true
                   functionToRun(); });
auto nh = t.native_handle();
// configure t using nh
configured = true;

Unfortunately, this causes the spawned thread to spin waiting for configured to become true. It’d be preferable for the spawned thread to block until configuration is complete.

One way to accomplish that seems to be to use a mutex (also untested):

std::mutex m;

std::unique_lock<std::mutex> lock(m);

std::thread t([&]{ std::lock_guard<std::mutex> lg(m);   // block until m available
                   functionToRun(); });
auto nh = t.native_handle();
// configure t using nh
lock.unlock();                                          // allow t to continue

This seems like it should work, but, conceptually, it seems like a condvar is more suited to the job of indicating when a condition (configuration is complete) is satisfied. But using a condvar requires all of the above, plus a condvar, and it would require dealing with the possibility of spurious wakes, which, as far as I know, is not an issue for mutexes.

Is there a better way to spawn a thread and then have it immediately stop so that I can use its native handle to configure it before allowing it to run further?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T14:45:24+00:00Added an answer on June 6, 2026 at 2:45 pm

    When I wish to delay the start of a thread’s main function until some external configuration has been completed I use a future. This avoids the spin-wait, and has the same blocking properties as a mutex or condvar, but provides a clearer intention. Using this pattern, you could write your example as:

    std::promise<void> p;
    std::thread t([&p]{
        p.get_future().wait();
        thread_func();
    }
    
    auto nh=t.native_handle();
    // configure nh
    p.set_value();
    

    I especially like using this pattern with shared_future for multithreaded tests — this way you can ensure all threads are running and ready to go before the test begins.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I originally started this question in another thread, but that thread was sorta, kinda
I am tring to setText in another thread, that is, child thread. But for
I have a windows form on the main thread and another thread that does
Whenever I want to modify a winform from another thread, I need to use
I imagined that WndProc is called on another thread rather than main UI thread.
Answering another thread (see stackoverflow: generate css color schemes ) I bumped into the
[Note: There is another thread about this problem but it did not answer the
I want to access a scrollviewer from another thread. Please tell me how to
As I don't want to hijack another thread here comes my question about mappings.
I want to update a Object in another thread, and then access it in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.