Вы можете использовать std :: thread или std :: future / std :: asyn c. Для этих «задач» лучше / проще использовать std :: assync / future, поскольку управление потоками выполняется за вас.
bool func1(int a) {...}
bool func2(int a) {...}
void some_func()
{
std::future<bool> f1 = std::async(std::launch::async, func1, 1);
std::future<bool> f2 = std::async(std::launch::async, func1, 2);
bool res1 = f1.get(); // Only need this if you care about the result
bool res2 = f2.get(); // Only need this if you care about the result
}
Если вас не волнуют результаты, вы не Мне нужны последние две строчки. Но .get()
в основном позволяет вам дождаться завершения работы ваших функций sh. Есть и другие варианты для этого ... но это довольно общий вопрос ...
Потоки и лямбды:
bool func1(int a) {...}
bool func2(int a) {...}
void some_func()
{
std::thread t1 = []{ return func1(1); };
std::thread t2 = []{ return func2(2); };
// You must do this, otherwise your threads will go out of scope and std::terminate is called!
if (t1.joinable())
{
t1.join()
}
if (t2.joinable())
{
t2.join()
}
// Or instead of joining you can detach. But this is not recommend as you lose the ability to control your thread (left commented out as an example)
// t1.detach();
// t2.detach();
}
Обновить
Ссылка на ваш «фиксированный» код: https://onlinegdb.com/S1hcwRAsL
Вот фрагмент кода для вашего удобства (и я не уверен, нужно ли мне сохранять изменения! В GDB онлайн! ):
int main()
{
std::shared_ptr<classC> c = std::make_shared<classC>();
classB* b;
classA* a;
std::thread first([&b, &c]{ b->functionB(c); });
std::thread second([&a, &c]{ a->functionA(c); });
// synchronize threads:
first.join();
second.join();
std::cout << "A and B completed.\n";
return 0;
}