C++多线程编程基础

joinable

判断线程是否还活着
在这里插入图片描述

join

阻塞当前进程,直到所标识的线程结束(是否线程资源)
在这里插入图片描述

detach

将线程和主线程分离,但是当主线程结束,分离的线程也会结束
在这里插入图片描述
C++中线程的结束,不是指线程函数运行完成,而是指线程对应的资源被释放,才表明线程结束
线程控制块和某一个线程函数关联,join的目的就是使其分开

void fun()
{
	//std::this_thread::sleep_for(std::chrono::milliseconds(100));
	cout << "fun" << endl;
	std::this_thread::sleep_for(std::chrono::milliseconds(300));
	cout << "end fun" << endl;
}

int main()
{
	thread tha(fun);

	cout << "main begin" << endl;
	cout << tha.joinable() << endl;

	cout << tha.get_id() << endl;
	tha.detach();
	cout << tha.get_id() << endl;
	while (tha.joinable())
	{
		cout << "thread able " << endl;
	}
	std::this_thread::sleep_for(std::chrono::milliseconds(100));
	cout << tha.joinable() << endl;
	cout << "main end " << endl;
	return 0;
}

在这里插入图片描述
因此get_id()==0 ; joinable()检测不到线程,因此返回值为false ;
当主函数运行完,线程函数也会被终止
在这里插入图片描述

std::move()

线程间可以出现资源的转移,std::move();将a线程的资源转移给b线程,则a线程会死亡

class Test
{
private:
	int value;
	Test& operator=(const Test&);
	Test(const Test&);
public:
	Test(int x) :value(x) {}

};
void funa()
{
	cout << "funa" << endl;
}
void funb()
{
	cout << "funb" << endl;
}
int main()
{
	thread tha(funa);
	thread thb(std::move(tha));
	thread thc;

	//tha.swap(thb);
	thc = std::move(thb);
	thb.join();
	return 0;
}

在这里插入图片描述

C++线程函数可以传递多个参数

void fun(int* p, int n)
{
	for (int i = 0; i < n; ++i)
	{
		cout << p[i] << endl;
	}
	return;
}

int main()
{
	const int n = 10;
	int ar[n] = { 12,23,34,45,56,67,78,89,90,100 };
	thread tha(fun, ar, n);
	tha.join();
	cout << endl;
}

得到线程函数返回值

C++线程函数不能得到其返回值 ,想要得到其返回值只能通过参数传入指针,或者引用将其得到

void add(int a, int b, int* p)
{
	*p = a + b;
}
int  Add(int a, int b, int& c)
{
	c = a + b;
	return c;
}
int main()
{
	int x = 10, y = 20;
	int z = 0;
	unsigned int n = std::thread::hardware_concurrency();
	cout << n << endl;
	thread tha(Add, x, y, std::ref(z));

	tha.join();


	cout << z << endl;
	return 0;
}
int num = 0;
std::mutex g_mtx;
void funa()
{
	for (int i = 0; i < 10; ++i)
	{
		srand(time(NULL));
		std::this_thread::sleep_for(std::chrono::milliseconds(rand() % 100));

		std::unique_lock<std::mutex> ulokc(g_mtx);
		//	g_mtx.lock();
		cout << " funa: " << (num += 2) << endl;
		//g_mtx.unlock();
	}
}
void funb()
{
	for (int i = 0; i < 10; ++i)
	{
		srand(time(NULL));
		std::this_thread::sleep_for(std::chrono::milliseconds(rand() % 100));
		std::unique_lock<std::mutex> ulokc(g_mtx);
		//g_mtx.lock();
		cout << "funb: " << (num += 100) << endl;
		//g_mtx.unlock();
	}
}

int main()
{
	thread tha(funa);
	thread thb(funb);


	tha.join();
	thb.join();
	cout << "main end" << endl;
}

lock 和 unique_lock

互斥锁只有构造函数和析构函数
加锁相当于构造
去锁相当于析构

std::mutex g_mtx;

唯一性锁:类似于智能指针

		std::unique_lock<std::mutex> ulokc(g_mtx);
		cout << " funa: " << (num += 2) << endl;
		

等价于

		g_mtx.lock();
		cout << " funa: " << (num += 2) << endl;
		g_mtx.unlock();
