In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this article, the editor introduces in detail "how to use the standard thread library in C++". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to use the standard thread library in C++" can help you solve your doubts. Let's follow the editor's ideas slowly and deeply, together to learn new knowledge.
1. Create thread execution asynchronously
We can create a thread directly and asynchronously through the async function, which is relatively simple, and the result of thread execution can be obtained directly with future.
# include # include / / the corresponding function bool thread_func (int x) {return true;} int main () {int inputNum = 65547; std::future future = std::async (thread_func, inputNum); bool ret = future.get (); getchar ();} 2. Prevent thread conflicts by using mutexes
There is generally no thread safety problem when reading content synchronously between threads, but conflicts are easy to occur if the same content is written synchronously between threads. For example, if each thread executes once, it will add up the number of global executions. if multiple threads execute operations at the same time without locking at the time of writing, this may lead to repeated accumulation of execution times.
# include # include # include std::mutex mtx; int count=0; void print_block (int n) {mtx.lock (); count++; / / do somethings mtx.unlock ();} int main () {std::thread thread1 (print_block, 50); std::thread thread2 (print_block, 50); thread1.join (); thread2.join (); getchar (); return 0 } 3. Semaphore is used to control the running of threads.
The conditional variable (condition_variable) is used to control the running of the thread. If the conditional variable waits when the thread starts, it will block the running of the thread until the corresponding notification is sent by the conditional variable. By using conditional variables, we can control the running of the thread and prevent the thread from running idle and consuming computing resources.
# include # include std::mutex mtx;std::condition_variable cv; void print_id (int id) {std::unique_lock lck (mtx); cv.wait (lck); std::cout
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.