【问题标题】:waiting thread/s until onther thread/s ends c++ linux OS等待线程直到另一个线程/ s结束 c++ linux OS
【发布时间】:2014-06-07 09:17:57
【问题描述】:

我的项目中有三个用 c++ 编写的函数,在同一个 .cpp 文件中,我有两个 .h 文件。 现在,我有三个相应的线程,因此,第二个线程从第一个线程获取输出,最后一个线程等待直到第二个线程结束其操作。注意线程处于“永远循环”中,即 while(1 ){....}

//source.cpp 看起来像这样:

#include <iostream>
#include "source.h"
#include "glob_variables.h"
#include "armadillo"
#include <vector>
using namespace arma;
using namespace std;

void source::first_function(int size)
{
  for(int i=0;i<size;i++)
  {
   container.push_back(i);//container is global variable vector of type int declared in glob_variables.h
  }
}
//~~~~~~~~~~~~~~~~~~~~~~
void source::second_function()
{
   //sleep until the first function fills the the vector to set set its size to matrix.n_rows 
  matrix.set_size(container.size(),33);
   for(int i=0;i<container.size();i++)
   {
    for(int j=0;j<50;j++)
     {
       matrix(i,j)=i+j;//matrix is also a global variable in glob_variables.h
     }
   }
}
//~~~~~~~~~~~~~~~~~~~~~~
void source::third_function()
{
//sleep untill the second function fills the matrix, then print
cout<<matrix;//print out the matrix
}

//source.h

#include "glob_variables.h"
#include "armadillo"
#include <vector>
using namespace arma;
using namespace std;
class source
{
public:
 void first_function(int size);
 void second_function();
 void third_function();
};

//glob_variables.h

#include "armadillo"
#include <vector>
using namespace arma;
using namespace std;

extern mat matrix;
extern vector<int> container;

//Main.cpp

    #include <iostream>
    #include <stdio.h>
    #include <pthread.h>
    #include "source.h"
    #include "glob_variables.h"
    #include "armadillo"
    #include <vector>
    using namespace arma;
    using namespace std;

//thread functions
     void* first_reader(void* id1)
     {
       source mysource;
       while(1)
       {
         mysource.first_function(80);
       }
     }

     void* second_reader(void* id2)
     {
       source mysource;
       while(1)
       {
         mysource.second_function();
       }
     }

     void* third_reader(void* id3)
     {
       source mysource;
       while(1)
       {
        mysource.third_function();
       }
     }

   int main()
   {
    pthread_t first;
    pthread_t second;
    pthread_t third;

    int hndl_first;
    int hndl_second;
    int hndl_third;

    hndl_first = pthread_create(&first, NULL, first_reader, NULL);
    hndl_second= pthread_create(&second, NULL, second_thread, NULL);
    hndl_third;= pthread_create(&third, NULL,third_thread, NULL);

   pthread_exit(NULL);
   return 0; 
   }

我可以有任何技术来做到这一点,或者任何简单的例子。 谢谢。

【问题讨论】:

    标签: c++ linux pthreads mutex armadillo


    【解决方案1】:

    来自documentation

    属性

    .n_rows           number of rows; present in Mat, Col, Row, Cube, field and SpMat
    .n_cols           number of columns; present in Mat, Col, Row, Cube, field and SpMat
    .n_elem           total number of elements; present in Mat, Col, Row, Cube, field and SpMat
    .n_slices             number of slices; present in Cube
    .n_nonzero            number of non-zero elements; present in SpMat
    
    Member variables which are read-only; to change the size, use .set_size(), .copy_size(), .zeros(), .ones(), or .reset()
    

    所以,照他们说的做,使用

    matrix.set_size(container.size(),50);
    

    【讨论】:

    • 谢谢,jsantander,现在它可以完美地与 matrix.set_size(container.size(),50);.
    • 嗨@kahsaykalayu,如果这个或任何答案已经解决了您的问题,请点击复选标记考虑accepting it。这向更广泛的社区表明您已经找到了解决方案,并为回答者和您自己提供了一些声誉。没有义务这样做。
    【解决方案2】:

    看起来您声明为 extern 的矩阵的 n_rows 和 n_cols 成员在 arma 命名空间中定义为 constant

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-28
      • 2012-07-13
      • 1970-01-01
      • 2020-09-21
      • 2021-11-03
      相关资源
      最近更新 更多