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 is thread multithreading in C language programming?

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article shows you how thread multithreading is in C language programming, the content is concise and easy to understand, it can definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Thread creation and termination

Four header files have been introduced to support multithreaded programming in the new standard, namely, and.

This headline mainly declares two classes, std::atomic and std::atomic_flag, as well as a set of C-style atomic types and functions for C-compatible atomic operations.

This header file mainly declares the std::thread class, and the std::this_thread namespace is also in the header file.

This header file mainly declares classes related to mutex, including std::mutex series classes, std::lock_guard, std::unique_lock, and other types and functions.

This header file mainly declares classes related to conditional variables, including std::condition_variable and std::condition_variable_any.

The header file mainly declares two Provider classes, std::promise and std::package_task, and two Future classes, std::future and std::shared_future, as well as some related types and functions. The std::async () function is declared in this header file.

# include # include void F1 (int n) {for (int I = 0; I)

< 5; ++i) { std::cout lock(); _Owns = true; } _NODISCARD bool try_lock() { // try to lock the mutex _Validate(); _Owns = _Pmtx->

Try_lock (); return (_ Owns);} template _ NODISCARD bool try_lock_for (const chrono::duration& _ Rel_time) {/ / try to lock mutex for _ Rel_time _ Validate (); _ Owns = _ Pmtx- > try_lock_for (_ Rel_time) Return (_ Owns);} template _ NODISCARD bool try_lock_until (const chrono::time_point& _ Abs_time) {/ / try to lock mutex until _ Abs_time _ Validate (); _ Owns = _ Pmtx- > try_lock_until (_ Abs_time) Return (_ Owns);} _ NODISCARD bool try_lock_until (const xtime * _ Abs_time) {/ / try to lock the mutex until _ Abs_time _ Validate (); _ Owns = _ Pmtx- > try_lock_until (_ Abs_time); return (_ Owns) } void unlock () {/ / try to unlock the mutex if (! _ Pmtx | |! _ Owns) _ THROW (system_error (_ STD make_error_code (errc::operation_not_permitted)); _ Pmtx- > unlock () _ Owns = false;} void swap (unique_lock& _ Other) noexcept {/ / swap with _ Other _ STD swap (_ Pmtx, _ Other._Pmtx); _ STD swap (_ Owns, _ Other._Owns) } _ Mutex * release () noexcept {/ / disconnect _ Mutex * _ Res = _ Pmtx; _ Pmtx = nullptr; _ Owns = false; return (_ Res) } _ NODISCARD bool owns_lock () const noexcept {/ / return true if this object owns the lock return (_ Owns);} explicit operator bool () const noexcept {/ / return true if this object owns the lock return (_ Owns) } _ NODISCARD _ Mutex * mutex () const noexcept {/ / return pointer to managed mutex return (_ Pmtx);} private: _ Mutex * _ Pmtx; bool _ Owns Void _ Validate () const {/ / check if the mutex can be locked if (! _ Pmtx) _ THROW (system_error (_ STD make_error_code (errc::operation_not_permitted) If (_ Owns) _ THROW (system_error (_ STD make_error_code (errc::resource_deadlock_would_occur);}}

There is a pointer to a lock with _ Mutex * _ Pmtx;; left-value copy construction and assignment are not allowed, but right-value copy construction and assignment can be used during function calls. So it can be used with the condition variable: cv.wait (lock); / / can be passed in as a function argument

Example:

For a code segment running in a multithreaded environment, we need to consider whether there is a race condition. If there is a race condition, we say that the code segment is not thread-safe and cannot be run directly in a multithreaded environment. For such a code segment, we often call it critical section resources. For critical section resources, we need to ensure that they are executed as atomic operations in a multithreaded environment. You need to use the mutually exclusive operation-lock mechanism between threads, and the thread class library also provides more lightweight atomic operation classes based on CAS operations.

When unlocked:

# include # include / / the atomic class provided by the C++ 11 thread library # include / / the header file of the C++ thread class library # include int count = 0scramble / thread function void sumTask () {/ / each thread adds for to count 10 times (int I = 0; I < 10; + + I) {count++; std::this_thread::sleep_for (std::chrono::milliseconds (10)) }} int main () {/ / create 10 threads in the container std::vector vec; for (int I = 0; I < 10; + + I) {vec.push_back (std::thread (sumTask));} / wait for thread execution to complete for (unsigned int i = 0; I < vec.size (); + + I) {vector [I] .join ();} / all child threads run end 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.

Share To

Development

Wechat

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

12
Report