相关疑难解决方法(0)

将数据从一个线程传递到另一个线程的最快方法

我正在使用boost spsc_queue将我的东西从一个线程移动到另一个线程.它是我软件中的关键位置之一,所以我希望尽快完成.我写了这个测试程序:

#include <boost/lockfree/spsc_queue.hpp>
#include <stdint.h>

#include <condition_variable>
#include <thread>

const int N_TESTS = 1000;

int results[N_TESTS];

boost::lockfree::spsc_queue<int64_t, boost::lockfree::capacity<1024>> testQueue;

using std::chrono::nanoseconds;
using std::chrono::duration_cast;

int totalQueueNano(0);
int totalQueueCount(0);

void Consumer() {
    int i = 0;
    int64_t scheduledAt;
    while (i < N_TESTS - 1) {
        while (testQueue.pop(scheduledAt)) {
            int64_t dequeuedAt = (duration_cast<nanoseconds>(
                    std::chrono::high_resolution_clock::now().time_since_epoch())).count();
            auto diff = dequeuedAt - scheduledAt;
            totalQueueNano += diff;
            ++totalQueueCount;
            results[i] = diff;
            ++i;
        }
    }
    for (int i = 0; i < N_TESTS; i++) {
        printf("%d …
Run Code Online (Sandbox Code Playgroud)

c++ performance multithreading boost lock-free

8
推荐指数
2
解决办法
1258
查看次数

标签 统计

boost ×1

c++ ×1

lock-free ×1

multithreading ×1

performance ×1