2

好吧,我试图拥有一个并发队列,但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();
}
4

3 回答 3

5

Visual C++ concurrent_queue 实际上基于英特尔线程构建模块库(如果您在 VC++ 中打开 concurrent_queue.h 头文件,您将看到一个确认)

你可以从

http://threadingbuildingblocks.org/

该库也将在 Linux 上运行。

于 2012-11-28T02:39:03.457 回答
3

我认为线程池可以做到这一点,或者是一个名为 lockfree非官方 Boost 增强功能,现在应该是. 我没有同时使用这两种方法,但如果你有任何运气,请告诉我们。Boost::Atomics

于 2012-11-28T01:54:30.910 回答
1

我建议查看https://github.com/romanek-adam/boost_locking_queue的代码以及使用条件变量实现线程安全队列的文章

于 2012-11-28T02:03:23.913 回答