Giter VIP home page Giter VIP logo

threadsafequeue's Introduction

ThreadSafeQueue

Header-only thread-safe queue. Compiles with C++11 compatible compilers. Tested with gcc 4.7-9.0 and Visual Studio 2012.

Relevant methods:

namespace codepi{

template <class T>
class ThreadSafeQueue{
public:
  // constructor - option to use stack instead of queue
  ThreadSafeQueue(bool useStack=false);

  // inserts onto back of queue  
  void enqueue(T t);       // supports move, copies if needed
  
  // retrieves from front of queue (blocks until success)
  T dequeue();
  
  // dequeue with timeout in seconds (returns true if successful)
  bool dequeue(double timeout_sec, T& returnVal);

  // basic methods  
  size_t size();
  bool  empty();
  void  clear();

  ...
};
}

Example usage:

#include <thread>
#include "ThreadSafeQueue.h"

using namespace std;
using namespace codepi;

void populate(ThreadSafeQueue<int>& q){
  for(int i=0;;i++){
    q.enqueue(i);
    this_thread::sleep_for(chrono::seconds(1));
  }
}

int main(){
  // threaded queue
  ThreadSafeQueue<int> q;

  // kick off generator thread
  thread t(populate, ref(q));

  // receive loop
  while(1){
      cout << q.dequeue() << endl;
  }
}

Move example:

vector<int> vec1(1000000);      // large object that supports move
ThreadSafeQueue<vector<int>> q;
q.enqueue(move(vec));           // moves vector onto queue without copy
vector<int> vec2 = q.dequeue(); // moves vector into vec2 without copy

ThreadSafeStack:

ThreadSafeQueue can also be used as a stack (first in, last out).

bool useStack = true;
ThreadSafeQueue<int> s(useStack);
s.enqueue(1);
s.enqueue(2);
std::cout << s.dequeue() << " " << s.dequeue() << "\n";
// prints: 2 1

threadsafequeue's People

Contributors

codepi avatar

Stargazers

 avatar

Forkers

vic4key

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.