BOOST線程詳解

線程的中斷點函數

thread::join myThread::join調用這個方法的線程進入wait狀態,直到myThread表明的線程完成
thread::try_join_for
thread::try_join_until
阻塞等待必定的時間段
   
condition_variable_any::wait wait(mu)
condition_variable_any::wait_for
condition_variable_any::wait_until
 
   
condition_variable_any::notify_one
condition_variable_any::notify_all
通知一個/全部等待中的線程
   
this_thread::sleep_for
this_thread::sleep_until
當前線程放棄時間片,指定堵塞時間,容許其餘同優先級的線程運行,不釋放資源
   
this_thread::yield 當前線程放棄時間片,不指定堵塞時間,容許其餘同優先級的線程運行,不釋放資源
   
   
thread::detach 分離線程
thread::interrupt 中斷線程

 

互斥鎖 boost::mutex mu
boost::mutex::scoped_lock lock(mu)
讀寫鎖 boost::shared_mutex rw_mu
boost::shared_lock<boost::shared_mutex> lock(rw_mu)
boost::unique_lock<boost::shared_mutex> lock(rw_mu)
條件量           boost::mutex ioMu
boost::condition_variable_any condPut
boost::mutex::scoped_lock lock(ioMu)
condPut.wait(ioMu)
condPut.notify_one()
遞歸鎖 boost::recursive_mutex rm
boost::recursive_mutex::scoped_lock lock(rw) 或者
boost::lock_guard<boost::recursive_mutex> lock(rw)

 

lambda表達式:匿名函數,自動類型推斷和類型獲取this

[captures] (params) –> ret {Statements;} 函數返回值後置atom

 

atomic原子操做spa

 

lock_guard、shared_lock、unique_lock都是RAII(資源獲取就是初始化)線程

boost::mutex::scoped_lock是boost::unique_lock<boost::mutex>的一個子類對象

 

互斥鎖:遞歸

int g_num = 0;
boost::mutex mu;  //定義互斥鎖對象 隊列

int Func(int nCount)
{
    for (int i = 0; i < nCount; i++)
    {
        boost::mutex::scoped_lock lock(mu);  //對共享數據進行操做,需加鎖
        g_num++;
        //cout << __FUNCTION__ << ": " << g_num << endl;
        cout << boost::this_thread::get_id() << ":" << g_num << endl;
    }
    return g_num;
} ci

int _tmain(int argc, _TCHAR* argv[])
{
    boost::thread th1(Func, 100);
    boost::thread th2(Func, 200);
    th1.join();
    th2.join(); 資源

    cout << boost::this_thread::get_id() << ":" << "main_end" << endl;

    //th1和th2的執行順序不定,不過必定是一個執行完畢後另外一個纔會執行,最後纔是main_end

    system("pause");
    return 0;
}

 

讀寫鎖:

int g_num = 0;
boost::shared_mutex rw_mu;   //定義讀寫鎖

int Write(int nCount)
{
    for (int i = 0; i < nCount; i++)
    {
        boost::unique_lock<boost::shared_mutex> lock(rw_mu);   //加惟一鎖
        g_num++;
        //cout << __FUNCTION__ << ": " << g_num << endl;
        cout << boost::this_thread::get_id() << ": " << __FUNCTION__ << ": " << g_num << endl;
    }
    return g_num;
}

void Read(int nCount)
{
    for (int i = 0; i < nCount; i++)
    {
        boost::shared_lock<boost::shared_mutex> lock(rw_mu);  //加共享鎖
        //cout << __FUNCTION__ << ": " << g_num << endl;
        cout << boost::this_thread::get_id() << ": " << __FUNCTION__ << ": (" << i << "):" << g_num << endl;
    }
}

int _tmain(int argc, _TCHAR* argv[])
{
    boost::thread th1(Write, 100);
    boost::thread th2(Read, 100);
    boost::thread th3(Read, 100);

    th1.join();
    th2.join();
    th3.join();

    system("pause");

    return 0;
}

 

條件量:

boost::mutex g_ioMutex;    //輸出控制鎖

template<typename T>
class CMsgQueue
{
public:
    CMsgQueue(size_t n):m_nCapacity(n)
    {
    }

    void Push(const T& val)
    {
        {
            boost::mutex::scoped_lock lock(m_mu);              //加鎖
            while(m_val.size() == m_nCapacity)                 //隊列已滿
            {
                {
                    boost::mutex::scoped_lock lock(g_ioMutex);
                    cout << "隊列已滿" << endl;
                }
                m_condPush.wait(m_mu);                         //等待,將暫時的解鎖
            }
            m_val.push(val);                                   //添加數據到隊列
        }
        m_condPop.notify_one();                                 //通知讀線程
    }

    void Pop(T& val)
    {
        {
            boost::mutex::scoped_lock lock(m_mu);               //加鎖
            while(m_val.size() == 0)                            //隊列爲空
            {
                {
                    boost::mutex::scoped_lock lock(g_ioMutex);
                    cout << "隊列爲空" << endl;
                }
                m_condPop.wait(m_mu);                           //等待可讀,
            }
            val = m_val.front();                                 //讀取數據
            m_val.pop();
        }
        m_condPush.notify_one();                                 //通知寫線程
    }

private:
    queue<T> m_val;                            //隊列
    int m_nCapacity;                           //隊列最大容量
    boost::condition_variable_any m_condPush;  //寫入條件量
    boost::condition_variable_any m_condPop;   //讀取條件量
    boost::mutex m_mu;                         //互斥鎖
};

CMsgQueue<int> g_numQueue(10);

void Push(int nCount)
{
    for (int i = 0; i < nCount; i++)
    {
        {
            boost::mutex::scoped_lock lock(g_ioMutex);
            cout << boost::this_thread::get_id() << ":" << __FUNCTION__ << " :Before_Push :" << i << endl;
        }   
        g_numQueue.Push(i);
    }
}

void Pop(int nCount)
{
    for (int i = 0; i < nCount; i++)
    {       
        int val;
        g_numQueue.Pop(val);
        boost::mutex::scoped_lock lock(g_ioMutex);
        cout << boost::this_thread::get_id() << ":" << __FUNCTION__ << " :After_Pop(" << i << "):" <<  val << endl;
    }
}

int _tmain(int argc, _TCHAR* argv[])
{
    boost::thread th1(Push, 50);
    boost::thread th2(Pop, 20);
    boost::thread th3(Pop, 30);

    th1.join();
    th2.join();
    th3.join();

    system("pause");

    return 0;
}

 

 

未完待續…

相關文章
相關標籤/搜索