【PTHREAD】linux 多线程编程---Mutex实现Service线程和work线程

本文介绍了一个使用C++实现的多线程程序案例,其中包含一个主线程负责向链表添加数据,两个子线程从链表中取出并删除数据。程序展示了线程同步机制,包括互斥锁的使用。

程序功能:

主线程向list<int>添加数据,两个子线程冲list<int>中取数据并删除该数据。主线程类似于Service线程,子线程类是于work线程。

Code

#include<iostream>
#include<pthread.h>
#include<list>
#include<unistd.h>
#include<stdlib.h>
#include<time.h>
using namespace std;

pthread_mutex_t vc_mutex;

list<int>* pgusts = NULL;
int gust_max_indx = 0;

int get_rand_num()
{
#define MAX_SLEEP_TIME 30
	time_t tsr;
	srand(time(&tsr));
	int rnum = rand()%MAX_SLEEP_TIME;
	return rnum;
}

void * handle_hair(void *arg)
{

	pthread_t pth_id = pthread_self();

   cout<<"child pid<handle_hair>:\t"<<(unsigned int)pth_id<<endl;
	while(1)
	{
		pthread_mutex_lock(&vc_mutex);
		if(NULL != pgusts && !pgusts->empty())
		{
			int igust = pgusts->front();
			pgusts->pop_front();
			cout<<"pthread "<<(unsigned int)pth_id<<" handle"<<igust<<endl;
		}
		pthread_mutex_unlock(&vc_mutex);
		sleep(get_rand_num());
	}
}
void * add_gust(void *arg)
{
  pthread_t pth_id = pthread_self();
  while(1)
  {
	  sleep(get_rand_num()+10);
	  int gust_num = get_rand_num()+1;

	  int i = 0;
	  while(i<gust_num)
	  {
		  pgusts->push_back(i+gust_max_indx+1);
		  ++i;
	  }
	  gust_max_indx+=gust_num;
  }
}

int main(void)
{
	int err;
	if(0 != pthread_mutex_init(&vc_mutex,NULL))
	{
		cerr<<"[error]initialize mutex fail"<<endl;
		return 1;
	}
	pgusts = new list<int>;

	if ( NULL == pgusts )
	{
		cerr<<"[error]malloc list<int> fail"<<endl;
		return 1;
	}
	pthread_t pth_id = pthread_self();

   cout<<"main pid:\t"<<(unsigned int)pth_id<<endl;

	err = pthread_create(&pth_id,NULL,handle_hair,NULL);
	if(0 != err )
	{
		cerr<<"[error]pthread_create fail"<<endl;
		return 1;
	}
   cout<<"child pid:\t"<<(unsigned int)pth_id<<endl;

	err = pthread_create(&pth_id,NULL,handle_hair,NULL);
	if(0 != err )
	{
		cerr<<"[error]pthread_create fail"<<endl;
		return 1;
	}
    cout<<"child pid:\t"<<(unsigned int)pth_id<<endl;

    add_gust(NULL);
	return 0;
}

编译Code:

g++ -g -Wall -o barber_out  barber.c  -lpthread


注意这里必须要添加-lpthread选项,因为pthread不是linux活在标准的c/c++库


运行结果:

