c++ - 多线程——在不同类的方法之间传递变量

标签 c++ multithreading boost

我正在做一个需要多线程的项目。我有三个线程,其中两个并行运行,一个异步运行,如示例代码所示。我有几个关于变量和 boost::shared_mutex

的问题
  1. Is there a more elegant approach to pass vectors and other variables between methods?
  2. We are having problems with the boost::shared_mutex in locking critical sections. Is there a better method in this case?

感谢您的帮助。对不起代码的长度。

// A.h
class A{
private:
       std::vector<int> a_data;
public:
       int capture_a(std::vector<int> &data_transfer,boost::shared_mutex &_access,int &flag);
};

// A.cpp
A::capture_a(std::vector<int> &a_data_transfer,boost::shared_mutex &_access,int &a_flag)
{
     // collect data 
     while(true)
     {
        //data input
        a_data.push_back(data);
        if(a_data.size() > N) //a certain limit
        {
             // save a_data
             a_flag = 1;
             boost::unique_lock< boost::shared_mutex > lock(_access);
             //swap with empty array
             // a_data_transfer should be empty
             a_data_transfer.swap(a_data);
        }
        if(boost::thread_interrupted&)
        {
             std::cout << " Thread interrupted" << std::endl;
             return 0;
        }
     }
}

// B.h
class B{
private:
    std::vector<int> b_data;
public:
    int capture_b(std::vector<int> &b_data_transfer, boost::shared_mutex &_access,int &a_flag,int &b_flag);
};

// B.cpp
B::capture_b(std::vector<int> &b_data_transfer, boost::shared_mutex &_access, int &a_flag,int &b_flag)
{
     // collect data 
     while(true)
     {
        //data input
        b_data.push_back(data);
        if(a_flag == 1) //a_flag is true
        {
             boost::unique_lock< boost::shared_mutex > lock(_access);
             b_data_transfer.swap(b_data);
             b_flag = 1;
             // save data
             a_flag = 0;

        }
        if(boost::thread_interrupted&)
        {
             std::cout << " Thread interrupted" << std::endl;
             return 0;
        }
     }
}

// C.h
class C
{
private: 
      std::vector<int> c_data;
public: 
      int compute_c(std::vector<int> &a_data,std::vector<int> &b_data,boost::shared_mutex &_access, int &b_flag);
}

// C.cpp
C::compute_c(std::vector<int> &a_data,std::vector<int> &b_data,boost::shared_mutex &_access,int &b_flag)
{
    while(true) {
     if(b_flag == 1)
     {
          boost::unique_lock< boost::shared_mutex > lock(_access);
          // compute on c
          c_data = a_data + b_data;    // for example
          // save c_data
          b_flag = 0;
          a_data.clear();
          b_data.clear();
     }
     if(boost::thread_interrupted&)
     {
         std::cout << " Thread interrupted" << std::endl;
         return 0;
     }
   }
}

int main()
{

     std::vector<int> a_data_transfer, b_data_transfer;
     boost::shared_mutex _access;
     int a_flag = 0, b_flag = 0;
     boost::thread t1(&A::capture_a,boost::ref(a_data_transfer),boost::ref(_access),boost::ref(a_flag));
     boost::thread t2(&B::capture_b,boost::ref(b_data_transfer),boost::ref(_access),boost::ref(a_flag),boost::ref(b_flag));
     boost::thread t3(&C::compute_c,boost::ref(a_data_transfer),boost::ref(b_data_transfer),boost::ref(_access),boost::ref(b_flag));
     // Wait for Enter 
     char ch;
     cin.get(ch);

     // Ask thread to stop
     t1.interrupt();
     t2.interrupt();
     t3.interrupt();

     // Join - wait when thread actually exits
     t1.join();
     t2.join();
     t3.join();
}

**********编辑*************

我想要实现的是:

  1. A and B should run parallelly and when a certain criteria is met in A, the a_data and b_data should be transferred to C. After transferring the data, the vectors should keep on collecting the new data.
  2. C should take in the a_data and b_data and perform a computation when the flag is true.

boost::shared_mutex 的问题 - 我们希望 a_data_transfer 在交换时为空。它有时会发生,但并非总是如此。我需要一种方法来确保代码正常运行。

最佳答案

Is there a more elegant approach to pass vectors and other variables between methods?

嗯...优雅是品味的问题...但是您可能希望将共享数据封装到某个类或结构中,其中包含:

  1. 共享数据
  2. 保护它们的互斥量(或多个互斥量)
  3. 处理共享数据并适当锁定它们的方法。

这将简化您需要传递给线程执行的数据量。

您可能已经知道这一点,但重要的是要认识到线程不是代码元素,而只是一个调度概念。事实上,在现代库中线程由一个对象表示,这只是为了我们的方便。

We are having problems with the boost::shared_mutex in locking critical sections. Is there a better method in this case?

如果没有进一步的细节,实际问题很难说。

一些注意事项:

  1. shared_mutex 是一个读/写互斥锁。旨在允许多个同时读取器,但只允许一个写入器。从代码来看,您似乎只写了 (unique_lock),所以也许您可以使用更简单的互斥锁。
  2. 在我看来,引入互斥锁是为了保护数据片段。并且您必须注意锁定最短的时间,并且仅在真正访问共享数据时,平衡使一组操作原子化的需要。您有一个保护两个 vector 的互斥锁。没关系,但您可能需要考虑一下是否需要这样做。

关于c++ - 多线程——在不同类的方法之间传递变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24216624/

相关文章:

c++ - 在模板类中初始化一个固定长度的数组

c++ - 在此范围内未声明“checkCudaErrors”

c# - 在 C# 中引用主线程

android - 如何使用ffmpeg进行多线程?

c++ - 当子线程崩溃和主线程等待加入时会发生什么?

c++ - boost::asio::ip::tcp::acceptor 如何与 TCP_DEFER_ACCEPT 和 TCP_FASTOPEN 一起工作

c++ - 这个链表是如何流动的?

c++ - 为什么使用更多线程会导致运行时间变慢?

c++ - BOOST ASIO - 如何编写控制台服务器

c++ - 从模板化类版本控制派生类的序列化