linux

Linux C++如何利用多核处理器

小樊
45
2025-06-01 14:26:13
栏目: 编程语言

在Linux环境下使用C++编写多线程程序,以充分利用多核处理器的性能,可以通过以下几种方法实现:

1. 使用POSIX线程(Pthreads)

POSIX线程是Linux下标准的线程库,可以方便地创建和管理线程。

示例代码:

#include <pthread.h>
#include <iostream>
#include <vector>

// 线程函数
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 <iostream>
#include <thread>
#include <vector>

// 线程函数
void threadFunction(int threadId) {
    std::cout << "Thread " << threadId << " is running on core " << sched_getcpu() << std::endl;
}

int main() {
    const int numThreads = 4;
    std::vector<std::thread> 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_concurrencystd::thread::native_handle(C++11线程)来实现。

示例代码(POSIX线程):

#include <pthread.h>
#include <iostream>
#include <vector>
#include <sched.h>

// 设置线程亲和性
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 <iostream>
#include <thread>
#include <vector>
#include <sched.h>

// 设置线程亲和性
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<std::thread> 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 <iostream>
#include <omp.h>

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++编写多线程程序,充分利用多核处理器的性能。

0
看了该问题的人还看了