C++11之线程库(Thread、Mutex、atomic、lock_guard、同步)

简介: C++11之线程库(Thread、Mutex、atomic、lock_guard、同步)

线程库C++11

在C++ 11引入了对多线程的支持。包括线程、互斥锁、原子操作、自动加减锁和同步。下面就分别介绍一下对应的用法。


线程Thread

线程:系统分配cup时间和调度的基本单位

头文件

#include<thread>

常用的成员函数

函数名 作用
get_id() 获取当前线程ID
join() 等待
detach() 分离


创建线程的方式

thread t1(函数地址);//无参

thread t2(函数地址,参数1,参数2,…);//有参

A a;

thread t3(&A::output,&a);成员函数,


全局函数实例:

#include <thread>
#include <string>
#include <iostream>
using namespace std;
void test1(string name)//子线程 有参
{
    for (int i = 0; i < 100; ++i)
    {
        cout << name <<  " test1:" << i << endl;
    }
}
void test2()//子线程
{
    for (int i = 0; i < 100; ++i)
    {
        cout << "test2:" << i << endl;
    }
}
int main()
{
    thread t1(test1, "线程1");//创建线程 并启动线程
    thread t2(test2);
    t1.join();//等待线程执行结束,不写会造成程序崩溃
    t2.join();
    return 0;
}


成员函数实例:

class A
{
public:
    void output(string name)
    {
        cout << "线程执行 " << name << endl;
    }
};
int main()
{
    A a;
    //格式:thread t1(函数地址 对象地址 参数1 参数2...)
    thread t1(&A::output, &a, "线程一");//成员函数做线程
    t1.join();
    return 0;
}


多线程的冲突问题

a. 出现了更高等级的指令

b. 该线程的时间片到了

互斥体Mutex

头文件

#include<mutex>

常用的成员函数

函数名 作用
lock() 加锁
unlock() 解锁


实例

#include <thread>
#include <string>
#include <iostream>
#include <mutex>
using namespace std;
int g_a = 0;
mutex g_m;//创建锁
void test3()
{
    for (int i = 0; i < 1000000; ++i)
    {
        //保证数据的完整性
        g_m.lock();//加锁
        g_a += 1;
        g_m.unlock();//解锁
    }
}
int main()
{
    thread t1(test3);
    thread t2(test3);
    thread t3(test3);
    t1.join();
    t2.join();
    t3.join();
    cout << g_a << endl;
    return 0;
}


原子操作Atomic

头文件

#include<atomic>

实例

#include <thread>
#include <iostream>
#include <atomic>
using namespace std;
atomic_int a;//将这个变量设置为int原子级别
void test3()
{
    for (int i = 0; i < 1000000; ++i)
    {
        ++a;//在执行过程中,等同于锁
    }
}
int main()
{
    thread t1(test3);
    thread t2(test3);
    thread t3(test3);
    t1.join();
    t2.join();
    t3.join();
    cout << a << endl;
    return 0;
}


自动加解锁lock_guard

在创建时调用构造函数自动加锁,出了作用域就调用析构函数解锁。

实例

#include <thread>
#include <string>
#include <iostream>
#include <mutex>
#include <atomic>
using namespace std;
int a = 0;
mutex g_m;//创建锁
void test3()
{
    for (int i = 0; i < 1000000; ++i)
    {
        lock_guard<mutex> la(g_m);//自动加锁 自动释放
        ++a;
        cout << a << endl;
    }
}
int main()
{
    thread t1(test3);
    thread t2(test3);
    thread t3(test3);
    t1.join();
    t2.join();
    t3.join();
    cout << a << endl;
    return 0;
}


同步

同步方式

  1. 信号量
  2. 事件
  3. 消息
  4. C++11中的条件变量

实例

