多线程

#include <iostream>
#include <thread>
#include <vector>
#include <memory>
#include <chrono>
#include <mutex> //独占互斥量 
using namespace std;
void say_hello()
{
	cout << "Hi!\n";
}

void say_hello1()
{
	_sleep(3000);
	cout << "hello\n";
}

void ca_time(int *arr,int &len_)
{
	cout << "You only got five seconds to type the number 1~5,,ready go,,\n";
	_sleep(5000);
	cout << "Sorry time out!!\n";
	cout << "Ok, here is your greads:\n";
	for(int i = 0; i < len_; i ++)
	{
		cout << arr[i] << " ";
	}
	cout << endl;
	cout <<"-->" << len_ << endl;
	return;
}
void fun_type(int *arr,int &len_)
{
	memset(arr,'\0',sizeof(arr));
	for(; len_ < 5; len_ ++)
	{
		cin >> arr[len_];
	}
}

//将线程放在容器里面
vector<thread> g_list;
vector<shared_ptr<thread>> g_list2; //智能指针
void CreateThread()
{
	thread t(say_hello);
	g_list.push_back(move(t));
	g_list2.push_back(make_shared<thread>(say_hello)); //make_shared作用和new差不多(速度比new快)
}

//获得线程得ID
thread::id GetThreadId(thread& t)
{
	return t.get_id();
}

//获得CPU的核心数(返回0获取失败)
int GetCPU_concurrency()
{
	return thread::hardware_concurrency();
}
void ThreadSleep()
{
	this_thread::sleep_for(chrono::seconds(6));
}
int main()
{
	非阻塞线程
	//thread t1(say_hello);
	//t1.detach();
	阻塞线程
	//thread t2([](void){cout << "hello,阻塞\n";}); //lambda表达式

	//非阻塞的线程
	//thread t3(say_hello1);
	//t3.detach();
	//阻塞的线程
	//thread t5(say_hello);
	//t5.join();

	//线程t5 会先于t3执行完,因为t3是非阻塞的 如果t3 换成t3.join()的话就会等待t3 返回以后t5才会执行(子线程与子线程之间没有等待关系)
	//thread t4(move(t3));
	//t4.join();

	//计时输入词语:
	/*int arr[100];
	int len = 0;
	thread time(ca_time,arr,ref(len));
	time.detach();
	fun_type(arr,len);*/

	将线程保存于容器中
	//CreateThread();
	//for(auto& thread : g_list)
	//{
	//	thread.join();
	//}
	//for(auto& thread : g_list2)
	//{
	//	thread->join();
	//}

	获取线程信息(线程ID,CPU核心数)
	//cout << "你的CPU核心数为:" << GetCPU_concurrency() << endl;
	//thread t1(say_hello);
	//thread t2(say_hello1);
	//thread t3(say_hello);
	//thread t4_sleep(ThreadSleep);
	//t1.join();
	//t2.join();
	//t3.join();
	//t4_sleep.join();
	//cout << "他们的线程id分别为:" << GetThreadId(t1)<< " "<< GetThreadId(t2)<< " " << GetThreadId(t3) << endl;
return 0;
}



//互斥量:

--保护多线程同时访问的共享数据

1)普通的互斥量

#include <iostream>
#include <thread>
#include <mutex>
int a = 0;
int b = 0;
std::mutex lock;
void fun1()
{
std::lock_guard<std::mutex>locker(lock); //上锁,只能允许使用此函数的进程访问共享变量
a = 1;
std::cout <<"fun1 a: " << a << std::endl;
b = 1;
std::cout <<"fun1 b: " << b << std::endl;


std::cout <<"fun1 a: " << a << std::endl;
std::cout <<"fun1 b: " << b << std::endl;
}
void fun2()
{
std::lock_guard<std::mutex>locker(lock);//同上
a = 5;
std::cout <<"fun2 a: " << a << std::endl;
b = 5;
std::cout <<"fun2 b: " << b << std::endl;


std::cout <<"fun2 a: " << a << std::endl;
std::cout <<"fun2 b: " << b << std::endl;
}
int main()
{
std::thread t1(fun1);
std::thread t2(fun2);
t1.join();
t2.join();
system("pause");
return 1;
}


