On 7/28/23 17:36, Pedro Alves wrote: > On 2023-07-28 11:59, Tom de Vries via Gdb-patches wrote: >> I wrote a patch using is_main_thread (), and found it returning false in the >> main thread due to main_thread_id not being initialized yet. >> >> Initialization currently takes place in _initialize_run_on_main_thread, but >> that's too late for earlier uses. >> >> Fix this by also initializing on first use. >> >> Tested on x86_64-linux. >> --- >> gdb/run-on-main-thread.c | 17 ++++++++++++++++- >> 1 file changed, 16 insertions(+), 1 deletion(-) >> >> diff --git a/gdb/run-on-main-thread.c b/gdb/run-on-main-thread.c >> index 91d25dae28f..bee5885d9a6 100644 >> --- a/gdb/run-on-main-thread.c >> +++ b/gdb/run-on-main-thread.c >> @@ -94,12 +94,27 @@ run_on_main_thread (std::function &&func) >> serial_event_set (runnable_event); >> } >> >> +#if CXX_STD_THREAD >> +static void >> +initialize_main_thread_id (void) >> +{ >> + static bool initialized = false; >> + >> + if (initialized) >> + return; >> + initialized = true; >> + >> + main_thread_id = std::this_thread::get_id (); >> +} >> +#endif >> + >> /* See run-on-main-thread.h. */ >> >> bool >> is_main_thread () >> { >> #if CXX_STD_THREAD >> + initialize_main_thread_id (); > Hi Pedro, thanks for the review. > This is assuming that is_main_thread() will always be called once by > the main thread, before any other thread could call it. Otherwise if > is_main_thread is called by some other thread first, > is_main_thread -> initialize_main_thread_id will record the wrong thread > id in the main_thread_id. At least a comment somewhere would > be warranted. I think putting: > > gdb_assert (is_main_thread ()); > > somewhere in the early main code would be good. It would prevent such > situation from ever happening undetected. > Updated accordingly. Any further comments? Thanks, - Tom