c++标准io与线程,互斥锁

封装一个 File 类,

用有私有成员 File* fp

实现以下功能

File f = "文件名" 要求打开该文件

f.write(string str) 要求将str数据写入文件中

string str = f.read(int size) 从文件中读取最多size个字节,

并将读取到的数据返回 析构函数

复制代码
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <sstream>
#include <vector>
#include <memory>

using namespace std;                 

class File{
private:
		FILE* fp;
public:

		File(const char* p);
		void write(const char* str);
		char* read(const int& size);	
		~File()
		{
			fclose(fp);
		}
};




File::File(const char* p)	
{
	fp=fopen(p,"w+");
	cout<<p<<"文件打开成功"<<endl;
}


void File::write(const char* str)
{
	if(fp==NULL)
	{
		cout<<"写入失败"<<endl;
	}
	int len=strlen(str);
	fwrite(str,len,1,fp);
	cout<<"写入成功"<<endl;
}


char* File::read(const int& size)
{
	if(fp==NULL)
	{
		cout<<"读取失败"<<endl;
	}
	char* p=(char*)malloc(size+1);
	fseek(fp,SEEK_SET,0);
	fread(p,size,1,fp);

	return p;
}


int main(int argc,const char** argv){

	File f="test.txt";
	cout<<"请输入文件内容"<<endl;
	char str1[64];
	cin>>str1;
	f.write(str1);

	int size;
	cout<<"请输入读取多少数据"<<endl;
	cin>>size;
	char* p=f.read(size);
	cout<<p<<endl;
	free(p);
	return 0;
}

封装一个 Mutex 互斥锁类 要求:

构造函数:初始化互斥锁,

并选择互斥锁的种类 lock 上锁互斥锁

unlock 解锁互斥锁 析构函数,

销毁互斥锁

并且开启一个线程测试该互斥锁

复制代码
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <sstream>
#include <vector>
#include <memory>
#include <signal.h>

using namespace std;                 

class Mutex{
private:
	pthread_mutex_t m;
public:
	void init();
	void lock();
	void unlock();
	~Mutex();
};


void Mutex::init()
{
	int i=0;
	cout<<"请选择互斥锁种类"<<endl;
	cout<<"1、默认锁"<<endl;
	cout<<"2、递归锁"<<endl;
	cout<<"3、错误检查锁"<<endl;
	cin>>i;
	switch(i)
	{
		case 1:
			{
				pthread_mutex_init(&m,NULL);
				break;
			}
		case 2:
			{
				pthread_mutexattr_t attr;
				pthread_mutexattr_init(&attr);
				pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_RECURSIVE_NP);
				pthread_mutex_init(&m,&attr);
				pthread_mutexattr_destroy(&attr);
				break;
			}
		case 3:
			{
				pthread_mutexattr_t attr;
				pthread_mutexattr_init(&attr);
				pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_ERRORCHECK_NP);
				pthread_mutex_init(&m,&attr);
				pthread_mutexattr_destroy(&attr);
				break;
			}
	}
}


void Mutex::lock()
{
	pthread_mutex_lock(&m);
}


void Mutex::unlock()
{
	pthread_mutex_unlock(&m);
}


Mutex::~Mutex()
{
	pthread_mutex_destroy(&m);
	cout<<"析构成功"<<endl;
}

/*-----------------------------------------------*/

	Mutex mutex1,mutex2;//设置全局的对象
	int flag=1;


void task1()
{
	while(flag)
	{
		mutex1.lock();
		printf("1号在看电视\n");
		sleep(1);
		mutex2.unlock();
	}
}


void task2()
{
	while(flag)
	{
		mutex2.lock();
		printf("2号在打游戏\n");
		sleep(1);
		mutex1.unlock();
	}
}

void* thread(void* arg)
{
	task2();
	return NULL;
}

void handler(int signum)
{
	if(signum==SIGINT)
	{
		flag=0;
	}
}


int main(int argc,const char** argv){

	mutex1.init();
	mutex2.init();
	mutex2.lock();
	
	signal(SIGINT,handler);

	pthread_t id;
	pthread_create(&id,0,thread,0);
	pthread_detach(id);
	task1();
	sleep(1);

	return 0;
}
相关推荐
tangguofeng5 分钟前
不带头结点的链队操作集(不含InitFlag)(C语言版)
算法
weixin_4407305018 分钟前
函数return、参数、参数组、递归函数、高阶函数等小结
开发语言·python
sugarzhangnotes43 分钟前
【无标题】
开发语言·人工智能·python
geovindu1 小时前
rust: Composite Pattern
开发语言·后端·设计模式·rust·组合模式
xailorliu1 小时前
SLIC_超像素算法
c++·opencv·目标检测·机器学习·计算机视觉
qetfw1 小时前
Debian Chrony 时间同步、RAID5 与 LUKS 磁盘加密配置
linux·debian
苏打豆1 小时前
CRISP源码阅读——基于ROS2力矩反馈控制器与速度加速度零空间
c++·线性代数·算法·矩阵·机器人
蛋蛋的就会顺顺的2 小时前
hot100——矩阵
java·数据结构·算法·leetcode·力扣
老歌老听老掉牙2 小时前
麻花钻切屑形态演变的力学机制与临界条件分析
python·算法·钻头
周杰偷奶茶2 小时前
【Java】数组的定义和使用(附管理系统实战案例)
java·开发语言