Is it possible to make a function always run in the same thread? [C++]

  c++, multithreading

Simple Example you have two functions, foo and baz. Baz calls foo. Even baz is launched from a separate thread can you run the foo it calls in the main thread?

int foo(int Z)
{
    cout << Z;
    return 0;
}

int baz(int Z)
{
   return foo(-Z);
}


int main()
{
   
   foo(6); // foo is run in main thread
   baz(6); // baz is run in main thread
  thread th2(baz,7); // baz is run in thread 2, but its call to foo is run in the main thread

}

Source: Windows Questions C++

LEAVE A COMMENT