user1397417
user1397417

Reputation: 738

c++ simple start a function with its own thread

i had once had a very simple one or two line code that would start a function with its own thread and continue running until application closed, c++ console app. lost the project it was in, and remember it was hard to find. cant find it online now. most example account for complicated multithreading situations. but i just need to open this one function in its own thread. hopefully someone knows what im talking about, or a similar solution.

eg. start void abc in its own thread, no parameters

Upvotes: 1

Views: 263

Answers (1)

juanchopanza
juanchopanza

Reputation: 227370

An example using C++11 thread support:

#include <thread>

void abc(); // function declaration

int main()
{
  std::thread abcThread(abc); // starts abc() on a separate thread

  ....

  abcThread.join(); // waits until abcThread is done.
}

If you have no C++11 support, the same is possible using boost::thread, just by replacing std::thread by boost::thread.

Upvotes: 5

Related Questions