Created
July 7, 2014 00:54
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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