Skip to content

Commit

Permalink
Merge pull request cameron314#24 from benaryorg/patch-1
Browse files Browse the repository at this point in the history
syntax highlighting in README.md
  • Loading branch information
cameron314 committed Jan 7, 2016
2 parents baf8267 + 4921868 commit 65980a8
Showing 1 changed file with 37 additions and 34 deletions.
71 changes: 37 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,43 +32,46 @@ from working correctly.

Example:

using namespace moodycamel;

ReaderWriterQueue<int> q(100); // Reserve space for at least 100 elements up front

q.enqueue(17); // Will allocate memory if the queue is full
bool succeeded = q.try_enqueue(18); // Will only succeed if the queue has an empty slot (never allocates)
assert(succeeded);

int number;
succeeded = q.try_dequeue(number); // Returns false if the queue was empty

assert(succeeded && number == 17);

// You can also peek at the front item of the queue (consumer only)
int* front = q.peek(); // Returns nullptr if the queue was empty
assert(front == nullptr);

```cpp
using namespace moodycamel;

ReaderWriterQueue<int> q(100); // Reserve space for at least 100 elements up front

q.enqueue(17); // Will allocate memory if the queue is full
bool succeeded = q.try_enqueue(18); // Will only succeed if the queue has an empty slot (never allocates)
assert(succeeded);

int number;
succeeded = q.try_dequeue(number); // Returns false if the queue was empty

assert(succeeded && number == 17);

// You can also peek at the front item of the queue (consumer only)
int* front = q.peek(); // Returns nullptr if the queue was empty
assert(front == nullptr);
```
The blocking version has the exact same API, with the addition of a `wait_dequeue` method:
BlockingReaderWriterQueue<int> q;

std::thread reader([&]() {
int item;
for (int i = 0; i != 100; ++i) {
q.wait_dequeue(item);
}
});
std::thread writer([&]() {
for (int i = 0; i != 100; ++i) {
q.enqueue(i);
}
});
writer.join();
reader.join();

assert(q.size_approx() == 0);
```cpp
BlockingReaderWriterQueue<int> q;
std::thread reader([&]() {
int item;
for (int i = 0; i != 100; ++i) {
q.wait_dequeue(item);
}
});
std::thread writer([&]() {
for (int i = 0; i != 100; ++i) {
q.enqueue(i);
}
});
writer.join();
reader.join();
assert(q.size_approx() == 0);
```

Note that `wait_dequeue` will block indefinitely while the queue is empty; this
means care must be taken to only call `wait_dequeue` if you're sure another element
Expand Down

0 comments on commit 65980a8

Please sign in to comment.