C++11多線程入門

線程建立

#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 iTemp = g_num_ato;
		cout << "當前值是:" << iTemp << "%" << endl;
		for (int i = 0; i < iTemp /5; i++)
		{
			cout << "★";
		}
	}
	cout << "加載完成"<<endl;
	system("pause");
	return 0;
}
相關文章
相關標籤/搜索