Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/142.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在cpp中使用线程来支持来自两个不同类的两个方法的并行执行?_C++_Multithreading - Fatal编程技术网

C++ 如何在cpp中使用线程来支持来自两个不同类的两个方法的并行执行?

C++ 如何在cpp中使用线程来支持来自两个不同类的两个方法的并行执行?,c++,multithreading,C++,Multithreading,所以,我一直在尝试使用std::thread对象在另一个类的方法执行期间启用另一个类方法的执行。伪代码如下所示 class A (init_A_args ){ A::method1(args_method1) { method1 functionality; return; } A::callback_method2(args_method2) { method2 functionality; return; } } class B (init_B_ar

所以,我一直在尝试使用std::thread对象在另一个类的方法执行期间启用另一个类方法的执行。伪代码如下所示

class A (init_A_args ){
 A::method1(args_method1) {

   method1 functionality;
   return;  
}

 A::callback_method2(args_method2) {
   method2 functionality; 
   return; 
}
}

class B (init_B_args){
 B::method1(args_B_method1) {
    method1 functionality;
    return; 
}

 B::method2(args_B_method2)
    method2 functionality; 
    return; 
}
所以我想做的事情如下:

A::callback_method2(args_method2) {
   init class B; 
   for (condition){
       classB.method2(); // should call this method periodically but in such way that it doesn't
       ...;             // block execution of rest of the for loop
       rest-of-the-for-loop; 
       ...; 
   }
   classB.stop(); 
   return; 
}
我不知道怎么做,但我很确定我需要使用线程。我能够在类A回调方法中实例化类B,但是在调用classB.method2()之后;for循环停止,并在
classB.method2()中继续执行。因此
classB.method2()
在类A的回调方法中阻塞for循环。然而,我想执行
classB.method2()
,但继续执行
A::callback\u method2
中的for循环。在超出
A::callback_method2
的范围后,应停止并销毁类B。因此,我想在类A和实例化的类B中的回调方法的执行和它的method2之间进行优雅的切换。

我一直在尝试使用
std::thread
,但运气不好,我可能做错了什么。因此,为了澄清,
A::callback_method2
应该实例化类B,并在继续
循环执行时定期调用
B.method2()
,因此B.method2将在后台运行,而for循环正常执行。我有
main
函数,但它应该只实例化类A,然后再实例化类B,所以我不确定这与我的问题陈述有什么关系(因为我主要看到
thread.join()
thread.detach()
在main方法中填充。

您可以为
std::thread
对象提供一个可调用的对象(可以调用的东西)线程对象将运行可调用对象。如果不希望线程阻止执行,则应
分离
线程,使其成为守护线程,这意味着它在后台运行,而不会阻止执行的主线程。
使用此解决方案时,应考虑线程上可调用运行的副作用:如果它不影响其范围之外的变量/状态,则不必担心太多,但如果需要注意线程安全:必须查看变量的并发访问。

#include <iostream>
#include <thread>

using namespace std;

void method1()
{
    long long j = 0;
    for(int i = 0; i < 10000; ++i)
    {
        j += i;
    }
    cout << "Sum is: " << j << endl;
}

int main()
{
    thread t1(method1);
    t1.detach();
    cout << "Stuff on main thread." << endl;
    return 0;
}
#包括
#包括
使用名称空间std;
void method1()
{
长j=0;
对于(int i=0;i<10000;++i)
{
j+=i;
}

cout
std::async()使这个行为更容易管理,我认为。<代码> STD::线程是现代C++并发的较低级别的构建块。不需要使用分离线程,除非不关心它们是否完成。不是初学者的特征。这个特定程序可能或可能不打印总和,这取决于月亮的相位。是的,但是在创建线程之后,会发生这样的情况:在调用
join()
detach()
回调方法超出范围后,调用函数,这意味着我的classB被销毁,我得到
终止调用,没有活动异常
和segfault。我正在调用
join()
在超出范围之前从
A::callback_method2
分离