线程处理c++ linux操作系统
thread handling c++ linux OS
在我的项目中有三个用c++编写的函数。它们在同一个。cpp文件中,我有两个头文件。
现在,我有三个对应的线程,因此,第二个线程从第一个线程获取输出,最后一个线程等待直到第二个线程结束其操作。请注意,线程在"永远循环"中,即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;
}
我可以有任何技巧来做到这一点,或任何简单的例子。谢谢。
尝试使用以下链接。AutoResetEvent可以满足您的需求。链接中的代码正是您所期望的。
Linux下AutoResetEvent的c++等效是什么?
我认为你想按顺序播放线程。在第一个线程做某事->在第二个线程做某事->在第三个线程做某事
正确吗?在这种情况下,您可以使用signal.
的例子:
pthread_cond_t cond;
pthread_mutex_t mtx;
typedef enum {
ready1,
ready2,
ready3
} thread_ready;
thread_ready t_ready;
void* first_reader(void* id1)
{
//source mysource;
while(1)
{
pthread_mutex_lock(&mtx);
while(t_ready != ready1)
pthread_cond_wait(&cond, &mtx);
pthread_mutex_unlock(&mtx);
std::cout << "first" << std::endl;
//mysource.first_function(80);
pthread_mutex_lock(&mtx);
t_ready = ready2;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mtx);
sleep(1);
}
}
void* second_reader(void* id2)
{
//source mysource;
while(1)
{
pthread_mutex_lock(&mtx);
while(t_ready != ready2)
pthread_cond_wait(&cond, &mtx);
pthread_mutex_unlock(&mtx);
std::cout << "second" << std::endl;
//mysource.second_function();
pthread_mutex_lock(&mtx);
t_ready = ready3;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mtx);
sleep(1);
}
}
void* third_reader(void* id3)
{
//source mysource;
while(1)
{
pthread_mutex_lock(&mtx);
while(t_ready != ready3)
pthread_cond_wait(&cond, &mtx);
pthread_mutex_unlock(&mtx);
std::cout << "third" << std::endl;
//mysource.third_function();
pthread_mutex_lock(&mtx);
t_ready = ready1;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mtx);
sleep(1);
}
}
int main()
{
pthread_t first;
pthread_t second;
pthread_t third;
int hndl_first;
int hndl_second;
int hndl_third;
pthread_cond_init(&cond,NULL);
pthread_mutex_init(&mtx,NULL);
t_ready = ready1;
hndl_first = pthread_create(&first, NULL, first_reader, NULL);
hndl_second = pthread_create(&second, NULL, second_reader, NULL);
hndl_third = pthread_create(&third, NULL,third_reader, NULL);
pthread_join(first, NULL);
pthread_join(second, NULL);
pthread_join(third, NULL);
return 0;
}
实际上这种情况下不需要做3个线程。如果你想做我的代码,你最好只做一个线程。
相关文章:
- 从utsname找出Linux操作系统名称
- 如何应用 printers.py 修改?(Linux操作系统)
- LINUX 操作系统上的错误:令牌之前预期的构造函数、析构函数或类型转换'('?
- 操作系统 (Linux) 在使用 boost::asio::generic::raw_protocol::socket
- 如何从支持linux/mac操作系统的第三方代码为我的c#项目创建dll
- 如何在Linux / Windows操作系统上使用文件*.cpp一步编译下面的代码
- 让可执行文件在多个 Linux 操作系统上运行:
- 非常量引用类型的无效初始化中出错.在Linux操作系统中使用c++代码.GCC编译器
- 操作系统Windows和Linux的结果不同
- 仅在Linux操作系统上使用双指针后的Seg错误
- 如何以编程方式阻止 Linux 操作系统用户空间中的 USB 存储设备
- 代码在Linux上运行良好,但在Windows操作系统上给出不同的输出
- 如何在linux操作系统上打开com端口(rs422)
- Linux中的SEH等效或如何处理操作系统信号(如SIGSERV)并保持继续
- Minix vs Linux学习操作系统设计
- 在Linux操作系统的后台按下Get键
- 线程处理c++ linux操作系统
- 如何在Linux/Windows机器的Android操作系统上运行c++程序
- 分析底层Linux操作系统函数/代码,如select()和poll()
- C/C++裸机编译和针对特定操作系统(Linux)的编译之间有什么区别