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;
}
相关推荐
chainbees10 分钟前
Qt 布局管理器的层级关系
开发语言·qt
买了一束花15 分钟前
预分配矩阵内存提升文件数据读取速度
java·人工智能·算法·matlab
南瓜胖胖24 分钟前
【R语言科研绘图】
开发语言·r语言
要加油哦~33 分钟前
刷题 | 牛客 - js中等题-下(更ing)30/54知识点&解答
java·开发语言·javascript
五步晦暝39 分钟前
【Excel 扩展正则的能力】工作中赋予处理单元格文本的强大正则表达提取能力
开发语言·excel
gkdpjj43 分钟前
Linux网络 网络基础一
linux·服务器·开发语言·网络·后端·智能路由器·软件工程
不忘不弃1 小时前
由浮点数的位级表示判断大小关系
c语言·算法
wusam1 小时前
Linux系统管理与编程23:巧用git资源一键部署LAMP
linux·运维·git·shell·lamp
凤年徐1 小时前
【数据结构初阶】顺序表专题
c语言·开发语言·网络·数据结构·c++·经验分享·笔记
梁下轻语的秋缘1 小时前
每日c/c++题 备战蓝桥杯(修理牛棚 Barn Repair)
c语言·c++·蓝桥杯