2

Well I am tryng to have a queue which is concurrent, but concurrency_queue isn't standard C++ and it's for windows, linux doesn't have it. Is there anything for linux like this (with the same functions like in the windows equivalent?)?

Edit: This is needed to port this windows code to 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();
}

3 Answers 3

5

The Visual C++ concurrent_queue is actually based on the Intel Threading Building Block Library (If you open concurrent_queue.h header file in VC++ you will see an acknowledgement)

You can get the library from

http://threadingbuildingblocks.org/

The library will run on Linux as well.

1
  • which headers are specific to the queue in TBB? #include "tbb/concurrent_queue.h" I suppose? :)
    – user1182183
    Nov 28, 2012 at 7:33
3

I think threadpool does this or an unofficial Boost enhancement called lockfree and should by now be part of Boost::Atomics. I haven't use both but let us know if you have any luck.

1

I would suggest looking at https://github.com/romanek-adam/boost_locking_queue for the code and the article that goes with it at Implementing a Thread-Safe Queue using Condition Variables

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.