0%
Theme NexT works best with JavaScript enabled
多线程的基本知识 C++11Thead线程库的基本使用(链接 ) 创建一个线程 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include <iostream> #include <thread> using namespace std;void printHelloWorld () { cout << "Hello World" << endl; } int main () { thread thread1 (printHelloWorld) ; if (thread1.joinable ()) { thread1.join (); } return 0 ; }
互斥量 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 mutex mtx; int a = 0 ;void func () { for (int i = 0 ; i < 10000 ; i++){ mtx.lock (); a++; mtx.unlock (); } } int main () { thread thread1 (func) ; thread thread2 (func) ; thread1.join (); thread2.join (); cout << a; return 0 ; }
互斥量死锁 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 mutex mtx1; mutex mtx2; void func1 () { for (int i = 0 ; i < 100000 ; i++) { mtx1.lock (); mtx2.lock (); mtx1.unlock (); mtx2.unlock (); } } void func2 () { for (int i = 0 ; i < 100000 ; i++) { mtx2.lock (); mtx1.lock (); mtx1.unlock (); mtx2.unlock (); } } int main () { thread thread1 (func1) ; thread thread2 (func2) ; thread1.join (); thread2.join (); return 0 ; }
lock_guard和unique_lock 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 mutex mtx; int a = 0 ;void func () { for (int i = 0 ; i < 10000 ; i++) { unique_lock<mutex> lg (mtx) ; a++; } } int main () { thread thread1 (func) ; thread thread2 (func) ; thread1.join (); thread2.join (); cout << a; return 0 ; }
call_once与其使用场景 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include <iostream> #include <thread> #include <mutex> using namespace std;class Log {public : Log (const Log& log) = delete ; Log& operator = (const Log& log) = delete ; static Log& GetInstance () { static Log log; return log; } void printLog (string msg) { cout << __TIME__<<" " << msg << endl; } private : Log () {}; }; int main () { Log::GetInstance ().printLog ("error" ); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 class Singleton {public : static Singleton& getInstance () { call_once (m_onceFlag, &Singleton::init); return *m_instance; } void setData (int data) { m_data = data; } int getData () const { return m_data; } private : Singleton () {} Singleton (const Singleton&) = delete ; Singleton& operator =(const Singleton&) = delete ; static void init () { m_instance = new Singleton (); } static Singleton *m_instance; static once_flag m_onceFlag; int m_data = 0 ; }; Singleton *Singleton::m_instance; once_flag Singleton::m_onceFlag; void func () { Singleton::getInstance ().setData (1 ); } void func1 () { cout<< Singleton::getInstance ().getData (); } int main () { thread t1 (func) ; thread t2 (func1) ; t1.join (); t2.join (); return 0 ; }
condition_variable 条件变量 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 #include <iostream> #include <thread> #include <mutex> #include <condition_variable> #include <queue> using namespace std;queue<int > g_queue; condition_variable g_cv; mutex mtx; void producer () { for (int i = 0 ; i < 10 ; i++) { { unique_lock<mutex> lock (mtx) ; g_queue.push (i); g_cv.notify_one (); cout << "producer:" << i << endl; } this_thread::sleep_for (chrono::microseconds (1 )); } }; void consumer () { while (1 ) { unique_lock<mutex> lock (mtx) ; g_cv.wait (lock, []() {return !g_queue.empty (); }); int value = g_queue.front (); g_queue.pop (); cout << "consumer:" << value << endl; } }; int main () { thread t1 (producer) ; thread t2 (consumer) ; t1.join (); t2.join (); return 0 ; }
线程池 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 #include <iostream> #include <thread> #include <mutex> #include <condition_variable> #include <queue> #include <vector> #include <functional> using namespace std;class ThreadPool {public : ThreadPool (int nums) :stop (false ) { for (int i = 0 ; i < nums; i++) { threads.emplace_back ([this ] { while (true ) { unique_lock<mutex> lock (mtx); conditionn_v.wait (lock, [this ] {return !tasks.empty () || stop; }); if (stop && tasks.empty ()) { return ; } auto task = move (tasks.front ()); tasks.pop (); lock.unlock (); task (); }; }); } } ~ThreadPool () { { unique_lock<mutex> mtx; stop = true ; } conditionn_v.notify_all (); for (auto & thread : threads) { thread.join (); } } template <typename F, typename ... Args> void enqueue (F&& f, Args&&... args) { function<void () > task (bind(forward<F>(f), forward<Args>(args)...)) ; { unique_lock<mutex>lock (mtx); tasks.emplace (move (task)); } conditionn_v.notify_one (); } private : vector<thread> threads; queue<function<void ()>> tasks; mutex mtx; condition_variable conditionn_v; bool stop; }; int main () { ThreadPool poo1 (10 ) ; for (int i = 0 ; i < 100 ; i++) { poo1.enqueue ([i] { printf ("Task:%d is running in thread:%d\n" , i, this_thread::get_id ()); this_thread::sleep_for (chrono::milliseconds (1 )); printf ("Task:%d is done in thread:%d\n" , i, this_thread::get_id ()); }); } return 0 ; }