//不使用上面两句std::lock_guard<std::mutex>locker(lock)的结果 如图(出现混乱)


2)递归互斥量

//死锁示例(普通不支持递归互斥量)
struct Complex
{
	mutex mutex;
	int i;
	Complex():i(1) {}
	void mul(int x)
	{
		lock_guard<std::mutex>lock(mutex);
		i *= x;
		cout << "--" << i <<endl;
	}
	void div(int x)
	{
		lock_guard<std::mutex>lock(mutex);
		i /= x;
		cout << "==" << i <<endl;
	}
	void both(int x, int y)
	{
		lock_guard<std::mutex>lock(mutex);
		mul(x);
		div(y);
	}
};


//用递归锁改进上面的死锁
struct Complex_recursion
{
<span style="white-space:pre">	</span>std::recursive_mutex mutex;
<span style="white-space:pre">	</span>int i;
<span style="white-space:pre">	</span>Complex_recursion():i(1){}
<span style="white-space:pre">	</span>void mul(int x)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>lock_guard<std::recursive_mutex>lock(mutex);
<span style="white-space:pre">		</span>i *= x;
<span style="white-space:pre">		</span>cout << "--" << i <<endl;
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>void div(int x)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>std::lock_guard<std::recursive_mutex>lock(mutex);
<span style="white-space:pre">		</span>i /= x;
<span style="white-space:pre">		</span>cout << "==" << i <<endl;
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>void both(int x, int y)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>std::lock_guard<std::recursive_mutex>lock(mutex);
<span style="white-space:pre">		</span>mul(x);
<span style="white-space:pre">		</span>div(y);
<span style="white-space:pre">	</span>}
};<pre name="code" class="cpp">void main()
{
//死锁示范:
//Complex complex;
//complex.both(32,23);
//递归互斥量改进死锁
//Complex_recursion complex_re;
//complex_re.both(32,23);

}

//带超时的互斥量tsd::timed_mutex

#include <iostream>
#include <thread>
#include <mutex>
#include <ctime>
//带超时的锁
//std::timed_mutex //带超时
//std::recursive_timed_mutex //带超时+递归

std::timed_mutex timedMutex;
void get_mutex()
{
	std::chrono::seconds times(8); //设置时间 chrono boost的时间库 现已加入c++11豪华标准套餐
	//timedMutex.try_lock(); //如果已获取锁,则为值 true;否则为值 false。
	while(!timedMutex.try_lock_for(times))
	{
		std::cout << "001 Wait for get...\n";
	}
	//	timedMutex.try_lock_until;
	std::cout << "001 function get_mutex have got it!\n";
	std::this_thread::sleep_for(std::chrono::seconds(8));
	timedMutex.unlock();
}
void try_to_get_mutex()
{
	std::chrono::seconds times(1);
	while(!timedMutex.try_lock_for(times))
	{
		std::cout << "002 Wait for get...\n";
	}
	std::cout << "002 function try_to_get_mutex have got it!\n";
	timedMutex.unlock();
}

void try_to_get_mutex1()
{
	std::chrono::seconds times(1);
	while(!timedMutex.try_lock_for(times))
	{
		std::cout << "003 Wait for get...\n";
	} 
	std::cout << "003 function try_to_get_mutex1 have got it!\n";
	timedMutex.unlock();
}
int mian()
{
//std::thread t1(get_mutex);
	//std::thread t2(try_to_get_mutex);
	//t1.join();
	//t2.join();
//通过运行可以发现当线程t1获得互斥量以后,线程t2经过8次循环获得了互斥量,因为线程t1休眠8秒,线程t2用try_lock_for(1秒)每次获取时常1秒
}


 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值