Overall

This library provides few classes to better deal with the producer/consumer pattern.

Shared Queue

Defined in header <haz/SharedQueue.hpp>

template<
    class T,
    std::size_t N
> class SharedQueue;

SharedQueue is an fixed size queue with additional functions to controle multiple access at the same time. The elements are stored contiguously in a buffer of size N, but are not in order. The iterators will iterate through the collection in order of insertion though.

Complexity: - Insertion at the beginning and removal at the end of elements: constant O(1) - Random Access: constant O(1)

Template parameters

T: The type of the elements N: The maximum number of element in the buffer

It is recommended to use a power of 2 for the size S so the index is computed faster.

Members functions

Element access

at Access specified element with bounds checking
back Access the last element
front Access the first element
operator [] Access specified element without bounds checking

Iterators

begin, cbegin Returns an iterator to the beginning
end, cend Returns an iterator to the end
rbegin, crbegin Returns a reverse iterator to the beginning
end, cend Returns a reverse iterator to the end

Capacity

capacity Returns the maximum number of elements
empty Checks whether the container is empty
max_size Returns the maximum number of elements
size Returns the number of elements

Modifiers

assign Replaces the content
clear Clear the content
emplace_back Construct an element in-place at the end
pop_front Pop the first element
push_back Insert an element to the end
swap Swaps the contents

Non-member function

operator ==, !=, <=, >=, <, > Lexicograpically compares the values
std::swap specialize the std::swap algorithm

Capacity

capacity Returns the maximum number of elements
empty Checks whether the container is empty
max_size Returns the maximum number of elements
size Returns the number of elements

haz::SharedQueue::capacity

constexpr size_type capacity() const noexcept;

Returns the maximum number of element in the container.

Parameters

(none)

Return value

Maximum number of elements.

Complexity

Constant.

Notes

For haz::SharedQueue<T, N>, the value returned is S.
It's a synonym of max_size.

Example

#include <haz/SharedQueue.hpp>
#include <iostream>

int main() {
    haz::SharedQueue<int, 10> queue;
    std::cout << "The capacity of the queue is " << queue.capacity() << '\n';
}

Output:

The capacity of the queue is 10

See also

size Returns the number of elements
max_size Returns the maximum number of elements

haz::SharedQueue::max_size

constexpr size_type max_size() const noexcept;

Returns the maximum number of element in the container.

Parameters

(none)

Return value

Maximum number of elements.

Complexity

Constant.

Notes

For haz::SharedQueue<T, N>, the value returned is S.
It's a synonym of capacity.

Example

#include <haz/SharedQueue.hpp>
#include <iostream>

int main() {
    haz::SharedQueue<int, 10> queue;
    std::cout << "The maximum size of the queue is " << queue.max_size() << '\n';
}

Output:

The maximum size of the queue is 10

See also

size Returns the number of elements
capacity Returns the maximum number of elements

haz::SharedQueue::size

constexpr size_type size() const noexcept;

Returns the number of elements in the container.

Parameters

(none)

Return value

The number of elements in a SharedQueue.

Complexity

Constant.

Example

#include <haz/SharedQueue.hpp>
#include <iostream>

int main() {
    haz::SharedQueue<int, 10> queue{ 0, 1, 2 };
    std::cout << "The queue contains " << queue.size() << " elements\n";
}

Output:

The queue contains 3 elements

See also

empty Checks whether the container is empty
max_size Returns the maximum number of elements

haz::SharedQueue::empty

constexpr bool empty() const noexcept;

Checks whether the container has no elements.

Parameters

(none)

Return value

true if the element is empty, otherwise false.

Complexity

Constant.

Example

#include <haz/SharedQueue.hpp>
#include <iostream>

int main() {
    haz::SharedQueue<int, 10> empty_queue;
    haz::SharedQueue<int, 10> not_empty_queue{ 0, 1, 2 };
    std::cout << "empty_queue is empty ? " << empty_queue.empty() << '\n';
    std::cout << "not_empty_queue is empty ? " << not_empty_queue.empty() << '\n';
}

Output:

empty_queue is empty ? 1
not_empty_queue is empty ? 0

See also

size Returns the number of elements

Element Access

at Access specified element with bounds checking
back Access the last element
front Access the first element
operator [] Access specified element without bounds checking

haz::SharedQueue::front

constexpr const_reference front() const noexcept;
constexpr reference front() noexcept;

Returns the first element of the container. Calling front on an empty container is undefined.

Parameters

(none)

Return value

Reference to the first element.

Complexity

Constant.

Notes

The element returned is the first element that has been pushed but not poped yet.

Example

#include <haz/SharedQueue.hpp>
#include <iostream>

int main() {
    haz::SharedQueue<int, 10> queue{ 0, 1, 2 };
    std::cout << "The first element is " << queue.front() << '\n';
}

Output:

The first element is 0

See also

back Access the last element

haz::SharedQueue::back

constexpr const_reference back() const noexcept;
constexpr reference back() noexcept;

Returns the last element of the container. Calling back on an empty container is undefined.

Parameters

(none)

Return value

Reference to the last element.

Complexity

Constant.

Notes

The element returned is the last element that has been pushed.

Example

#include <haz/SharedQueue.hpp>
#include <iostream>

int main() {
    haz::SharedQueue<int, 10> queue{ 0, 1, 2 };
    std::cout << "The last element is " << queue.back() << '\n';
}

Output:

The last element is 2

See also

front Access the first element

haz::SharedQueue::at

constexpr const_reference at(size_type index) const;
constexpr reference at(size_type index);

Returns the number a reference to the element at the index specified. If the index is not within the range [0, size()), an exception of type std::out_of_range is thrown.

Parameters

index index of the element to return

Return value

Reference to the requested element.

Exception

std::out_of_range if index >= size().

Complexity

Constant.

Example

#include <haz/SharedQueue.hpp>
#include <iostream>

int main() {
    haz::SharedQueue<int, 10> queue{ 0, 1, 2 };
    std::cout << "The second element is " << queue.at(1) << '\n';
}

Output:

The second element is 1

See also

operator [] Access specified element without bounds checking

haz::SharedQueue::operator[]

constexpr const_reference operator[](size_type index) const noexcept;
constexpr reference operator[](size_type index) noexcept;

Returns the number a reference to the element at the index specified.

Parameters

index index of the element to return

Return value

Reference to the requested element.

Complexity

Constant.

Example

#include <haz/SharedQueue.hpp>
#include <iostream>

int main() {
    haz::SharedQueue<int, 10> queue{ 0, 1, 2 };
    std::cout << "The second element is " << queue[1] << '\n';
}

Output:

The second element is 1

See also

at Access specified element with bounds checking

Modifier

assign Replaces the content
clear Clear the content
emplace_back Construct an element in-place at the end
pop_front Pop the first element
push_back Insert an element to the end
swap Swaps the contents

haz::SharedQueue::clear

template<typename InputIt>
constexpr void assign(InputIt first, InputIt last) noexcept(/* see below */);           (1)
constexpr void assign(size_type n, T const& value) noexcept(/* see below */);           (2)
constexpr void assign(std::initilializer_list<T> list) noexcept(/* see below */);       (3)

Replaces the content of the container.
    (1) Copy each element in the range [first, last) to the container
    (2) Replaces the content with n copies of value
    (3) Replaces the content with those in the list

Parameters

n the new size of the container
value the value to initialize the elements with
first, last the range to copy the elements from
list initiliazer list to copy from

Return value

(none)

Exception

noexcept specification: noexcept(is_nothrow_constructible_v<decltype(*std::declval<InputIt>)>) for (1)
noexcept(is_nothrow_copy_constructible_v<T>) for (2-3)

Complexity

    (1) Linear in distance between first and last
    (2) Linear in n
    (3) Linear in list.size()

Example

#include <haz/SharedQueue.hpp>
#include <iostream>

int main() {
    haz::SharedQueue<int, 10> queue{ 0, 1, 2 };

    std::cout << "Contents:\n";
    for(auto i : queue) {
        std::cout << i << ", ";
    }
    std::cout << '\n';

    queue.assign(5, 1337);

    std::cout << "Contents:\n";
    for(auto i : queue) {
        std::cout << i << ", ";
    }
    std::cout << '\n';
}

Output:

Contents:
0, 1, 2,
Contents:
1337, 1337, 1337, 1337, 1337,

See also

(constructor) Constructs the SharedQueue

haz::SharedQueue::clear

constexpr void clear() noexcept;

Remove all elements of the container. After that size() returns 0. Invalidate all iterators, references and pointers.

Parameters

(none)

Return value

(none)

Complexity

Constant if the elements are trivially destructible, otherwise linear in the size of the container.

Example

#include <haz/SharedQueue.hpp>
#include <iostream>

int main() {
    haz::SharedQueue<int, 10> queue{ 0, 1, 2 };
    std::cout << "Size before : " << queue.size() << '\n';
    queue.clear();
    std::cout << "Size after : " << queue.size() << '\n';
}

Output:

Size before : 3
Size afer : 0

See also

pop_front Pop the first element

haz::SharedQueue::push_back

void push_back(T const& value);     (1)
void push_back(T&& value);          (2)

Insert the element at the end of the container.
    (1) The new element is initialized as a copy of value
    (2) value is moved into the new element

All iterators past-the-end, end included, are invalidated. No reference are invalidated. If the container is full, i.e. size() == max_size() then the behaviour is undefined.

Parameters

value the value of the element to append

Type requirement

T must meet the requirements of CopyInsertable in order to use overload (1).
T must meet the requirements of MoveInsertable in order to use overload (2).

Return value

(none)

Complexity

Constant.

Exception

If an exception is thrown, this function has no effect (strong exception guarantee).

Example

#include <haz/SharedQueue.hpp>
#include <iostream>
#include <string>

int main() {
    haz::SharedQueue<std::string, 10> queue;
    
    queue.push_back("abc");
    
    std::string s = "def";
    queue.push_back(std::move(s));

    std::cout << "Contents:\n";
    for(auto const& i : queue) {
        std::cout << std::quoted(i) << '\n';
    }
}

Output:

Contents:
"abc"
"def"

See also

emplace_back Construct an element in-place at the end

haz::SharedQueue::pop_front

constexpr void pop_front() noexcept;

Removes the first element of the container. If there are no elements in the container, the behaviour is undefined. Iterator and reference to the erased element are invalidated. The other are not unless the container is now empty.

Parameters

(none)

Return value

(none)

Complexity

Constant.

Example

#include <haz/SharedQueue.hpp>
#include <iostream>

int main() {
    haz::SharedQueue<int, 10> queue{ 0, 1, 2, 3 };
    std::cout << "Contents:\n";
    for(auto i : queue) {
        std::cout << i << '\n';
    }

    std::cout << "Pop 2 values\n";
    queue.pop_front();
    queue.pop_front();

    std::cout << "Contents:\n";
    for(auto i : queue) {
        std::cout << i << ", ";
    }
    std::cout << '\n';
}

Output:

Contents:
0, 1, 2, 3,
Pop 2 values
Contents:
2, 3,

See also

clear Clear the content

haz::SharedQueue::emplace_back

template<typename...Args>
reference emplace_back(Args&&... args);

Construct a new element at the end of the container. The element is constructed in place using placement-new. The arguments args... are forwarded to the constructor using std::forward<Args>(args).... All iterators past-the-end, end included, are invalidated. No reference are invalidated. If the container is full, i.e. size() == max_size() then the behaviour is undefined.

Parameters

args arguments to forward to the constructor of the element

Type requirement

T (the container's element type) must meet the requirement of EmplaceConstructible.

Return value

Reference to the inserted element.

Complexity

Constant.

Exception

If an exception is thrown, this function has no effect (strong exception guarantee).

Example

#include <haz/SharedQueue.hpp>
#include <iostream>
#include <string>

struct Person {
    std::string name;
    unsigned age;

    Person(std::string name, unsigned age) : name(name), age(age) {
        std::cout << "A person is constructed!\n";
    }

    Person(Person&& other) : name(std::move(other.name)), age(other.age) {
        std::cout << "A person is moved!\n";
    }

    Person& operator=(Person const& other) = default;
};

int main() {
    haz::SharedQueue<Person, 10> queue;

    std::cout << "emplace_back:\n";
    queue.emplace_back("John", 32);

    std::cout << "\npush_back:\n";
    queue.push_back(Person("Bob", 47));

    std::cout << "\nContents:\n";
    for(auto const& person : queue) {
        std::cout << person.name << " is " << person.age << '\n';
    }
}

Output:

emplace_back: 
A person is constructed!

push_back: 
A person is constructed!
A person is moved!

Contents:
John is 32
Bob is 47

See also

push_back Insert an element to the end

haz::SharedQueue::swap

constexpr void swap(SharedQueue& other) noexcept(/* see below */);

Exchanges the contents of the container with those of other. Does not cause iterators and references to associate with the other container.

Parameters

other Container to exchange the contents with

Return value

(none)

Exception

noexcept specification: noexcept(is_nothrow_swappable_v<T>)

Complexity

Linear in the size of the container.

Example

#include <haz/SharedQueue.hpp>
#include <iostream>

int main() {
    haz::SharedQueue<int, 10> queue_a{ 0, 1, 2, 3 };
    haz::SharedQueue<int, 10> queue_b{ 4, 5, 6 };
    std::cout << "Contents of A:\n";
    for(auto i : queue_a) {
        std::cout << i << ", ";
    }
    std::cout << '\n';
    std::cout << "Contents of B:\n";
    for(auto i : queue_b) {
        std::cout << i << ", ";
    }
    std::cout << '\n';

    std::cout << "Swap\n";
    queue_a.swap(queue_b);

    std::cout << "Contents of A:\n";
    for(auto i : queue_a) {
        std::cout << i << ", ";
    }
    std::cout << '\n';
    std::cout << "Contents of B:\n";
    for(auto i : queue_b) {
        std::cout << i << ", ";
    }
    std::cout << '\n';
}

Output:

Contents of A:
0, 1, 2, 3,
Contents of B:
4, 5, 6,
Swap
Contents of A:
4, 5, 6,
Contents of B:
0, 1, 2, 3,

See also

clear Clear the content

Iterators

begin, cbegin Returns an iterator to the beginning
end, cend Returns an iterator to the end
rbegin, crbegin Returns a reverse iterator to the beginning
end, cend Returns a reverse iterator to the end

haz::SharedQueue::begin, haz::SharedQueue::cbegin

constexpr iterator begin() noexcept();
constexpr const_iterator begin() const noexcept();
constexpr const_iterator cbegin() const noexcept();

Returns an iterator to the first element of the container. If the container is empty, the returned iterator will be equal to end()

Parameters

(none)

Return value

Iterator to the first element.

Complexity

Constant.

Example

#include <haz/SharedQueue.hpp>
#include <iostream>

int main() {
    haz::SharedQueue<int, 10> queue{ 0, 1, 2, 3 };
    for(typename haz::SharedQueue<int, 10>::iterator it = queue.begin(); it != queue.end(); ++it)
        std::cout << *it << ", ";
    std::cout << '\n';
}

Output:

0, 1, 2, 3,

See also

end, cend Returns an iterator to the end

haz::SharedQueue::end, haz::SharedQueue::cend

constexpr iterator end() noexcept();
constexpr const_iterator end() const noexcept();
constexpr const_iterator cend() const noexcept();

Returns an iterator to the element following the last element of the container. This element acts as a placeholder; attempting to access it results in undefined behaviour, it is not dereferencable.

Parameters

(none)

Return value

Iterator to the element following the last element.

Complexity

Constant.

Example

#include <haz/SharedQueue.hpp>
#include <iostream>

int main() {
    haz::SharedQueue<int, 10> queue{ 0, 1, 2, 3 };
    for(typename haz::SharedQueue<int, 10>::iterator it = queue.begin(); it != queue.end(); ++it)
        std::cout << *it << ", ";
    std::cout << '\n';
}

Output:

0, 1, 2, 3,

See also

begin, cbegin Returns an iterator to the beginning

haz::SharedQueue::rbegin, haz::SharedQueue::crbegin

constexpr reverse_iterator rbegin() noexcept();
constexpr const_reverse_iterator rbegin() const noexcept();
constexpr const_reverse_iterator crbegin() const noexcept();

Returns an iterator to the first element of the reversed container. It corresponds to the last element of the non-reversed container. If the container is empty, the returned iterator will be equal to rend()

Parameters

(none)

Return value

Reverse iterator to the first element.

Complexity

Constant.

Example

#include <haz/SharedQueue.hpp>
#include <iostream>

int main() {
    haz::SharedQueue<int, 10> queue{ 0, 1, 2, 3 };
    for(typename haz::SharedQueue<int, 10>::reverse_iterator it = queue.rbegin(); it != queue.rend(); ++it)
        std::cout << *it << ", ";
    std::cout << '\n';
}

Output:

3, 2, 1, 0,

See also

rend, crend Returns a reverse iterator to the end

haz::SharedQueue::rend, haz::SharedQueue::crend

constexpr reverse_iterator rend() noexcept();
constexpr const_reverse_iterator rend() const noexcept();
constexpr const_reverse_iterator crend() const noexcept();

Returns a reverse iterator to the element following the last element of the reversed container. It corresponds to the element preceding the first element of the non-reversed container. This element acts as a placeholder; attempting to access it results in undefined behaviour, it is not dereferencable.

Parameters

(none)

Return value

reverse iterator to the element following the last element.

Complexity

Constant.

Example

#include <haz/SharedQueue.hpp>
#include <iostream>

int main() {
    haz::SharedQueue<int, 10> queue{ 0, 1, 2, 3 };
    for(typename haz::SharedQueue<int, 10>::reverse_iterator it = queue.rbegin(); it != queue.rend(); ++it)
        std::cout << *it << ", ";
    std::cout << '\n';
}

Output:

3, 2, 1, 0,

See also

rbegin, crbegin Returns a reverse iterator to the beginning

Non-member fucntions

operator ==, !=, <=, >=, <, > Lexicograpically compares the values
std::swap specialize the std::swap algorithm

operator==, !=, <, <=, >, >= (haz::SharedQueue)

template<typename T, std::size_t N>
constexpr bool operator==(haz::SharedQueue<T, N> const& lhs, haz::SharedQueue<T, N> const& rhs) noexcept();         (1)
template<typename T, std::size_t N>
constexpr bool operator!=(haz::SharedQueue<T, N> const& lhs, haz::SharedQueue<T, N> const& rhs) noexcept();         (2)
template<typename T, std::size_t N>
constexpr bool operator<(haz::SharedQueue<T, N> const& lhs, haz::SharedQueue<T, N> const& rhs) noexcept();         (3)
template<typename T, std::size_t N>
constexpr bool operator<=(haz::SharedQueue<T, N> const& lhs, haz::SharedQueue<T, N> const& rhs) noexcept();         (4)
template<typename T, std::size_t N>
constexpr bool operator>(haz::SharedQueue<T, N> const& lhs, haz::SharedQueue<T, N> const& rhs) noexcept();         (5)
template<typename T, std::size_t N>
constexpr bool operator>=(haz::SharedQueue<T, N> const& lhs, haz::SharedQueue<T, N> const& rhs) noexcept();         (6)

Compare the contents of two queue.     (1-2) Checks if the contents of lhs and rhs are equal, that is, whether each element in lhs compares equals with the element in rhs at the same position.     (3-6) Compares the contents of lhs and rhs lexicographically. The comparaison is performed by a function equivalent to std::lexicographical_compare.

Parameters

lhs, rhs Containers whose contents to compare

Type requirement

T must meet the requirements of EqualityComparable in order to use overload (1-2).
T must meet the requirements of LessThanComparable in order to use overload (3-6).

Return value

(1) true if the contents of the containers are equal, false otherwise.
(2) true if the contents of the containers are not equal, false otherwise.
(3) true if the contents of the lhs are lexicographically less than the contents of rhs, false otherwise.
(4) true if the contents of the lhs are lexicographically less than or equal the contents of rhs, false otherwise.
(5) true if the contents of the lhs are lexicographically greater than the contents of rhs, false otherwise.
(6) true if the contents of the lhs are lexicographically greater than or equal the contents of rhs, false otherwise.

Complexity

Constant.

std::swap (haz::SharedQueue)

template<typename T, std::size_t N>
void swap(haz::SharedQueue<T, N> const& lhs, haz::SharedQueue<T, N> const& rhs) noexcept(/* see below */);

Specialize the std::swap algorithm for haz::SharedQueue. Swaps the contents of lhs and rhs. Calls lhs.swap(rhs).

Parameters

lhs, rhs Containers whose contents to swap

Return value

(none)

Complexity

Constant.

Exceptions

noexcept specification: noexcept(noexcept(lhs.swap(rhs)))

Example

#include <haz/SharedQueue.hpp>
#include <iostream>
#include <algorithm>

int main() {
    haz::SharedQueue<int, 10> queue_a{ 0, 1, 2, 3 };
    haz::SharedQueue<int, 10> queue_b{ 4, 5, 6 };
    std::cout << "Contents of A:\n";
    for(auto i : queue_a) {
        std::cout << i << ", ";
    }
    std::cout << '\n';
    std::cout << "Contents of B:\n";
    for(auto i : queue_b) {
        std::cout << i << ", ";
    }
    std::cout << '\n';

    std::cout << "Swap\n";
    std::swap(queue_a, queue_b);

    std::cout << "Contents of A:\n";
    for(auto i : queue_a) {
        std::cout << i << ", ";
    }
    std::cout << '\n';
    std::cout << "Contents of B:\n";
    for(auto i : queue_b) {
        std::cout << i << ", ";
    }
    std::cout << '\n';
}

Output:

Contents of A:
0, 1, 2, 3,
Contents of B:
4, 5, 6,
Swap
Contents of A:
4, 5, 6,
Contents of B:
0, 1, 2, 3,

See also

swap Swaps the contents

acquire_lock

acquire_lock

wait_not_empty

wait_not_full

interrupt_all