In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "how to avoid thread generation and destruction in C++". Many people will encounter such a dilemma in the operation of actual cases. then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
CP.41: avoid thread generation and destruction as much as possible
Reason (reason)
Thread creation is expensive.
It is expensive to create threads.
Example (sample)
Void worker (Message m)
{
/ / process
}
Void dispatcher (istream& is)
{
For (Message m; is > > m;)
Run_list.push_back (new thread (worker, m))
}
A thread is generated for each message in the code, and run_list is most likely ready to destroy them at the end of the task.
As an improvement, we can prepare a set of pre-generated worker threads to process these messages.
Sync_queue work
Void dispatcher (istream& is)
{
For (Message m; is > > m;)
Work.put (m)
}
Void worker ()
{
For (Message m; m = work.get ();) {
/ / process
}
}
Void workers () / / set up worker threads (specifically 4 worker threads)
{
Joining_thread w1 {worker}
Joining_thread w2 {worker}
Joining_thread w3 {worker}
Joining_thread w4 {worker}
} Note (note)
If your system has a good thread pool, use it. If your system has a good message queue, use it.
If your system contains an excellent thread pool, use it. If your system contains an excellent message queue, use it.
This is the end of the content of "how C++ avoids thread generation and destruction". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.