
随着计算机技术的发展和硬件性能的提升,多线程技术已经成为了现代编程的必备技能。C++是一门经典的编程语言,也提供了许多强大的多线程技术。本文将介绍C++中的一些多线程优化技巧,以帮助读者更好地应用多线程技术。
一、使用 std::thread
C++11引入了 std::thread,将多线程技术直接集成到了标准库中。使用 std::thread 创建一个新的线程非常简单,只需要传递一个函数指针即可。例如:
#include <thread>
#include <iostream>
void hello()
{
std::cout << "Hello World!";
}
int main()
{
std::thread t(hello);
t.join();
return 0;
}上面的代码创建了一个新的线程t,执行hello函数,并等待线程t完成。注意,线程的创建和销毁需要一定的开销,因此需要合理地使用 std::thread。
立即学习“C++免费学习笔记(深入)”;
二、使用 std::async
std::async是另一个方便的多线程技术,它可以异步地执行一个函数,并返回一个 std::future 对象。使用 std::async 可以更方便地管理异步任务的执行和结果获取。例如:
#include <future>
#include <iostream>
int add(int a, int b)
{
return a + b;
}
int main()
{
auto async_result = std::async(add, 1, 2);
std::cout << async_result.get();
return 0;
}上面的代码调用add函数异步地计算1+2,并使用 std::future 对象管理计算结果的获取。需要注意的是,std::async 默认使用 std::launch::async 策略,会在新的线程中执行函数。如果希望使用 std::launch::deferred 策略,则需要手动指定。但是,使用 std::launch::deferred 策略会导致函数在调用 std::future::get() 时才执行,因此需要根据具体情况进行选择。
三、使用 std::condition_variable
在多线程编程中,线程之间需要进行通信和同步,而 std::condition_variable 可以很好地实现这个目的。使用 std::condition_variable 可以让一个线程等待另一个线程的某个条件成立,从而实现线程之间的同步。例如:
#include <condition_variable>
#include <mutex>
#include <thread>
#include <iostream>
std::mutex mutex;
std::condition_variable cv;
bool ready = false;
void producer()
{
std::unique_lock<std::mutex> lock(mutex);
// wait for the condition to become true
cv.wait(lock, [] { return ready; });
std::cout << "Producer done." << std::endl;
}
void consumer()
{
std::this_thread::sleep_for(std::chrono::seconds(1));
ready = true;
std::cout << "Consumer done." << std::endl;
cv.notify_one();
}
int main()
{
std::thread t1(producer);
std::thread t2(consumer);
t1.join();
t2.join();
return 0;
}上面的代码创建了两个线程t1和t2,其中t1在等待一个条件变量ready变为true之前一直处于等待状态,而t2则在等待1秒后设置条件变量为true,并通知t1。需要注意的是,使用 std::condition_variable 必须配合std::mutex一起使用,以防止多个线程同时访问条件变量的问题。
四、使用线程池
在大量需要创建并运行的短期任务的情况下,经常会使用线程池以提高程序的性能。线程池维护一定数量的线程并管理任务的分配和执行。使用线程池可以避免频繁创建和销毁线程的额外开销,同时可以充分利用多核CPU的优势。例如:
#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <vector>
#include <queue>
#include <functional>
class ThreadPool
{
public:
ThreadPool(std::size_t numThreads = std::thread::hardware_concurrency())
{
for (std::size_t i = 0; i < numThreads; ++i)
{
pool.emplace_back([this] {
while (!stop)
{
std::function<void()> task;
{
std::unique_lock<std::mutex> lock{ mutex };
condition.wait(lock, [this] { return stop || !tasks.empty(); });
if (stop && tasks.empty()) return;
task = std::move(tasks.front());
tasks.pop();
}
task();
}
});
}
}
~ThreadPool()
{
{
std::unique_lock<std::mutex> lock{ mutex };
stop = true;
}
condition.notify_all();
for (auto& worker : pool)
{
worker.join();
}
}
template <typename F, typename... Args>
auto enqueue(F&& f, Args&&... args)
-> std::future<typename std::result_of<F(Args...)>::type>
{
using return_type = typename std::result_of<F(Args...)>::type;
auto task = std::make_shared<std::packaged_task<return_type()>>(
std::bind(std::forward<F>(f), std::forward<Args>(args)...));
std::future<return_type> future = task->get_future();
{
std::unique_lock<std::mutex> lock{ mutex };
if (stop) throw std::runtime_error("enqueue on stopped ThreadPool");
tasks.emplace([task](){ (*task)(); });
}
condition.notify_one();
return future;
}
private:
std::vector<std::thread> pool;
std::queue<std::function<void()>> tasks;
std::mutex mutex;
std::condition_variable condition;
bool stop = false;
};
void hello()
{
std::cout << "Hello World!" << std::endl;
}
int add(int a, int b)
{
return a + b;
}
int main()
{
{
ThreadPool pool;
auto f1 = pool.enqueue(hello);
auto f2 = pool.enqueue(add, 1, 2);
std::cout << f2.get() << std::endl;
}
return 0;
}上面的代码定义了一个ThreadPool类,其中包含多个线程和一个任务队列。线程池不断从任务队列中取出任务并执行,直到队列为空或者线程池停止。使用 ThreadPool::enqueue 方法将任务加入任务队列,并返回一个 std::future 对象来管理任务执行的结果。
总的来说,C++提供了多种多线程技术来帮助开发者发挥多核CPU的性能,并能够更灵活地管理线程和任务。开发者应该合理使用这些技术来优化程序性能。
以上就是C++中的多线程优化技巧的详细内容,更多请关注php中文网其它相关文章!
c++怎么学习?c++怎么入门?c++在哪学?c++怎么学才快?不用担心,这里为大家提供了c++速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号