繁体   English   中英

在Linux上是否有Windows concurrency_queue.h等效文件?

[英]Is there a windows concurrency_queue.h equivalent on linux?

好吧,我正在尝试有一个并发的队列,但是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 ++ parallel_queue实际上是基于Intel线程构建模块库的(如果您在VC ++中打开parallel_queue.h头文件,则会看到一个确认)

你可以从

http://threadingbuildingblocks.org/

该库也将在Linux上运行。

我认为, 线程池做这个或非官方的加速增强叫lockfree和现在应该的一部分Boost::Atomics 我没用过,但如果您有运气,请告诉我们。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM