Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the ways in which threads can be started by Clear11?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "what are the ways of starting threads in Category 11". In daily operation, I believe that many people have doubts about the ways in which they start threads. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the questions about "how to start threads in Craft 11". Next, please follow the editor to study!

1. Function pointer mode

Starting a thread with a function pointer is the most primitive and basic way. Define the handler function ThreadFunction that implements the thread, and then when you start the thread, pass the function pointer to the constructor of the thread class.

After starting the thread, the following information is printed indicating that the thread processing function was called after the thread started.

Second, function object mode

The second way to start a thread is in the form of a function object, first creating a class ThreadOpeartor, then overloading the operator () operator, and finally passing it to the thread as a parameter to start in the form of a mock function.

Similarly, after running the program, the following information can be seen on the serial port, indicating that the function object way to start the thread is effective.

III. Lambda mode

The third way to start a thread is in the form of the lambda function, the lambda function is an anonymous function, which starts the thread as an argument to the thread.

After compiling and running the program, you can also verify that the lambda function is called when the thread starts.

Fourth, the pointer mode of class function

The fourth way to start a thread is in the form of a class function pointer, which first defines a class member function among the class members, and then uses the function pointer as a parameter to start the thread.

Judging from the running result, the started thread successfully called the member function of the class.

5. Std::bind mode

The fifth way to start a thread is in the form of stb::bind, first with the class member function as an argument to stb::bind, and then stb::bind as a thread parameter to start the thread.

After compiling successfully, the result of running the program is as follows, which also shows that it is feasible to start the thread in std::bind mode.

Advantages and disadvantages of start-up mode

Function pointer is an old way to start threads. In linux system, pthread library is supported and belongs to the idea of process-oriented. If the system you are using does not support Cellular 11, you can use this way.

The way the function object starts the thread can define more details within the class, such as defining member variables and encapsulating more logical processing details.

The way the lambda function starts the thread is that the code is relatively simple, because the function is embedded and has good readability, it is suitable for scenarios where the logic of the thread processing function is not very complex.

The way the class function pointer starts the thread, the thread's processing function belongs to the class, so you need to pay attention to its life cycle.

The way std::bind starts the thread, which can be combined with std::function to achieve function callback.

7. Thread application scenario

This section will start the thread in the form of a function object to show how to apply it to practical applications.

Assuming that there are different commands that need to be executed in the thread, first implement the command base class

Then create the execution command class JWorkThread, overload the operator () operator, and execute the command internally.

Then there is a class JRunCmd that inherits the base class JCommand, which is called as follows

At this point, the study on "what is the way to start threads in Cellular 11" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report