详解C++ thread用法总结
什么是C++ thread?
C++ thread是一个多线程库,用于在C++中实现多线程编程。多线程是指在同一时间内执行多个线程,从而实现并发执行的目的。C++ thread为程序员提供了创建、启动、等待、终止线程以及互斥锁、条件变量等并发编程工具。
C++ thread用法总结
创建和启动线程
在C++中创建和启动线程可以使用std::thread
,使用方法如下:
#include <iostream>
#include <thread>
void threadFunction() {
std::cout << "This is a new thread." << std::endl;
}
int main() {
std::thread t(threadFunction);
t.join();
return 0;
}
这段代码创建了一个名为t
的线程,启动了threadFunction()
函数,并且使用join()
函数等待线程结束。执行结果如下:
This is a new thread.
等待线程结束
std::thread
提供了join()
函数,用于等待线程结束。可以使用下面两种方式实现等待多个线程结束:
方式一:
std::thread t1(threadFunction1);
std::thread t2(threadFunction2);
t1.join();
t2.join();
方式二:
std::vector<std::thread> threads;
threads.push_back(std::thread(threadFunction1));
threads.push_back(std::thread(threadFunction2));
for (auto& th : threads) {
th.join();
}
传递参数到线程函数
可以通过lambda函数传递参数到线程函数:
#include <iostream>
#include <thread>
void threadFunction(int n) {
std::cout << "The parameter is " << n << std::endl;
}
int main() {
int n = 10;
std::thread t([](int n) {
threadFunction(n);
}, n);
t.join();
return 0;
}
执行结果如下:
The parameter is 10
处理线程函数的返回值
可以使用std::future
和std::async()
来实现处理线程函数的返回值。
#include <iostream>
#include <thread>
#include <future>
int threadFunction(int n) {
return n * n;
}
int main() {
int n = 10;
std::future<int> result = std::async(threadFunction, n);
int res = result.get();
std::cout << "Result is " << res << std::endl;
return 0;
}
执行结果如下:
Result is 100
示例
示例一:计算1到10000的和
#include <iostream>
#include <thread>
#include <vector>
#include <numeric>
void sum(std::vector<int>::iterator start, std::vector<int>::iterator end, int& result) {
result = std::accumulate(start, end, 0);
}
int main() {
std::vector<int> data(10000);
std::iota(std::begin(data), std::end(data), 1);
int res1, res2, res3, res4;
std::thread t1(sum, std::begin(data), std::begin(data) + 2500, std::ref(res1));
std::thread t2(sum, std::begin(data) + 2500, std::begin(data) + 5000, std::ref(res2));
std::thread t3(sum, std::begin(data) + 5000, std::begin(data) + 7500, std::ref(res3));
std::thread t4(sum, std::begin(data) + 7500, std::end(data), std::ref(res4));
t1.join();
t2.join();
t3.join();
t4.join();
int result = res1 + res2 + res3 + res4;
std::cout << "The sum of 1 to 10000 is " << result << std::endl;
return 0;
}
执行结果如下:
The sum of 1 to 10000 is 50005000
示例二:生产者-消费者模型
#include <iostream>
#include <queue>
#include <mutex>
#include <thread>
#include <chrono>
std::queue<int> myQueue;
std::mutex myMutex;
std::condition_variable myCond;
void producer() {
for (int i = 0; i < 10; i++) {
std::lock_guard<std::mutex> myLockGuard(myMutex);
myQueue.push(i);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
myCond.notify_one();
}
}
void consumer() {
for (int i = 0; i < 10; i++) {
std::unique_lock<std::mutex> myUniqueLock(myMutex);
myCond.wait(myUniqueLock, []{return !myQueue.empty();});
std::cout << "Consume " << myQueue.front() << std::endl;
myQueue.pop();
}
}
int main() {
std::thread t1(producer);
std::thread t2(consumer);
t1.join();
t2.join();
return 0;
}
执行结果如下:
Consume 0
Consume 1
Consume 2
Consume 3
Consume 4
Consume 5
Consume 6
Consume 7
Consume 8
Consume 9
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:详解C++ thread用法总结 - Python技术站