main pid:	267999040
child pid:	251287296
child pid:	242894592
child pid<handle_hair>:	251287296
child pid<handle_hair>:	242894592
pthread 251287296 handle1
pthread 242894592 handle2
pthread 251287296 handle3
pthread 242894592 handle4
pthread 251287296 handle5
pthread 242894592 handle6
pthread 251287296 handle7
pthread 242894592 handle8
pthread 251287296 handle9
pthread 242894592 handle10
pthread 251287296 handle11
pthread 242894592 handle12
pthread 251287296 handle13
pthread 242894592 handle14
pthread 242894592 handle15
pthread 251287296 handle16
pthread 251287296 handle17
pthread 251287296 handle18
pthread 242894592 handle19
pthread 242894592 handle20
pthread 242894592 handle21
pthread 242894592 handle22
pthread 242894592 handle23
pthread 242894592 handle24
pthread 242894592 handle25
pthread 242894592 handle26
pthread 242894592 handle27
pthread 242894592 handle28
pthread 242894592 handle29
pthread 242894592 handle30
pthread 242894592 handle31
pthread 242894592 handle32
pthread 242894592 handle33
pthread 242894592 handle34
pthread 242894592 handle35
pthread 242894592 handle36
pthread 242894592 handle37
pthread 242894592 handle38
pthread 242894592 handle39
pthread 242894592 handle40
pthread 242894592 handle41
pthread 242894592 handle42
pthread 242894592 handle43
pthread 242894592 handle44
pthread 242894592 handle45
pthread 242894592 handle46
pthread 242894592 handle47
pthread 242894592 handle48
pthread 242894592 handle49
pthread 242894592 handle50
pthread 242894592 handle51
pthread 242894592 handle52
pthread 242894592 handle53
pthread 242894592 handle54
pthread 242894592 handle55
pthread 242894592 handle56
pthread 242894592 handle57
pthread 242894592 handle58
pthread 242894592 handle59
pthread 242894592 handle60
pthread 242894592 handle61
pthread 242894592 handle62
pthread 242894592 handle63
pthread 242894592 handle64
pthread 242894592 handle65
pthread 242894592 handle66
pthread 242894592 handle67
pthread 242894592 handle68
pthread 242894592 handle69
pthread 242894592 handle70
pthread 251287296 handle71
pthread 242894592 handle72
pthread 251287296 handle73
pthread 242894592 handle74
pthread 251287296 handle75
pthread 242894592 handle76
pthread 251287296 handle77
pthread 242894592 handle78
pthread 251287296 handle79
pthread 251287296 handle80
pthread 251287296 handle81
pthread 251287296 handle82
pthread 251287296 handle83
pthread 251287296 handle84
pthread 251287296 handle85
pthread 251287296 handle86
pthread 251287296 handle87
pthread 251287296 handle88
pthread 251287296 handle89
pthread 251287296 handle90
pthread 251287296 handle91
pthread 251287296 handle92
pthread 251287296 handle93
pthread 251287296 handle94
pthread 251287296 handle95
pthread 251287296 handle96
pthread 251287296 handle97
pthread 251287296 handle98
pthread 251287296 handle99
pthread 251287296 handle100
pthread 251287296 handle101
pthread 251287296 handle102
pthread 251287296 handle103
pthread 251287296 handle104
pthread 251287296 handle105
pthread 251287296 handle106
pthread 251287296 handle107
pthread 251287296 handle108
pthread 251287296 handle109
pthread 251287296 handle110
pthread 251287296 handle111
pthread 251287296 handle112
pthread 251287296 handle113
pthread 251287296 handle114
pthread 251287296 handle115
pthread 251287296 handle116
pthread 251287296 handle117
pthread 251287296 handle118
pthread 251287296 handle119
pthread 251287296 handle120
pthread 251287296 handle121
pthread 251287296 handle122
pthread 251287296 handle123
pthread 242894592 handle124
pthread 251287296 handle125
pthread 242894592 handle126
pthread 251287296 handle127

查看线程运行情况:

ialan@Apprentice>thread$ top -c -H -p 3901

top - 18:01:02 up  2:06,  3 users,  load average: 0.74, 0.68, 0.73
top - 18:01:15 up  2:06,  3 users,  load average: 0.86, 0.72, 0.74
Tasks:   3 total,   0 running,   3 sleeping,   0 stopped,   0 zombie
Cpu(s): 22.0%us,  4.3%sy,  0.0%ni, 72.1%id,  0.8%wa,  0.0%hi,  0.7%si,  0.0%st
Mem:   3903276k total,  2261200k used,  1642076k free,    64988k buffers
Swap:  7999484k total,        0k used,  7999484k free,   878068k cached


  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                                                        
 3901 ialan     20   0 31124 1104  928 S    0  0.0   0:00.00 ./barber_out                                                   
 3902 ialan     20   0 31124 1104  928 S    0  0.0   0:00.00 ./barber_out                                                   
 3903 ialan     20   0 31124 1104  928 S    0  0.0   0:00.00 ./barber_out




基于数据驱动的 Koopman 算子的递归神经网络模型线性化,用于纳米定位系统的预测控制研究(Matlab代码实现)内容概要:本文围绕“基于数据驱动的Koopman算子的递归神经网络模型线性化”展开,旨在研究纳米定位系统的预测控制问题,并提供完整的Matlab代码实现。文章结合数据驱动方法与Koopman算子理论,利用递归神经网络(RNN)对非线性系统进行建模与线性化处理,从而提升纳米级定位系统的精度与动态响应性能。该方法通过提取系统隐含动态特征,构建近似线性模型,便于后续模型预测控制(MPC)的设计与优化,适用于高精度自动化控制场景。文中还展示了相关实验验证与仿真结果,证明了该方法的有效性先进性。; 适合人群:具备一定控制理论基础Matlab编程能力,从事精密控制、智能制造、自动化或相关领域研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①应用于纳米级精密定位系统(如原子力显微镜、半导体制造设备)中的高性能控制设计;②为非线性系统建模与线性化提供一种结合深度学习与现代控制理论的新思路;③帮助读者掌握Koopman算子、RNN建模与模型预测控制的综合应用。; 阅读建议:建议读者结合提供的Matlab代码逐段理解算法实现流程,重点关注数据预处理、RNN结构设计、Koopman观测矩阵构建及MPC控制器集成等关键环节,并可通过更换实际系统数据进行迁移验证,深化对方法泛化能力的理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值