在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
参考链接: thread:构造线程 #include <iostream> // std::cout #include <thread> // std::thread void thr_function1() { for (int i = 0; i != 10; ++i) { std::cout << "thread 1 print " << i << std::endl; } } void thr_function2(int n) { std::cout << "thread 1 print " << n << std::endl; } int main() { std::thread t1(thr_function1); // spawn new thread that calls foo() std::thread t2(thr_function2, 111); // spawn new thread that calls bar(0) std::cout << "main, foo and bar now execute concurrently...\n"; // synchronize threads: t1.join(); // pauses until first finishes 主线程等待t1线程结束 t2.join(); // pauses until second finishes 主线程等待t2线程结束 std::cout << "thread 1 and htread 2 completed.\n"; return 0; } class thread member: http://www.cplusplus.com/reference/thread/thread/ Member types get_id //Thread id (public member type ) native_handle_type //Native handle type (public member type ) Member functions: Construct thread (public member function ) Thread destructor (public member function ) operator= // Move-assign thread (public member function ) get_id // Get thread id (public member function ) joinable //Check if joinable (public member function ) join //Join thread (public member function ) detach //Detach thread (public member function ) swap //Swap threads (public member function ) native_handle //Get native handle (public member function ) hardware_concurrency [static] //Detect hardware concurrency (public static member function ) 多线程变量安全 std::mutex 互斥量: https://zh.cppreference.com/w/cpp/thread/mutex #include <iostream> // std::cout #include <thread> // std::thread #include <mutex> // std::mutex std::mutex mtx; // mutex for critical section void print_block(int n, char c) { // critical section (exclusive access to std::cout signaled by locking mtx): // mtx.try_lock //尝试锁定互斥,若互斥不可用,则返回 mtx.lock(); //锁定互斥,若互斥不可用则阻塞 for (int i = 0; i<n; ++i) { std::cout << c; } std::cout << '\n'; mtx.unlock(); //解开互斥 } int main() { std::thread th1(print_block, 50, '*'); std::thread th2(print_block, 50, '$'); th1.join(); th2.join(); return 0; } mutex类4种 recursive_mutex: https://zh.cppreference.com/w/cpp/thread/recursive_mutex time_mutex: //https://zh.cppreference.com/w/cpp/thread/timed_mutex #include <iostream> // std::cout #include <chrono> // std::chrono::milliseconds #include <thread> // std::thread #include <mutex> // std::timed_mutex std::timed_mutex mtx; void fireworks() { // waiting to get a lock: each thread prints "-" every 200ms: while (!mtx.try_lock_for(std::chrono::milliseconds(200))) { std::cout << "-"; } // got a lock! - wait for 1s, then this thread prints "*" std::this_thread::sleep_for(std::chrono::milliseconds(1000)); std::cout << "*\n"; mtx.unlock(); } int main () { std::thread threads[10]; // spawn 10 threads: for (int i=0; i<10; ++i) threads[i] = std::thread(fireworks); for (auto& th : threads) th.join(); return 0; } Lock 类(两种) #include <iostream> // std::cout #include <thread> // std::thread #include <mutex> // std::mutex, std::lock_guard, std::adopt_lock std::mutex mtx; // mutex for critical section void print_thread_id(int id) { mtx.lock(); std::lock_guard<std::mutex> lck(mtx, std::adopt_lock); //= mtx.lock() 且在lck 析构时,mtk.unlock std::cout << "thread #" << id << '\n'; } int main() { std::thread threads[10]; // spawn 10 threads: for (int i = 0; i<10; ++i) threads[i] = std::thread(print_thread_id, i + 1); for (auto& th : threads) th.join(); return 0; } **** scope_lock:构造时是否加锁是可选的(不加锁时假定当前线程已经获得锁的所有权),析构时自动释放锁,所有权不可转移,对象生存期内不允许手动加锁和释放锁。
class LogFile { std::mutex _mu; ofstream f; public: LogFile() { f.open("log.txt"); } ~LogFile() { f.close(); } void shared_print(string msg, int id) { std::unique_lock<std::mutex> guard(_mu);//如果 guard(_mu, std::defer_lock); 表示不上锁 //do something 1 guard.unlock(); //临时解锁 //do something 2 guard.lock(); //继续上锁 // do something 3 f << msg << id << endl; cout << msg << id << endl; // 结束时析构guard会临时解锁 // 这句话可要可不要,不写,析构的时候也会自动执行 // guard.ulock(); } };
类是同步原语 //https://zh.cppreference.com/w/cpp/thread/condition_variable #include <iostream> // std::cout #include <thread> // std::thread #include <mutex> // std::mutex, std::unique_lock #include <condition_variable> // std::condition_variable std::mutex mtx; // 全局互斥锁. std::condition_variable cv; // 全局条件变量. bool ready = false; // 全局标志位. void do_print_id(int id) { std::unique_lock <std::mutex> lck(mtx); while (!ready) // 如果标志位不为 true, 则等待... cv.wait(lck); // 当前线程被阻塞, 当全局标志位变为 true 之后,此外还有 wait for ,wait until 等语句 // 线程被唤醒, 继续往下执行打印线程编号id. std::cout << "thread " << id << '\n'; } void go() { std::unique_lock <std::mutex> lck(mtx); ready = true; // 设置全局标志位为 true. cv.notify_all(); // 唤醒所有线程. //notify_one 通知一个等待的线程 } int main() { std::thread threads[10]; // spawn 10 threads: for (int i = 0; i < 10; ++i) threads[i] = std::thread(do_print_id, i); std::cout << "10 threads ready to race...\n"; go(); // go! for (auto & th:threads) th.join(); return 0; } 结果: 10 threads ready to race... |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论