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;
}
相关推荐
独自破碎E3 分钟前
BISHI54货物堆放
android·java·开发语言
mit6.82443 分钟前
二分+贪心
算法
『往事』&白驹过隙;1 小时前
浅谈PC开发中的设计模式搬迁到ARM开发
linux·c语言·arm开发·设计模式·iot
顾北121 小时前
SpringCloud 系列 04:Gateway 断言 / 过滤器 / 限流 一站式落地指南
java·开发语言·数据库
programhelp_1 小时前
特斯拉 MLE 超详细面经 + 避坑
数据结构·人工智能·算法·面试·职场和发展
wuqingshun3141592 小时前
java创建对象的方式
java·开发语言
二十雨辰2 小时前
[英语]-介词和动词
开发语言
程序员敲代码吗2 小时前
提升Python编程效率的五大特性
开发语言·python
越甲八千2 小时前
深入了解迭代器erase()之后的失效逻辑
算法
Kurbaneli2 小时前
C语言过时了吗?2025年仍不可替代
c语言·开发语言