c++ - 在 Linux 上是否有等效的 Windows concurrency_queue.h?

标签 c++ linux multithreading concurrency thread-safety

好吧,我想有一个并发的队列,但是concurrency_queue不是标准的 C++,它适用于 Windows,Linux 没有。有没有像这样的 linux(具有与 Windows 等效功能相同的功能?)?

编辑: 这是将此 Windows 代码移植到 Linux 所必需的:

#include <concurrent_queue.h>

#ifdef defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
    #define SLEEP(x) { Sleep(x); }
    #include <windows.h>
    #include <process.h>
    #define OS_WINDOWS
    #define EXIT_THREAD() { _endthread(); }
    #define START_THREAD(a, b) { _beginthread( a, 0, (void *)( b ) ); }
#else
    #include <pthread.h>
    #define sscanf_s sscanf
    #define sprintf_s sprintf
    #define EXIT_THREAD() { pthread_exit( NULL ); }
    #define START_THREAD(a, b) {    pthread_t thread;\
                                pthread_create( &thread, NULL, a, (void *)( b ) ); }
#endif

using namespace std;
using namespace Concurrency;

struct QuedData
{
    int start;
    int end;
    int extraid;
    AMX * script;
    QuedData(){start = 0;end = 0;extraid = 0;script = NULL;}
    QuedData(int start_,int end_,int extraid_, AMX * script_){start = start_;end = end_;extraid = extraid_;script = script_;}
};

struct PassData //thanks to DeadMG for improvements.
{
    std::vector<cell> Paths;
    int extraid;
    AMX * script;
    cell MoveCost;
    PassData(){extraid = 0;script = NULL;MoveCost = 0;Paths.clear();}
    template<typename Iterator> PassData(Iterator begin, Iterator end, int extraid_, cell MoveCost_, AMX * script_)
        : Paths(begin, end)
    {extraid = extraid_;MoveCost = MoveCost_;script = script_;}
    ~PassData(){Paths.clear();}
};

concurrent_queue            <QuedData>          QueueVector;
concurrent_queue            <PassData>          PassVector;
PassData LocalPass;

void PLUGIN_CALL
    ProcessTick()
{
    if(PassVector.try_pop(LocalPass))
    {
        amx_Push(LocalPass.script, LocalPass.MoveCost);
        //blabla
    }
}

static cell AMX_NATIVE_CALL n_CalculatePath( AMX* amx, cell* params )
{
    QueueVector.push(QuedData(params[1],params[2],params[3],amx));
    return 1;
}

bool PLUGIN_CALL Load( void **ppData )
{
    START_THREAD( Thread::BackgroundCalculator, 0);
    return true;
}

QuedData RecievedData;
vector <cell>tbcway;
cell tbccostx;
#ifdef OS_WINDOWS
    void Thread::BackgroundCalculator( void *unused )
#else
    void *Thread::BackgroundCalculator( void *unused )
#endif
{
    while( true ){
        if(QueueVector.try_pop(RecievedData)){
            dgraph->findPath_r(xNode[RecievedData.start].NodeID ,xNode[RecievedData.end].NodeID,tbcway,tbccostx);
            PassVector.push(PassData(tbcway.begin(),tbcway.end(),RecievedData.extraid,tbccostx,RecievedData.script));
        }
        SLEEP(5);
    }
    EXIT_THREAD();
}

最佳答案

Visual C++ concurrent_queue 实际上是基于 Intel Threading Building Block Library (如果您在 VC++ 中打开 concurrent_queue.h 头文件,您将看到一个确认信息)

你可以从中获取库

http://threadingbuildingblocks.org/

该库也将在 Linux 上运行。

关于c++ - 在 Linux 上是否有等效的 Windows concurrency_queue.h?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13596485/

相关文章:

C++ 发送返回 SOCKET_ERROR

c++ - 通过套接字发送文件太慢

linux - 执行二进制文件一段时间的 Bash 脚本

java - ThreadPoolExecutor 在低负载时不收缩

c++ - 将(指向可变大小数组的指针)转换为(指向指针的指针)

c++ - 将 stdlib 函数与索引一起使用而不是迭代器?

linux - git sparse-checkout忽略特定的文件类型

linux - LKM - 无法编译模块 - 缺少头文件但安装了头包

c++ - 在 C++ 多线程程序中使用指向局部变量的指针

java - 从Java中的Runnable线程调用主线程