Home > Backend Development > C++ > How to implement multi-threaded programming in C++ using STL?

How to implement multi-threaded programming in C++ using STL?

WBOY
Release: 2024-06-05 11:54:57
Original
792 people have browsed it

Using STL to implement multi-threaded programming in C++ involves: using std::thread to create threads. Protect shared resources using std::mutex and std::lock_guard. Use std::condition_variable to coordinate conditions between threads. This method supports concurrent tasks such as file copying, where multiple threads process file blocks in parallel.

如何在 C++ 中使用 STL 实现多线程编程?

How to use STL to implement multi-threaded programming in C++

STL (Standard Template Library) provides a powerful set of Concurrency primitives and containers can easily implement multi-threaded programming. This article demonstrates how to use key components in STL to create multi-threaded applications.

Using Threads

To create a thread, use std::thread Class:

std::thread t1(some_function);
t1.join(); // 等待线程完成
Copy after login

some_function is a function to be executed concurrently.

Mutexes and locks

Mutexes can be used to prevent multiple threads from accessing shared resources at the same time. Using std::mutex:

std::mutex m;
{
    std::lock_guard<std::mutex> lock(m);
    // 在此处访问共享资源
} // 解除 m 的锁定
Copy after login

Condition variable

Condition variable allows a thread to wait for a specific condition, such as when a shared resource is available. Using std::condition_variable:

std::condition_variable cv;
std::unique_lock<std::mutex> lock(m);
cv.wait(lock); // 等待 cv 信号
cv.notify_one(); // 唤醒一个等待线程
Copy after login

Practical case: multi-threaded file copy

The following code demonstrates how to use STL to implement multi-threaded file copy:

#include <fstream>
#include <iostream>
#include <thread>
#include <vector>

void copy_file(const std::string& src, const std::string& dst) {
    std::ifstream infile(src);
    std::ofstream outfile(dst);
    outfile << infile.rdbuf();
}

int main() {
    std::vector<std::thread> threads;
    const int num_threads = 4;

    // 创建线程池
    for (int i = 0; i < num_threads; ++i) {
        threads.emplace_back(copy_file, "input.txt", "output" + std::to_string(i) + ".txt");
    }

    // 等待所有线程完成
    for (auto& t : threads) {
        t.join();
    }

    std::cout << "Files copied successfully!" << std::endl;
    return 0;
}
Copy after login

The above is the detailed content of How to implement multi-threaded programming in C++ using STL?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template