阅读量:3
在Linux环境下使用C++编写多线程程序,以充分利用多核处理器的性能,可以通过以下几种方法实现:
1. 使用POSIX线程(Pthreads)
POSIX线程是Linux下标准的线程库,可以方便地创建和管理线程。
示例代码:
#include
#include
#include
// 线程函数
void* threadFunction(void* arg) {
int threadId = *(static_cast<int*>(arg));
std::cout << "Thread " << threadId << " is running on core " << sched_getcpu() << std::endl;
return nullptr;
}
int main() {
const int numThreads = 4;
pthread_t threads[numThreads];
int threadIds[numThreads];
// 创建线程
for (int i = 0; i < numThreads; ++i) {
threadIds[i] = i;
if (pthread_create(&threads[i], nullptr, threadFunction, &threadIds[i]) != 0) {
std::cerr << "Failed to create thread "<< i << std::endl;
return 1;
}
}
// 等待线程结束
for (int i = 0; i < numThreads; ++i) {
pthread_join(threads[i], nullptr);
}
return 0;
}
2. 使用C++11线程库
C++11引入了标准线程库,提供了更简洁和安全的线程管理方式。
示例代码:
#include
#include
#include
// 线程函数
void threadFunction(int threadId) {
std::cout << "Thread " << threadId << " is running on core " << sched_getcpu() << std::endl;
}
int main() {
const int numThreads = 4;
std::vector threads;
// 创建线程
for (int i = 0; i < numThreads; ++i) {
threads.emplace_back(threadFunction, i);
}
// 等待线程结束
for (auto& t : threads) {
t.join();
}
return 0;
}
3. 绑定线程到特定核心
为了更好地利用多核处理器,可以将线程绑定到特定的CPU核心上。可以使用pthread_setaffinity_np函数(POSIX线程)或std::thread::hardware_concurrency和std::thread::native_handle(C++11线程)来实现。
示例代码(POSIX线程):
#include
#include
#include
#include
// 设置线程亲和性
void setThreadAffinity(pthread_t thread, int coreId) {
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(coreId, &cpuset);
if (pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset) != 0) {
std::cerr << "Failed to set thread affinity" << std::endl;
}
}
// 线程函数
void* threadFunction(void* arg) {
int threadId = *(static_cast<int*>(arg));
setThreadAffinity(pthread_self(), threadId);
std::cout << "Thread " << threadId << " is running on core " << sched_getcpu() << std::endl;
return nullptr;
}
int main() {
const int numThreads = 4;
pthread_t threads[numThreads];
int threadIds[numThreads];
// 创建线程
for (int i = 0; i < numThreads; ++i) {
threadIds[i] = i;
if (pthread_create(&threads[i], nullptr, threadFunction, &threadIds[i]) != 0) {
std::cerr << "Failed to create thread "<< i << std::endl;
return 1;
}
}
// 等待线程结束
for (int i = 0; i < numThreads; ++i) {
pthread_join(threads[i], nullptr);
}
return 0;
}
示例代码(C++11线程):
#include
#include
#include
#include
// 设置线程亲和性
void setThreadAffinity(std::thread& thread, int coreId) {
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(coreId, &cpuset);
if (pthread_setaffinity_np(thread.native_handle(), sizeof(cpu_set_t), &cpuset) != 0) {
std::cerr << "Failed to set thread affinity" << std::endl;
}
}
// 线程函数
void threadFunction(int threadId) {
setThreadAffinity(std::this_thread::get_id(), threadId);
std::cout << "Thread " << threadId << " is running on core " << sched_getcpu() << std::endl;
}
int main() {
const int numThreads = 4;
std::vector threads;
// 创建线程
for (int i = 0; i < numThreads; ++i) {
threads.emplace_back(threadFunction, i);
}
// 等待线程结束
for (auto& t : threads) {
t.join();
}
return 0;
}
4. 使用OpenMP
OpenMP是一种用于共享内存并行编程的标准API,可以方便地实现并行计算。
示例代码:
#include
#include
int main() {
const int numThreads = 4;
// 设置线程数
omp_set_num_threads(numThreads);
#pragma omp parallel for
for (int i = 0; i < numThreads; ++i) {
std::cout << "Thread " << omp_get_thread_num() << " is running on core " << sched_getcpu() << std::endl;
}
return 0;
}
通过以上方法,可以在Linux环境下使用C++编写多线程程序,充分利用多核处理器的性能。
以上就是关于“Linux C++如何利用多核处理器”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm