Skip to content

Commit

Permalink
Add usage example
Browse files Browse the repository at this point in the history
  • Loading branch information
dadrian committed Aug 22, 2016
1 parent d48627c commit a709e16
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
David Adrian <[email protected]>
49 changes: 49 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,51 @@
# cchannel
Golang-style channels for C++

### Origins

This was used internally by some [Censys](https://censys.io)-related code. This
is a dump of the code with some minor changes so that it's usable outside of
Censys.

### Building

Easiest is to just copy both source files in the build system for your project.
If this is popular, at some point, I might add a Makefile to this repository
for testing purposes, but for now I'm just supplying the code as is.

### Usage

Dummy example asynchronously processes a vector, and writes the output to a
file.

```cpp
Channel<std::string> to_write;
std::fstream output_file("output.txt");

// Write output to a file as it comes through the queue
std::thread output_thread([&]() {
for (auto it = to_write.range(); it.valid(); ++it) {
if (it->size() > 0) {
output_file << *it;
}
}
output_file.flush();
});

// Read input and send through queue
std::vector<Object> input_data = get_input();
for (const auto& obj : input_data) {
// Do some work
std::string = do_something(obj);

// Sending releases ownership
to_write.send(std::move(s));
}

output_thread.join();
```
### Support
I'll take pull requests, but I'm not going to provide much other support.

0 comments on commit a709e16

Please sign in to comment.