c++11 std:thread 多線程


參考:

1.C++11 並發指南一(C++11 多線程初探)

2.C++11 並發指南二(std::thread 詳解)

3.C++11 Thread多線程的學習心得與問題

4.C++11多線程(簡約但不簡單)

5.github(《c++並發編程》基本上述所以例子都出於這里,也不是很長,有一點基礎的直接看這個就行)

 

如果第一次接觸,直接看我給的參考即可,看過了之后,我把我覺得ok的重點總結在了下面

-----------------------------------------------------筆記--------------------------------------------------

博客一:(一個簡單例程+makefile)

#include <stdio.h> #include <stdlib.h> #include <iostream> // std::cout
#include <thread>   // std::thread

void thread_task() { std::cout << "hello thread" << std::endl; } /* * === FUNCTION ========================================================= * Name: main * Description: program entry routine. * ======================================================================== */
int main(int argc, const char *argv[]) { std::thread t(thread_task); t.join(); return EXIT_SUCCESS; } /* ---------- end of function main ---------- */
View Code
all:Thread

CC=g++
CPPFLAGS=-Wall -std=c++11 -ggdb
LDFLAGS=-pthread

Thread:Thread.o
    $(CC) $(LDFLAGS) -o $@ $^

Thread.o:Thread.cc
    $(CC) $(CPPFLAGS) -o $@ -c $^


.PHONY:
    clean

clean:
    rm Thread.o Thread
View Code

 

博客二:(算是cplusplus官網給的.join()例子的擴充,挺好的)(也有其他函數的官網鏈接)

#include <stdio.h> #include <stdlib.h> #include <chrono>    // std::chrono::seconds
#include <iostream>  // std::cout
#include <thread>    // std::thread, std::this_thread::sleep_for

void thread_task(int n) { std::this_thread::sleep_for(std::chrono::seconds(n)); std::cout << "hello thread "
        << std::this_thread::get_id() << " paused " << n << " seconds" << std::endl; } /* * === FUNCTION ========================================================= * Name: main * Description: program entry routine. * ======================================================================== */
int main(int argc, const char *argv[]) { std::thread threads[5]; std::cout << "Spawning 5 threads...\n"; for (int i = 0; i < 5; i++) { threads[i] = std::thread(thread_task, i + 1); } std::cout << "Done spawning threads! Now wait for them to join\n"; for (auto& t: threads) { t.join(); } std::cout << "All threads joined.\n"; return EXIT_SUCCESS; } /* ---------- end of function main ---------- */
View Code

結果是隔一秒打印一行

其他成員函數

 

博客三:(有一個使用互斥量的例子,介紹了volatile關鍵字)

如果該線程是在同一類的某一成員函數當中被構造,則直接用this關鍵字代替即可。

這里使用this指針代替實例化對象的地址

#include<iostream> #include<thread> #include<mutex> std::mutex mut; class A{ public: volatile int temp; A(){ temp=0; } void fun(int num){ int count=10; while(count>0){ mut.lock(); temp++; std::cout<<"thread_"<<num<<"...temp="<<temp<<std::endl; mut.unlock(); count--; } } void thread_run(){ std::thread t1(&A::fun,this,1); std::thread t2(&A::fun,this,2); t1.join(); t2.join(); } }; int main(){ A a; a.thread_run(); }
View Code

 

參考五:(hardware_concurrency)

檢測硬件並發特性,返回當前平台的線程實現所支持的線程並發數目,但返回值僅僅只作為系統提示(hint)。

#include <iostream>
  #include <thread>
   
  int main() {
      unsigned int n = std::thread::hardware_concurrency();
      std::cout << n << " concurrent threads are supported.\n";
  }

虛擬機設置為2核,支持兩個線程並發

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM