C++并发与多线程(五)互斥量,atomic、与线程池(上)

简介: C++并发与多线程(五)互斥量,atomic、与线程池(上)

互斥量


  互斥量多线程编程中 用于保护共享数据:先锁住, 操作共享数据, 解锁。有两个线程,对一个变量进行操作,一个线程读这个变量的值,一个线程往这个变量中写值。即使是一个简单变量的读取和写入操作,如果不加锁,也有可能会导致读写值混乱(一条C语句会被拆成34条汇编语句来执行,所以仍然有可能混乱)。


#include <iostream>
#include <thread>
using namespace std;
int g_count = 0;
void mythread1() {
  for (int i = 0; i < 1000000; i++) {
    g_count++;
  }
}
int main() {
  std::thread t1(mythread1);
  std::thread t2(mythread1);
  t1.join();
  t2.join();
  cout << "正常情况下结果应该是200 0000次,实际是" << g_count << endl;
}

  出现上述情况的问题在于g_count++;这步操作被打断了。我们可以使用mutex解决这个问题:


#include <iostream>
#include <thread>
#include <mutex>
using namespace std;
int g_count = 0;
std::mutex mymutex;
void mythread1() {
  for (int i = 0; i < 1000000; i++) {
    std::unique_lock<std::mutex> u1(mymutex);
    g_count++;
  }
}
int main() {
  std::thread t1(mythread1);
  std::thread t2(mythread1);
  t1.join();
  t2.join();
  cout << "正常情况下结果应该是200 0000次,实际是" << g_count << endl;
}


std::atomic


  大家可以把原子操作理解成一种:不需要用到互斥量加锁(无锁)技术的多线程并发编程方式。原子操作:在多线程中不会被打断的程序执行片段。从效率上来说,原子操作要比互斥量的方式效率要高。互斥量的加锁一般是针对一个代码段,而原子操作针对的一般都是一个变量。原子操作,一般都是指“不可分割的操作”;也就是说这种操作状态要么是完成的,要么是没完成的,不可能出现半完成状态std::atomic来代表原子操作,是个类模板。其实std::atomic是用来封装某个类型的值的。需要添加#include头文件。

#include <iostream>
#include <thread>
#include <atomic>
using namespace std;
std::atomic<int> g_count = 0; //封装了一个类型为int的 对象(值)
void mythread1() {
  for (int i = 0; i < 1000000; i++) {
    g_count++;
  }
}
int main() {
  std::thread t1(mythread1);
  std::thread t2(mythread1);
  t1.join();
  t2.join();
  cout << "正常情况下结果应该是200 0000次,实际是" << g_count << endl;
}

  输出结果为:


#include <iostream>
#include <thread>
#include <atomic>
using namespace std;
std::atomic<bool> g_ifEnd = false; //封装了一个类型为bool的 对象(值)
void mythread() {
  std::chrono::milliseconds dura(1000);
  while (g_ifEnd == false) {
    cout << "thread id = " << std::this_thread::get_id() << "运行中" << endl;
    std::this_thread::sleep_for(dura);
  }
  cout << "thread id = " << std::this_thread::get_id() << "运行结束" << endl;
}
int main() {
  std::thread t1(mythread);
  std::thread t2(mythread);
  std::chrono::milliseconds dura(5000);
  std::this_thread::sleep_for(dura);
  g_ifEnd = true;
  cout << "程序执行完毕" << endl;
  t1.join();
  t2.join();
}

  程序输出结果为:


  原子操作一般用于计数或者统计(如累计发送多少个数据包,累计接收到了多少个数据包),多个线程一起统计,这种情况如果不使用原子操作会导致统计发生混乱。


std::atomic续谈


#include <iostream>
#include <mutex>
#include <thread>
using namespace std;
std::atomic<int> g_count;
void mythread1() {
    for (int i = 0; i < 1000000; i++) {
        //g_count += 1; // 正确
        g_count = g_count + 1; // 错误:虽然g_count使用了原子操作模板,但是这种写法既读又写,
    }
}
int main() {
    std::thread t1(mythread1);
    std::thread t2(mythread1);
    t1.join();
    t2.join();
    cout << "正常情况下结果应该是200 0000次,实际是" << g_count << endl;
}

  一般atomic原子操作,针对+++=-=&=|=^=是支持的,其他操作不一定支持。


std::async深入理解


  std::async参数详述,async用来创建一个异步任务。延迟调用参数 std::launch::deferred【延迟调用】,std::launch::async【强制创建一个线程】。std::async()我们一般不叫创建线程(他能够创建线程),我们一般叫它创建一个异步任务。


#include <iostream>
#include <mutex>
#include <thread>
#include <future>
using namespace std;
std::atomic<int> g_count;
int mythread() {
    cout << "thread start thread id is: " << std::this_thread::get_id() << endl;
    return 10;
}
int main() {
    cout << "main start thread id is: " << std::this_thread::get_id() << endl;
    std::future<int> res = std::async(mythread);
    cout << "res.get() is : " << res.get() << endl;
}

  程序输出结果为:

main start thread id is: 0x1000e3d40
res.get() is : thread start thread id is: 0x16fe87000
10

  std::asyncstd::thread最明显的不同,就是async有时候并不创建新线程。

  1. 如果用std::launch::deferred来调用async
#include <iostream>
#include <mutex>
#include <thread>
#include <future>
using namespace std;
std::atomic<int> g_count;
int mythread() {
    cout << "thread start thread id is: " << std::this_thread::get_id() << endl;
    return 10;
}
int main() {
    cout << "main start thread id is: " << std::this_thread::get_id() << endl;
    std::future<int> res = std::async(std::launch::deferred,mythread);
    cout << "res.get() is : " << res.get() << endl;
}

  程序输出结果为:

main start thread id is: 0x1000e3d40
res.get() is : thread start thread id is: 0x1000e3d40
10

  延迟到调用get()或者wait()时执行,如果不调用就不会执行。并且没有创建新线程。

相关文章
|
6天前
|
安全 前端开发 程序员
|
6天前
|
Java 数据库
【Java多线程】对线程池的理解并模拟实现线程池
【Java多线程】对线程池的理解并模拟实现线程池
18 1
|
6天前
|
Java Linux 调度
|
6天前
|
安全 C++
C++多线程编程:并发与同步
C++多线程编程:并发与同步
12 0
|
6天前
|
算法 安全 调度
【C++入门到精通】 线程库 | thread类 C++11 [ C++入门 ]
【C++入门到精通】 线程库 | thread类 C++11 [ C++入门 ]
17 1
|
1天前
|
C++
【C++基础】类class
【C++基础】类class
9 1
|
1天前
|
安全 程序员 编译器
C++程序中的基类与派生类转换
C++程序中的基类与派生类转换
8 1
|
1天前
|
C++
C++程序中的类成员函数
C++程序中的类成员函数
7 1
|
1天前
|
C++
C++程序中的类封装性与信息隐蔽
C++程序中的类封装性与信息隐蔽
8 1
|
1天前
|
C++
C++程序中的类声明与对象定义
C++程序中的类声明与对象定义
9 1