#include <thread>
#include <string>
#include <iostream>
#include <mutex>
#include <atomic>
#include <string>
#include <condition_variable>
using namespace std;
string  buffer;
mutex m; 
condition_variable cv; //状态变量
//为了防止竞争,条件变量的使用总是和一个互斥锁结合在一起;
void workThread()
{
    //unique_lock比lock_guard灵活很多,效率上差一点,内存占用多一点。
    unique_lock<mutex> lk(m);
    cv.wait(lk);//等待信号
    cout << "收到数据:" << buffer << endl;
}
int main()
{
    thread t1(workThread);
    string name;
    cin >> name;
    buffer = name;
    cv.notify_all();//发出通知
    t1.join();
    cout << "数据处理完成" << endl;
    return 0;
}
目录
相关文章
|
API C++ Windows
Visual C++运行库、.NET Framework和DirectX运行库的作用及常见问题解决方案,涵盖MSVCP140.dll丢失、0xc000007b错误等典型故障的修复方法
本文介绍Visual C++运行库、.NET Framework和DirectX运行库的作用及常见问题解决方案,涵盖MSVCP140.dll丢失、0xc000007b错误等典型故障的修复方法,提供官方下载链接与系统修复工具使用指南。
2549 2
|
缓存 算法 程序员
C++STL底层原理:探秘标准模板库的内部机制
🌟蒋星熠Jaxonic带你深入STL底层:从容器内存管理到红黑树、哈希表,剖析迭代器、算法与分配器核心机制,揭秘C++标准库的高效设计哲学与性能优化实践。
1051 3
C++STL底层原理:探秘标准模板库的内部机制
|
Ubuntu API C++
C++标准库、Windows API及Ubuntu API的综合应用
总之,C++标准库、Windows API和Ubuntu API的综合应用是一项挑战性较大的任务,需要开发者具备跨平台编程的深入知识和丰富经验。通过合理的架构设计和有效的工具选择,可以在不同的操作系统平台上高效地开发和部署应用程序。
527 11
|
IDE 编译器 开发工具
msvcp100.dll,msvcp120.dll,msvcp140.dll,Microsoft Visual C++ 2015 Redistributable,Visual C++ 运行库安装
MSVC是Windows下C/C++开发核心工具,集成编译器、链接器与调试器,配合Visual Studio使用。其运行时库(如msvcp140.dll)为程序提供基础函数支持,常因缺失导致软件无法运行。通过安装对应版本的Microsoft Visual C++ Redistributable可解决此类问题,广泛应用于桌面软件、游戏及系统级开发。
1650 2
|
并行计算 C++ Windows
|
Arthas 监控 Java
Arthas thread(查看当前JVM的线程堆栈信息)
Arthas thread(查看当前JVM的线程堆栈信息)
2561 10
|
编译器 C++ 开发者
【C++篇】深度解析类与对象(下)
在上一篇博客中,我们学习了C++的基础类与对象概念,包括类的定义、对象的使用和构造函数的作用。在这一篇,我们将深入探讨C++类的一些重要特性,如构造函数的高级用法、类型转换、static成员、友元、内部类、匿名对象,以及对象拷贝优化等。这些内容可以帮助你更好地理解和应用面向对象编程的核心理念,提升代码的健壮性、灵活性和可维护性。
|
编译器 C++ 容器
【c++11】c++11新特性(上)(列表初始化、右值引用和移动语义、类的新默认成员函数、lambda表达式)
C++11为C++带来了革命性变化,引入了列表初始化、右值引用、移动语义、类的新默认成员函数和lambda表达式等特性。列表初始化统一了对象初始化方式,initializer_list简化了容器多元素初始化;右值引用和移动语义优化了资源管理,减少拷贝开销;类新增移动构造和移动赋值函数提升性能;lambda表达式提供匿名函数对象,增强代码简洁性和灵活性。这些特性共同推动了现代C++编程的发展,提升了开发效率与程序性能。
755 12
|
编译器 C语言 C++
类和对象的简述(c++篇)
类和对象的简述(c++篇)