等待线程/s直到另一个线程/s结束
waiting thread/s until onther thread/s ends c++ linux OS
我在我的项目中有三个函数用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;
}
我可以有任何技巧来做到这一点,或任何简单的例子。谢谢。
从文档
属性.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);
看起来像您声明为的矩阵的n_rows和n_cols成员extern被定义为常量在arma命名空间
相关文章:
- 结束另一个线程中使用的对象的生存期
- 使用 shared_ptr 在中断时结束多线程循环
- C++:我可以在线程仍在运行时为线程提供新值,还是必须先结束它?
- 线程池如何结束程序
- 为什么我的线程永远不会结束
- 当进程结束时,处于Sleep()中间的线程会发生什么
- Linux C++程序以一个仍在运行的线程结束后该怎么办
- 线程未正确结束:它忽略失败的循环条件
- 哪个线程以多线程结束
- C++结束从主线程分离的线程
- 函数执行结束时,向量中的线程会发生什么情况
- 如何在给定的openmp线程结束时退出程序
- 等待线程结束(c++)
- 等待线程/s直到另一个线程/s结束
- QThread::quit()是立即结束线程还是等到返回事件循环
- 线程意外结束.c++
- c++在另一个线程中观察main()的结束
- 分离线程执行结束
- 如果由于接收对象的线程已结束而未处理Qt信号,参数会被删除吗?
- 使用async结束线程并超出其未来的范围是否安全?