Boost執行緒學習

2021-09-02 23:19:20 字數 3200 閱讀 5706

//本文參考部落格

//

#include #include #include #include #include #include #include void hello()

// 第一種方式:最簡單方法

int main_01(int argc, char* argv)

//第二種方式:複雜型別物件作為引數來建立執行緒

// 結構中也可以定義操作符

boost::mutex io_mutex;

// count是乙個結構

struct count

void operator()()

}int id;//預設為public

//如今的struct和class沒啥兩樣,只是struct預設是public,class預設是private

};int main_02(int argc, char* argv)

//第三種方式:在類內部建立執行緒

//(1)類內部靜態方法啟動執行緒

class statichelloworld

static void start()

};//如果要執行這個,把main_31改為main

//因為只能有乙個main函式

int main_31(int argc, char* argv)

//(2)如果要求start()和hello()方法不能是靜態方法則採用下面的方法建立執行緒:

class bindhelloworld

void hellowithparamter(int i)

void start()

};int main_32(int argc, char* argv)

//(3)在singleton模式內部建立執行緒:

//第四種方法:用類內部函式在類外部建立執行緒

//這兩種方式太複雜,懶得折騰了

//互斥體

//1. 乙個互斥體一次只允許乙個執行緒訪問共享區。當乙個執行緒想要訪問共享區時,首先要做的就是鎖住(lock)互斥體。

//2. boost執行緒庫支援兩大類互斥體,包括簡單互斥體(****** mutex)和遞迴互斥體(recursive mutex)。

//   有了遞迴互斥體,單個執行緒就可以對互斥體多次上鎖,當然也必須解鎖同樣次數來保證其他執行緒可以對這個互斥體上鎖。

//3. boost執行緒庫提供的互斥體型別:

//     boost::mutex

//     boost::try_mutex

//     boost::timed_mutex

//     boost::recursive_mutex

//     boost::recursive_try_mutex

//     boost::recursive_timed_mutex

//     boost::shared_mutex

////4. mutex類採用scope lock模式實現互斥體的上鎖和解鎖。即建構函式對互斥體加鎖,析構函式對互斥體解鎖。

//5. 對應現有的幾個mutex匯入了scoped_lock,scoped_try_lock,scoped_timed_lock.

//6. scoped系列的特色就是析構時解鎖,預設構造時加鎖,這就很好的確定在某個作用域下某執行緒獨佔某段**。

//條件變數

//1. 有的時候僅僅依靠鎖住共享資源來使用它是不夠的。有時候共享資源只有某些狀態的時候才能夠使用。

//   比方說,某個執行緒如果要從堆疊中讀取資料,那麼如果棧中沒有資料就必須等待資料被壓棧。這種情

//   況下的同步使用互斥體是不夠的。另一種同步的方式--條件變數,就可以使用在這種情況下。

const int buf_size = 10;

const int iters = 100;

class buffer

void put(int m)

while (full == buf_size)

cond.wait(lock);

}buf[p] = m;

p = (p + 1) % buf_size;

++full;

cond.notify_one();

}int get()

while (full == 0)

cond.wait(lk);

}int i = buf[c];

c = (c + 1) % buf_size;

--full;

cond.notify_one();

return i;

}private:

boost::mutex mutex;

boost::condition cond;

unsigned int p, c, full;

int buf[buf_size];

};buffer buf;

void writer()

buf.put(n);

}}void reader()

}}int main_04(int argc, char* argv)

//執行緒區域性儲存

不懂)//函式的不可重入

//boost執行緒庫提供了智慧型指標boost::thread_specific_ptr來訪問本地儲存執行緒(thread local storage)。

boost::thread_specific_ptrptr;

struct count_local

void operator()()

for (int i = 0; i < 10; ++i)

}int id;

};int main_05(int argc, char* argv)

//僅執行一次的例程

//1. 如何使得初始化工作(比如說建構函式)是執行緒安全的。

//2. 「一次實現」(once routine)。「一次實現」在乙個應用程式只能執行一次。

//3. boost執行緒庫提供了boost::call_once來支援「一次實現」,

//   並且定義了乙個標誌boost::once_flag,乙個初始化這個標誌的巨集 boost_once_init。

int i = 0;

boost::once_flag flag = boost_once_init;

void init()

void thread()

int main(int argc, char* argv)

Boost執行緒學習簡記

boost執行緒學習簡記 include boost thread是執行緒類,建立執行緒非常簡單,只需要建立乙個thread物件,並把執行緒工作函式作為引數傳給構造引數。void output boost thread t output 如果我們的工作函式需要引數怎麼辦呢?void output2 ...

boost建立執行緒池 boost庫使用 執行緒類

boost 庫中提供了兩種建立執行緒的方式,一種是單個執行緒建立,另外一種是執行緒組的建立,進行執行緒管理 thread 就是沒有組管理,與我們在linux下使用pthread create 函式是一樣的,只是在c 11中,引入了boost中的thread方法 包含標頭檔案 include usin...

Boost學習摘要 三線程

boost庫在工作 16 執行緒之一 boost thread boost bind run,1 boost庫在工作 17 執行緒之二 boost thread group 主要使用了boost庫里的執行緒池類thread group,它提供了多個執行緒建立 儲存 退出等管理。比如使用create ...