2012-11-28 2 views
1

Ну, я пытаюсь иметь очередь, которая является параллельной, но concurrency_queue не является стандартным C++, и это для Windows, linux его не имеет. Есть ли что-нибудь для linux, подобного этому (с теми же функциями, что и в эквиваленте Windows?)?Есть ли эквивалент windows concurrency_queue.h для linux?

Edit: Это необходимо, чтобы портировать окна кода в 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

Visual C++ concurrent_queue фактически основана на Intel Threading Building Block Library (Если вы заголовочный файл открыт concurrent_queue.h в VC++ вы увидите подтверждение)

вы можете получить библиотеку из

http://threadingbuildingblocks.org/

либерал rary будет работать и на Linux.

+0

Какие заголовки относятся к очереди в TBB? #include "tbb/concurrent_queue.h" Полагаю? :) –

3

Я думаю threadpool делает это или неофициальное повышение Повысьте под названием lockfree и теперь должно быть частью Boost::Atomics. Я не использую оба, но дайте нам знать, если вам повезет.

Смежные вопросы