线程创建
#include <thread>//C++11引入的标准线程库
thread t(TestThreadFun);//thread t= thread
(TestThreadFun);//创建了一个线程对象来执行线程函数TestThreadFun
thread* pt = new thread(TestThreadFunction);//直接创建出来的线程对象,如果没有说明销毁的方式,在程序结束的时候回产生错误。
- 说明线程销毁方式有两种
- detach:使线程和主线程之间不再有联系
- join:如果是join方式,主线程要等join的线程结束才能执行
- t.detach();
互斥锁
#include<mutex>//互斥锁对象
mutex g_Mutex1;//创建互斥锁对象
int g_num=0;
lock_guard<mutex> _selfunlock(g_Mutex1);//自解锁,出了作用域自动销毁
g_Mutex1.lock();//锁住
g_num++;
g_Mutex1.unlock();//解锁
- 使用互斥锁的时候,虽然能够保证公共数据点安全,但是不能乱用
锁的数据范围,尽可能的小,锁住的东西越多越慢,锁越多也越慢。
原子变量
#include<atomic>
atomic<int>g_num_ato;//atomic_int g_num_ato;//原子变量
atomic_int//等价上面
//原子操作支持的类型
typedef atomic<bool> atomic_bool;
typedef atomic<char> atomic_char;
typedef atomic<signed char> atomic_schar;
typedef atomic<unsigned char> atomic_uchar;
typedef atomic<short> atomic_short;
typedef atomic<unsigned short> atomic_ushort;
typedef atomic<int> atomic_int;
typedef atomic<unsigned int> atomic_uint;
typedef atomic<long> atomic_long;
typedef atomic<unsigned long> atomic_ulong;
typedef atomic<long long> atomic_llong;
typedef atomic<unsigned long long> atomic_ullong;
typedef atomic<char16_t> atomic_char16_t;
typedef atomic<char32_t> atomic_char32_t;
typedef atomic<wchar_t> atomic_wchar_t;
typedef atomic<int8_t> atomic_int8_t;
typedef atomic<uint8_t> atomic_uint8_t;
typedef atomic<int16_t> atomic_int16_t;
typedef atomic<uint16_t> atomic_uint16_t;
typedef atomic<int32_t> atomic_int32_t;
typedef atomic<uint32_t> atomic_uint32_t;
typedef atomic<int64_t> atomic_int64_t;
typedef atomic<uint64_t> atomic_uint64_t;
typedef atomic<int_least8_t> atomic_int_least8_t;
typedef atomic<uint_least8_t> atomic_uint_least8_t;
typedef atomic<int_least16_t> atomic_int_least16_t;
typedef atomic<uint_least16_t> atomic_uint_least16_t;
typedef atomic<int_least32_t> atomic_int_least32_t;
typedef atomic<uint_least32_t> atomic_uint_least32_t;
typedef atomic<int_least64_t> atomic_int_least64_t;
typedef atomic<uint_least64_t> atomic_uint_least64_t;
typedef atomic<int_fast8_t> atomic_int_fast8_t;
typedef atomic<uint_fast8_t> atomic_uint_fast8_t;
typedef atomic<int_fast16_t> atomic_int_fast16_t;
typedef atomic<uint_fast16_t> atomic_uint_fast16_t;
typedef atomic<int_fast32_t> atomic_int_fast32_t;
typedef atomic<uint_fast32_t> atomic_uint_fast32_t;
typedef atomic<int_fast64_t> atomic_int_fast64_t;
typedef atomic<uint_fast64_t> atomic_uint_fast64_t;
typedef atomic<intptr_t> atomic_intptr_t;
typedef atomic<uintptr_t> atomic_uintptr_t;
typedef atomic<size_t> atomic_size_t;
typedef atomic<ptrdiff_t> atomic_ptrdiff_t;
typedef atomic<intmax_t> atomic_intmax_t;
typedef atomic<uintmax_t> atomic_uintmax_t;
多线程进度条练习
- 用线程调用加载资源的函数(每次循环随机等待一下,没循环一次修改一下全局的进度变量)
主线程中,循环访问全局进度变量,将当前的进度换成百分比输出,并且绘制进度条的变化
- printf("当前线程的id: %d\n", this_thread::get_id());//获取线程id
- this_thread::sleep_for(std::chrono::seconds(rand()%5+1));//#include 或者引入头文件
#include<iostream>
#include<thread>
#include<atomic>
#include<stdio.h>
using namespace std;
atomic<int>g_num_ato=0;
void fun(int n)
{
for (int i = 0; i < n; i++)
{
g_num_ato++;
this_thread::sleep_for(std::chrono::seconds(rand()%5+1));
}
}
int main()
{
thread t = thread(fun,100);
t.detach();
while (g_num_ato <100)
{
system("cls");
int iTem = g_num_ato;
cout << "当前值是:" << iTem << "%" << endl;
for (int i = 0; i < iTem /5; i++)
{
cout << "★";
}
}
cout << "加载完成"<<endl;
system("pause");
return 0;
}