Skip to content

Instantly share code, notes, and snippets.

@FlyingJester
Created July 7, 2014 00:54
Show Gist options
  • Select an option

  • Save FlyingJester/5c320058783ec5c94084 to your computer and use it in GitHub Desktop.

Select an option

Save FlyingJester/5c320058783ec5c94084 to your computer and use it in GitHub Desktop.
A cross platform concurrent queue header, including a simple implementation using Unix pipes when no existing implementation is present
#pragma once
#ifdef _MSC_VER
#include <concurrent_queue.h>
using concurrency::concurrent_queue;
#else
#ifdef USE_INTEL_TBB
#include <tbb/concurrent_queue.h>
using tbb::concurrent_queue;
#else
#include <unistd.h>
#include <cstdlib>
template<typename T>
class concurrent_queue {
int FD[2];
public:
concurrent_queue(){
pipe(FD);
}
~concurrent_queue(){
close(FD[0]);
close(FD[1]);
}
void push(const T &aP){
write(FD[1], &aP, sizeof(T));
}
bool try_pop(T &aTo){
return (read(FD[0], &aTo, sizeof(T))>0);
}
bool pop_if_present(T &aTo){
return try_pop(aTo);
}
size_t unsafe_size(){
uint64_t lAt = lseek(FD[0], 0, SEEK_CUR);
uint64_t rAt = lseek(FD[0], 0, SEEK_END);
lseek(FD[0], lAt, SEEK_SET);
return rAt;
}
};
#endif
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment