Skip to content

Instantly share code, notes, and snippets.

@tembleking
Created August 18, 2018 16:15
Show Gist options
  • Save tembleking/a61496f306b27b1bea809662a8b92cc8 to your computer and use it in GitHub Desktop.
Save tembleking/a61496f306b27b1bea809662a8b92cc8 to your computer and use it in GitHub Desktop.
Simple C++ implementation of Golang channels
/*
The MIT License (MIT)
Copyright (c) 2018 Federico Barcelona <tembleking@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#ifndef CHANNELS_CHAN_H
#define CHANNELS_CHAN_H
#include <queue>
#include <cstdint>
#include <mutex>
#include <condition_variable>
/*
Channels are the pipes that connect concurrent threads.
You can send values into channels from one thread and receive those values into another thread.
They have been created inspired in Golang channels: https://gobyexample.com/channels
*/
template<typename T>
class chan {
static_assert(std::is_default_constructible<T>::value && std::is_copy_constructible<T>::value,
"Channels only work with default constructible and copy constructible elements");
typedef std::unique_lock<std::recursive_mutex> lock;
std::queue<T> queue;
uint8_t count;
std::recursive_mutex mtx;
std::condition_variable_any cv;
bool open;
public:
chan(uint8_t size = 1) : count(size), open(true) {}
chan(const chan &) = delete; // Delete copy constructor
chan &operator=(chan const &) = delete; // Delete copy assignation
void put(T item) noexcept {
lock lck(mtx);
// Wait until its closed or until the count is higher than 0
cv.wait(lck, [this]() { return !open || count > 0; });
if (!open) return;
count--;
queue.emplace(item);
cv.notify_all();
}
T get() noexcept {
lock lck(mtx);
// Wait until its closed or the queue is not empty
cv.wait(lck, [this]() { return !open || !queue.empty(); });
if (!open) return T();
count++;
T item = queue.front();
queue.pop();
cv.notify_all();
return item;
}
bool isOpen() {
lock(mtx);
return open;
}
void close() {
lock(mtx);
open = false;
std::queue<T> empty;
std::swap(queue, empty); // clear the queue
cv.notify_all();
}
};
#endif //CHANNELS_CHAN_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment