Using smart pointer for critical section - c++

What is the drawbacks or errors in the following approach to use non-owning std::unique_ptr having custom deleter to arrange a critical section?
#include <memory>
#include <shared_mutex>
#include <optional>
#include <variant>
#include <cassert>
struct Data
{
std::optional<int> i;
};
struct DataLocker
{
std::variant<std::unique_lock<std::shared_mutex>, std::shared_lock<std::shared_mutex>> lock;
void operator () (const Data *)
{
std::visit([] (auto & lock) { if (lock) lock.unlock(); }, lock);
}
};
struct DataHolder
{
std::unique_ptr<Data, DataLocker> getLockedData()
{
return {&data, {std::unique_lock<std::shared_mutex>{m}}};
}
std::unique_ptr<const Data, DataLocker> getLockedData() const
{
return {&data, {std::shared_lock<std::shared_mutex>{m}}};
}
private :
mutable std::shared_mutex m;
Data data;
};
#include <iostream>
#include <thread>
int main()
{
DataHolder d;
auto producer = [&d]
{
d.getLockedData()->i = 123;
};
auto consumer = [&d = std::as_const(d)]
{
for (;;) {
if (const auto i = d.getLockedData()->i) {
std::cout << *i << std::endl;
return;
}
}
};
std::thread p(producer);
std::thread c(consumer);
p.join();
c.join();
}
One corner case, when writer reset()s a pointer and never destruct std::unique_ptr itself is covered by adding unlock to deleter's operator ().

Related

create a queue with multiple data types

I want to be able to have a queue class that can accept "any" preferred data type wether that is a CURL type or just a std::string type. Queue of strings or queue of curl handles if you will.
#pragma once
#include <iostream>
#include <queue>
#include <string>
#include <mutex>
#include <condition_variable>
#include <chrono>
#include "queue_safe.h"
void SafeQueue::initialize() {
/* initialize a std::queue */
safe_queue = {};
}
std::string SafeQueue::get() {
std::unique_lock<std::mutex> condition_lock(queue_lock);
if (safe_queue.empty()) {
if (ready.wait_for(condition_lock, std::chrono::seconds(20)) == std::cv_status::timeout) {
/* timeout was reached, no items left */
return std::string();
}
/* if a timeout was not hit, but the queue was empty previously this will execute */
std::string element = safe_queue.front();
safe_queue.pop();
return element;
}
else {
/* not empty, return an element */
std::string element = safe_queue.front();
safe_queue.pop();
return element;
}
/* do not need a return value here since empty HAS to be either true/false */
}
void SafeQueue::put(std::string& element) {
std::lock_guard<std::mutex> put_guard(queue_lock);
safe_queue.push(element);
ready.notify_one();
}
uint8_t SafeQueue::empty() {
std::lock_guard<std::mutex> empty_guard(queue_lock);
if (safe_queue.size() == 0) {
return 1;
}
return 0;
}
queue.cpp
#pragma once
#include <string>
#include <mutex>
#include <queue>
#include <condition_variable>
class SafeQueue {
public:
std::condition_variable ready;
std::mutex queue_lock;
std::queue<std::string> safe_queue;
uint8_t empty();
void initialize();
void put(std::string& element);
std::string get();
};
queue.h
This works fine for a single type (string). But how could i adapt it to allow any wanted type like mentioned above?
If you know the types you want to store in the queue up front, you can use std::variant. Being a type-safe union, std::variant guarantees that it contains one of a list of types; it also has better performance than std::any since a variant exists in automatic storage.
For example, this variant type can store one of std::string, int or double:
using MyVariant = std::variant<std::string, int, double>;
You can use a queue of MyVariant in your class SafeQueue. Finally, use a visitor to process each item in your queue.
I've reworked you class here:
#include <iostream>
#include <iomanip>
#include <mutex>
#include <queue>
#include <condition_variable>
#include <variant>
#include <vector>
using MyVariant = std::variant<std::string, int, double>;
template<class> inline constexpr bool always_false_v = false;
class SafeQueue {
public:
std::condition_variable ready;
std::mutex queue_lock;
std::queue<MyVariant> safe_queue;
uint8_t empty();
void initialize();
void put(MyVariant element);
MyVariant get();
};
void SafeQueue::initialize() {
/* initialize a std::queue */
safe_queue = {};
}
MyVariant SafeQueue::get() {
std::unique_lock<std::mutex> condition_lock(queue_lock);
if (safe_queue.empty()) {
if (ready.wait_for(condition_lock, std::chrono::seconds(20)) == std::cv_status::timeout) {
/* timeout was reached, no items left */
return std::string();
}
/* if a timeout was not hit, but the queue was empty previously this will execute */
auto element = safe_queue.front();
safe_queue.pop();
return element;
}
else {
/* not empty, return an element */
auto element = safe_queue.front();
safe_queue.pop();
return element;
}
/* do not need a return value here since empty HAS to be either true/false */
}
void SafeQueue::put(MyVariant element) {
{
std::lock_guard<std::mutex> put_guard(queue_lock);
safe_queue.push(std::move(element));
}
ready.notify_one();
}
uint8_t SafeQueue::empty() {
std::lock_guard<std::mutex> empty_guard(queue_lock);
if (safe_queue.empty()) {
return 1;
}
return 0;
}
int main()
{
SafeQueue q;
q.put("hello");
q.put(1);
q.put(2.3);
//Use visitor pattern to print elements from queue
while (!q.empty()) {
auto elem = q.get();
std::visit([](auto&& arg) {
using T = std::decay_t<decltype(arg)>;
if constexpr (std::is_same_v<T, std::string>)
std::cout << "std::string with value " << std::quoted(arg) << '\n';
else if constexpr (std::is_same_v<T, int>)
std::cout << "int with value " << arg << '\n';
else if constexpr (std::is_same_v<T, double>)
std::cout << "double with value " << arg << '\n';
else
static_assert(always_false_v<T>, "non-exhaustive visitor!");
}, elem);
}
}
Output:
std::string with value "hello"
int with value 1
double with value 2.3
Demo
If you want to have a single object corresponding to a single type. You must use Template Classes:
template <typename T>
class SafeQueue {
public:
std::condition_variable ready;
std::mutex queue_lock;
std::queue<T> safe_queue;
uint8_t empty();
void initialize();
void put(T& element);
T get();
};
But if you need a queue with multi-ply types, you could use std::any:
class SafeQueue {
public:
std::condition_variable ready;
std::mutex queue_lock;
std::queue<std::any> safe_queue;
uint8_t empty();
void initialize();
void put(std::any& element);
std::any get();
};
But it's better to have a structure for that to store the underlying type for better communication with your class API's.

(C++) How to use Payload Object to imiplement thread pool?

I saw this very well implemented thread pool: https://github.com/progschj/ThreadPool. I am wondering whether I can use a payload object instead. The idea is that instead of using a function pointer, use an object to describe the payload, which always contains a run function and a promise. The main thread then wait on the future of the promise.
Here is what I got:
#include <iostream>
#include <queue>
#include <thread>
#include <future>
#include <condition_variable>
#include <mutex>
class GenericPayload {
protected:
std::promise <int> m_returnCode;
public:
virtual void run() = 0;
std::future <int> getFuture() {
return m_returnCode.get_future();
}
};
class MyPayload:public GenericPayload {
private:
int m_input1;
int m_input2;
int m_result;
public:
MyPayload(int input1, int input2):m_input1(input1), m_input2(input2) {}
void run() {
m_result = m_input1 * m_input2;
m_returnCode.set_value(0);
}
int getResult() {
return m_result;
}
};
class ThreadPool {
private:
std::queue <GenericPayload *> payloads;
std::mutex queue_mutex;
std::condition_variable cv;
std::vector< std::thread > workers;
bool stop;
public:
ThreadPool(size_t threads)
: stop(false)
{
for(size_t i = 0;i<threads;++i)
workers.emplace_back(
[this]
{
for(;;)
{
GenericPayload *payload;
{
std::unique_lock<std::mutex> lock(this->queue_mutex);
this->cv.wait(lock,
[this]{ return this->stop || !this->payloads.empty(); });
if(this->stop)
return;
payload = this->payloads.front();
this->payloads.pop();
}
payload->run();
}
}
);
}
void addPayLoad (GenericPayload *payload) {
payloads.push(payload);
}
~ThreadPool()
{
{
std::unique_lock<std::mutex> lock(queue_mutex);
stop = true;
}
cv.notify_all();
for(std::thread &worker: workers)
worker.join();
}
};
int main() {
MyPayload myPayload(3, 5);
ThreadPool threadPool(2);
std::future <int> returnCode = myPayload.getFuture();
threadPool.addPayLoad(&myPayload);
returnCode.get();
std::cout << myPayload.getResult() << std::endl;
}
Is this the right way to do it though? I had to pass a pointer to the payload because 1. I want GenericPayload to be abstract and 2. std::promise is not copyable. Thx for any feedback.

Misuse of conditional variable

Could you please review and suggest what is wrong with this code?
It either crashes on line 21 (cond_var_.wait(lock); in the gc_thread_proc()) or locks on line 56 (lock.lock(); in release()).
#include <condition_variable>
#include <deque>
#include <functional>
#include <mutex>
#include <thread>
#include <vector>
#include <iostream>
class stream {
std::deque<int> pending_cleanups_;
std::mutex mut_{};
bool continue_{true};
std::thread gc_worker_;
std::condition_variable cond_var_;
void gc_thread_proc() {
while (true) {
std::vector<int> events_to_clean;
std::unique_lock<std::mutex> lock(mut_);
while (pending_cleanups_.empty() && continue_) {
cond_var_.wait(lock);
}
if (!continue_) {
break;
}
std::move(std::begin(pending_cleanups_), std::end(pending_cleanups_), std::back_inserter(events_to_clean));
pending_cleanups_.clear();
}
}
public:
explicit stream() : gc_worker_(&stream::gc_thread_proc, this) {}
void register_pending_event(int val) {
{
std::lock_guard<std::mutex> lock_guard(mut_);
pending_cleanups_.push_back(val);
}
cond_var_.notify_one();
}
void release() {
std::unique_lock<std::mutex> lock(mut_);
if (!continue_) {
return;
}
continue_ = false;
lock.unlock();
cond_var_.notify_one();
gc_worker_.join();
lock.lock();
pending_cleanups_.clear();
}
~stream() { release(); }
};
int main() {
int N=100000;
while(N--) {
std::cout << ".";
stream s;
}
std::cout << "ok";
return 0;
}
Changing order of members makes this problem go away - when cond_var_ is put before the gc_worker_ problem doesn't reproduce. But I guess it doesn't fix it just hides it somehow...
non-static data members are initialized in order of declaration in the class definition: https://en.cppreference.com/w/cpp/language/initializer_list
3) Then, non-static data members are initialized in order of declaration in the class definition.
In your case, since your std::thread member is initialized to start executing in its constructor, cv may not be initialized when it's used in gc_thread_proc. A command way to have a std::thread member is to move assign it in the class contructor, i.e.
class stream {
std::thread gc_worker_;
std::condition_variable cond_var_;
public:
stream(): {
gc_work = std::move(std::thread(&stream::gc_thread_proc, this));
}
};

Locking copy operation of std::shared_ptr inside lambda

For this sample code:
#include <iostream>
#include <thread>
#include <mutex>
#include <memory>
struct A
{
int _i;
A(int i):_i(i)
{
std::cout<<"A() "<<_i<<std::endl;
}
~A()
{
std::cout<<"~A() "<<_i<<std::endl;
}
void Print()
{
std::cout<<"Print() "<<_i<<std::endl;
}
};
struct B
{
std::shared_ptr<A> Asp;
std::mutex AspMutex;
void SetA()
{
static int i = 0;
std::unique_lock<std::mutex> lock(AspMutex);
Asp = std::make_shared<A>(i);
}
void AccessA1()
{
std::shared_ptr<A> aspCopy;
{
std::unique_lock<std::mutex> lock(AspMutex);
aspCopy = Asp;
}
(*aspCopy).Print();
}
void AccessA2()
{
auto aspCopy = [&]()
{
std::unique_lock<std::mutex> lock(AspMutex);
return Asp;
}();
(*aspCopy).Print();
}
void AccessA3()
{
(*[&]()
{
std::unique_lock<std::mutex> lock(AspMutex);
return Asp;
}()
).Print();
}
};
int main()
{
B b;
b.SetA();
std::thread t([&]{b.SetA();});
b.AccessA1();
b.AccessA2();
b.AccessA3();
t.join();
}
I'm curious if c++17 (or later) standard will guarantee that A::Access1 and A::Access2 methods are thread safe (copy of std::shared_ptr will be protected by lock).
Yes. The lock makes A::Access1 and A::Access2 thread safe with concurrent SetA. This is still true in C++17.

Accessing counter from two threads

I have a counter that is being incremented from one thread. In the main thread, I basically print it out by calling data member of a class. In the below code, nothing is being printed out.
#include <iostream>
#include <thread>
#include <windows.h>
#include <mutex>
std::mutex mut;
class Foo
{
public:
Foo(const int& m) : m_delay(m), m_count(0)
{}
void update()
{
std::cout << "count: " << this->m_count << std::endl;
}
void operator()()
{
while (true){
mut.lock();
m_count++;
mut.unlock();
Sleep(m_delay);
}
}
private:
int m_delay;
int m_count;
};
Foo *obj = new Foo(200);
int main()
{
std::thread *t = new std::thread(*obj);
t->join();
while(true)
{
obj->update();
Sleep(10);
}
return 0;
}
The problem with the original code is that this copies the Foo object:
std::thread *t = new std::thread(*obj);
That means that the increments happen to the copy, and so the value in the original Foo never changes, and so when main prints it out (if you move the misplaced join()) the value is always the same.
A solution is to use a reference not a copy:
std::thread *t = new std::thread(std::ref(*obj));
You also need to protect the read of the variable by the mutex (or use std::atomic<int> for the counter) to avoid undefined behaviour caused by concurrently reading and writing a non-atomic variable.
You should also stop using mut.lock() and mut.unlock() directly, use a scoped lock instead.
There's also no need to create things on the heap unnecessarily, overusing new is a bad habit of people who learnt Java and C# first.
You can also make the code portable by replacing the Windows-specific Sleep call with standard C++.
A correct version would be:
#include <iostream>
#include <thread>
#include <chrono>
#include <mutex>
std::mutex mut;
class Foo
{
public:
Foo(std::chrono::milliseconds m) : m_delay(m), m_count(0)
{}
void update()
{
int count = 0;
{
std::lock_guard<std::mutex> lock(mut);
count = m_count;
}
std::cout << "count: " << count << std::endl;
}
void operator()()
{
while (true)
{
{
std::lock_guard<std::mutex> lock(mut);
m_count++;
}
std::this_thread::sleep_for(m_delay);
}
}
private:
std::chrono::milliseconds m_delay;
int m_count;
};
Foo obj(std::chrono::milliseconds(200));
int main()
{
std::thread t(std::ref(obj));
while(true)
{
obj.update();
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
t.join();
return 0;
}
Alternatively, use an atomic variable so you don't need the mutex:
#include <iostream>
#include <thread>
#include <chrono>
#include <atomic>
class Foo
{
public:
Foo(std::chrono::milliseconds m) : m_delay(m), m_count(0)
{}
void update()
{
std::cout << "count: " << m_count << std::endl;
}
void operator()()
{
while (true)
{
m_count++;
std::this_thread::sleep_for(m_delay);
}
}
private:
std::chrono::milliseconds m_delay;
std::atomic<int> m_count;
};
Foo obj(std::chrono::milliseconds(200));
int main()
{
std::thread t(std::ref(obj));
while(true)
{
obj.update();
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
t.join();
return 0;
}