int num = 0;
std::mutex g_mtx;
void funa()
{
	for (int i = 0; i < 10; ++i)
	{
		srand(time(NULL));
		std::this_thread::sleep_for(std::chrono::milliseconds(rand() % 100));

		std::unique_lock<std::mutex> ulokc(g_mtx);
		//	g_mtx.lock();
		cout << " funa: " << (num += 2) << endl;
		//g_mtx.unlock();
	}
}
void funb()
{
	for (int i = 0; i < 10; ++i)
	{
		srand(time(NULL));
		std::this_thread::sleep_for(std::chrono::milliseconds(rand() % 100));
		std::unique_lock<std::mutex> ulokc(g_mtx);
		//g_mtx.lock();
		cout << "funb: " << (num += 100) << endl;
		//g_mtx.unlock();
	}
}

int main()
{
	thread tha(funa);
	thread thb(funb);


	tha.join();
	thb.join();
	cout << "main end" << endl;
}

条件变量

条件变量有一个等待队列
使用条件变量一定伴随着互斥锁的使用

在这里插入图片描述

wait会释放锁,notify仅仅只是通知,不释放锁

wait是指在一个已经进入了同步锁的线程内,让自己暂时让出同步锁,以便其他正在等待此锁的线程可以得到同步锁并运行,只有其他线程调用了notify方法(notify并不释放锁,只是告诉调用过wait方法的线程可以去参与获得锁的竞争了,但不是马上得到锁,因为锁还在别人手里,别人还没释放),调用wait方法的一个或多个线程就会解除wait状态,重新参与竞争对象锁,程序如果可以再次得到锁,就可以继续向下运行。

1)wait()、notify()和notifyAll()方法是本地方法,并且为final方法,无法被重写。
2)当前线程必须拥有此对象的monitor(即锁),才能调用某个对象的wait()方法能让当前线程阻塞。(这种阻塞是通过提前释放synchronized锁,重新去请求锁导致的阻塞,这种请求必须有其他线程通过notify()或者notifyAll()唤醒重新竞争获得锁)
3)调用某个对象的notify()方法能够唤醒一个正在等待这个对象的monitor的线程,如果有多个线程都在等待这个对象的monitor,则只能唤醒其中一个线程; (notify()或者notifyAll()方法并不是真正释放锁,必须等到synchronized方法或者语法块执行完才真正释放锁)
 4)调用notifyAll()方法能够唤醒所有正在等待这个对象的monitor的线程,唤醒的线程获得锁的概率是随机的,取决于cpu调度

例子1(错误使用导致线程阻塞):三个线程,线程3先拥有sum对象的锁,然后通过sum.notify()方法通知等待sum锁的线程去获得锁,但是这个时候线程1,2并没有处于wait()导致的阻塞状态,而是在synchronized方法块处阻塞了,所以,这次notify()根本没有通知到线程1,2。然后线程3正常结束,释放掉sum锁,这个时候,线程1就立刻获得了sum对象的锁(通过synchronized获得),然后调用sum.wait()方法释放掉sum的锁,线程2随后获得了sum对象的线程锁(通过synchronized获得),这个时候线程1,2都处于阻塞状态,但是悲催的是,这之后再也没有线程主动调用sum.notify()或者notifyAll()方法显示唤醒这两个线程,所以程序阻塞

在这里插入图片描述

无论怎样都要先打印奇数再打印偶数

std::mutex data_mutex;
std::condition_variable data_var;
bool tag = true;
// odd : 1 
// even: 2
// odd : 3
// even : 4

void printodd() // 打印奇数
{
	std::unique_lock<std::mutex> ulock(data_mutex);
	for (int odd = 1; odd <= 100; odd += 2)
	{
		while (!tag)
		{
			data_var.wait(ulock);
		}
		cout << "odd: " << odd << endl;
		tag = false;
		data_var.notify_all();
	}
}
void printeven() //打印偶数
{
	std::unique_lock<std::mutex> ulock(data_mutex);

	for (int even = 2; even <= 100; even += 2)
	{
		while (tag)
		{
			data_var.wait(ulock);//  1 2 
		}
		cout << "even: " << even << endl;
		tag = true;
		data_var.notify_all();
	}
}
int main()
{
	thread thodd(printodd);
	thread theven(printeven);

	thodd.join();
	theven.join();
	return 0;
}

在这里插入图片描述

在这里插入图片描述
奇数线程先得到锁

在这里插入图片描述
偶数线程先得到锁

在这里插入图片描述

notify之后并不会立刻释放锁,详情见

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值