c++11 std::thread和mutex用法

thread和mutex用法

本文对c++ 11中的std::thread 和 mutex作简要的使用说明

thread简单示例

#include <iostream>
#include <string>
#include <thread>
using namespace std;


void thread_hello_wolrd()
{
	cout << "hello world!" << endl;
}

void thread_any_word(string&str)
{
	cout << str << endl;
}
int main()
{
	thread t_hello(thread_hello_wolrd);
	t_hello.join(); //等待t_hello子线程结束,才继续下面的操作

	string str = "hello my world!";
	thread t_any_word(thread_any_word, std::ref(str));
	t_any_word.detach(); //t_any_world子线程与主线程分离,即使主线程结束,也会运行

	return 0;
}

thread构造函数梳理

thread() _NOEXCEPT:默认构造函数,创建的是空thread对象
explicit thread(_Fn&& _Fx, _Args&&… _Ax):可变参数的构造函数,该线程是可jointable的
thread(const thread&) = delete;拷贝构造函数不可用
thread(thread&& _Other) _NOEXCEPT:移动构造函数,使用之后,other线程为空,移交给当前thread

#include <iostream>
#include <thread>
#include <chrono>

using namespace std;

void fun1(int n)
{
	for (int i = 0; i < 10; i++)
	{
		cout << "fun1:" << n << endl;
		this_thread::sleep_for(chrono::microseconds(10));
	}
}

void fun2(int& n)
{
	for (int i = 0; i < 10; i++)
	{
		cout << "fun2:" << n << endl;
		++n;
		this_thread::sleep_for(chrono::microseconds(10));
	}
}

int main()
{
	int n = 0; 
	thread t1;
	thread t2(fun1, n);
	thread t3(fun2, std::ref(n));
	thread t4(move(t3));  //t3线程为空
	t2.join();
	t4.join();
	cout << "result n =" << n << endl;

	return 0;
}

在这里插入图片描述

thread关键成员函数

  • get_id():获取线程id
  • joinable(): 判断线程是否可joinable()
    joinable()函数是一个布尔类型的函数,他会返回一个布尔值来表示当前的线程是否是可执行线程(能被join或者detach),因为相同的线程不能join两次,也不能join完再detach,同理也不能detach,所以joinable函数就是用来判断当前这个线程是否可以joinable的。通常不能被joinable有以下几种情况:
    1)由thread的缺省构造函数而造成的(thread()没有参数:
    2)该thread被move过(包括move构造和move赋值):
    3)该线程被join或者detach过。
  • detach(): 将当前线程脱离主线程,线程可以单独执行
  • swap(): 交换两线程的句柄
  • std::this_thread::getid():获取当前线程id
  • std::this_thread::yield(): 当前线程放弃执行,调度另一线程继续执行
  • sleep_until(): 线程休眠至某时刻被唤醒
  • sleep_for(): 线程休眠时间片后被唤醒

mutex使用

多个线程访问共享资源时,需要加锁互斥访问

  • 独占式互斥量:std::mutex独占工互斥量加解锁是成对的,同一个线程内独占式互斥量在没有解锁的情况下,再次对其加锁是不正确的。
  • 递归式互斥量:std::recursive_mutex 递归式互斥量是在同一个线程内互斥量没有解锁的情况下可以再次对其加锁,但其加解锁的次数需要保持一致。
  • 允许超时的独占式互斥量: std::time_mutex
  • 允许超时的递归式互斥量: std::recursive_time_mutex
#include<iostream>
#include <thread>
#include <mutex>

using namespace std;

int total_num = 0;
std::mutex mtx;

void fun1()
{
	
	for (int i = 0; i < 500; i++)
	{ 
		mtx.lock();                      //对异常抛出的情况,会发生死锁
		cout << "fun1:" << total_num++ << endl;
		mtx.unlock();
		this_thread::sleep_for(chrono::microseconds(10));
	}
	
}

void fun2()
{
	
	for (int i = 0; i < 500; i++)
	{
		{
			std::lock_guard<mutex>gd(mtx); //可以防止死锁,更安全,作用域有效
			cout << "fun2:" << total_num++ << endl;
		}
		this_thread::sleep_for(chrono::microseconds(10));
	}
	
}

int main()
{
	std::thread t1(fun1);
	std::thread t2(fun2);
	t1.join();
	t2.join();
	return 0;
}

若线程1对共享资源的访问时间过长,而线程2又不想等那么久,可以设定一个超时时间 ,在超时时间内若线程1中的互斥量还没有解锁,线程2就继续向下执行,这就是允许超时的互斥量。lock_guard只是提供了对互斥量最基本的加解锁封装,而unique_lock提供了多种构造方法,使用起来更加灵活,对于允许超时的互斥量需要使用unnique_lock来包装。

#include <iostream>
#include <thread>
#include <mutex>
#include <Windows.h>

using namespace std;

timed_mutex mtx;
void fun1()
{
    unique_lock<timed_mutex> t1(mtx);
    Sleep(2000); //模拟其他事项占用了大量时间
    cout << "fun1" << endl;
}

void fun2()
{
    unique_lock<timed_mutex> t1(mtx, chrono::milliseconds(1000));//设置超时时间为1秒
    cout << "fun2" << endl;
}

int main(int argc, char* argv[])
{
    thread t1(fun1);
    Sleep(100);   //让线程1先启动
    thread t2(fun2);
    t1.join();
    t2.join();

    return 0;
} 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++中,std::thread是用于创建和管理线程的类。以下是std::thread的基本用法: 1. 包含头文件:为了使用std::thread,需要包含头文件<thread>。 2. 创建线程:使用std::thread的构造函数创建线程对象,并将函数指针或可调用对象作为参数传递给构造函数。例如: ```cpp void myFunction() { // 线程执行的代码 } std::thread myThread(myFunction); // 创建一个新线程,执行myFunction ``` 3. 启动线程:通过调用线程对象的成员函数`std::thread::join()`来启动线程。例如: ```cpp myThread.join(); // 启动线程执行 ``` 4. 传递参数:如果需要向线程传递参数,可以在创建线程时将参数作为额外的参数传递给构造函数。例如: ```cpp void myFunction(int arg1, std::string arg2) { // 使用传递的参数执行操作 } std::thread myThread(myFunction, 42, "Hello"); // 创建线程并传递参数 ``` 5. 分离线程:通过调用线程对象的成员函数`std::thread::detach()`可以将线程分离,使其在后台运行而不需要等待。例如: ```cpp myThread.detach(); // 分离线程,使其在后台运行 ``` 6. 获取线程ID:可以通过`std::thread::get_id()`成员函数获取线程的唯一标识符。例如: ```cpp std::thread::id threadId = myThread.get_id(); // 获取线程的ID ``` 7. 线程同步:在线程间进行同步操作时,可以使用互斥锁(std::mutex)和条件变量(std::condition_variable)等同步原语来确保线程安全。 注意事项: 线程的生命周期应与其所执行的函数的生命周期相匹配,以避免悬挂引用或访问已销毁的对象。 - 分离线程后,不能再对其进行join()操作,也无法获取其状态或等待其完成。 这是std::thread的基本用法,您可以根据实际需求进一步了解和使用其他相关函数和特性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值