c++11庫 的基本使用

創建線程:

/*************************************************************************
    > File Name: 1.cpp
    > Author: 朱紫鈺
    > Mail: [email protected]
    > Created Time: 2017年12月19日 星期二 13時32分14秒
 ************************************************************************/

#include<iostream>
#include <thread>
using namespace std;
void fun()
{
    cout << "This is thread of " << this_thread::get_id()<<  endl;

}
void fun2(int a)
{
    cout << "This is a thread of " << this_thread::get_id() <<"with refenerce " << a << endl;
}
int main()
{
    thread t1(fun);
    t1.join();//子線程執行完主線程纔會執行

    thread t2(fun2,5);
    t2.join();

    cout << "father end" <<endl;

}

運行結果:
這裏寫圖片描述

分離子線程

這樣原來的線程不用等它了

/*************************************************************************
    > File Name: 1.cpp
    > Author: 朱紫鈺
    > Mail: [email protected]
    > Created Time: 2017年12月19日 星期二 13時32分14秒
 ************************************************************************/

#include<iostream>
#include <thread>
using namespace std;
void fun()
{
    cout << "This is thread of " << this_thread::get_id()<<  endl;

}
void fun2(int a)
{
    cout << "This is a thread of " << this_thread::get_id() <<"with refenerce " << a << endl;
}
int main()
{
    thread t1(fun);
    t1.detah();

    thread t2(fun2,5);
    t2.detah();

    cout << "father end" <<endl;

}

運行結果:
這裏寫圖片描述

因爲此時的線程沒有確定的順序,因此顯示結果比較亂

加上互斥量

互斥鎖對於公共訪問區有巨大作用。線程共用一份代碼,而沒有確定執行順序的情況下,必須對公共訪問區加鎖以保證數據不會亂。
先看沒有加鎖的情況

/*************************************************************************
    > File Name: 1.cpp
    > Author: 朱紫鈺
    > Mail: [email protected]
    > Created Time: 2017年12月19日 星期二 13時32分14秒
 ************************************************************************/

#include<iostream>
#include <thread>
#include<mutex>

using namespace std;
mutex lock;
static int vary = 50;

void fun()
{
    cout << this_thread::get_id() << "  vary =  " << ++vary <<  endl;

}
void fun2()
{
    cout << this_thread::get_id() << "  vary =  " << ++vary << endl;
}
int main()
{
    thread t1(fun);
    thread t2(fun2);
    t1.detach();
    t2.detach();
    cout << "father end" <<endl;

}

運行結果:
這裏寫圖片描述

再看加鎖後的使用:

/*************************************************************************
    > File Name: 1.cpp
    > Author: 朱紫鈺
    > Mail: [email protected]
    > Created Time: 2017年12月19日 星期二 13時32分14秒
 ************************************************************************/

#include<iostream>
#include <thread>
#include<mutex>
#include<algorithm>
using namespace std;
mutex m;
static int vary = 50;

void fun()
{
    m.lock();
    ++vary;
    cout << this_thread::get_id() << "  vary =  " << ++vary <<  endl;
     m.unlock();
}
void fun2()
{
    m.lock();
    ++vary;
    cout << this_thread::get_id() << "  vary =  " << ++vary << endl;
     m.unlock();
}
int main()
{
    thread t1(fun);
    thread t2(fun2);
    t1.join();
    t2.join();
    cout << "father end" <<endl;

}

這裏寫圖片描述

鎖中lock_guard的使用

lock_guard:更加靈活的鎖管理類模板,構造時是否加鎖是可選的,在對象析構時如果持有鎖會自動釋放鎖,所有權可以轉移。對象生命期內允許手動加鎖和釋放鎖。

/*************************************************************************
    > File Name: 1.cpp
    > Author: 朱紫鈺
    > Mail: [email protected]
    > Created Time: 2017年12月19日 星期二 13時32分14秒
 ************************************************************************/

#include<iostream>
#include <thread>
#include<mutex>
#include<algorithm>
using namespace std;
mutex m;
static int vary = 50;

void fun()
{
    std::lock_guard<std::mutex> lck(m);
    ++vary;
    cout << this_thread::get_id() << "  vary =  " << ++vary <<  endl;
}
void fun2()
{
    std::lock_guard<std::mutex> lck(m);
    ++vary;
    cout << this_thread::get_id() << "  vary =  " << ++vary << endl;

}
int main()
{
    thread t1(fun);
    thread t2(fun2);
    t1.join();
    t2.join();
    cout << "father end" <<endl;

}

因爲有封裝mutex的意思,即銷燬lock_guard變量執行析構函數自動解鎖,因此更方便管理。
結果:
這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章