init
Some checks failed
Docker. / Ubuntu (push) Has been cancelled
User-agent updater. / User-agent (push) Failing after 15s
Lock Threads / lock (push) Failing after 10s
Waiting for answer. / waiting-for-answer (push) Failing after 22s
Close stale issues and PRs / stale (push) Successful in 13s
Needs user action. / needs-user-action (push) Failing after 8s
Can't reproduce. / cant-reproduce (push) Failing after 8s
Some checks failed
Docker. / Ubuntu (push) Has been cancelled
User-agent updater. / User-agent (push) Failing after 15s
Lock Threads / lock (push) Failing after 10s
Waiting for answer. / waiting-for-answer (push) Failing after 22s
Close stale issues and PRs / stale (push) Successful in 13s
Needs user action. / needs-user-action (push) Failing after 8s
Can't reproduce. / cant-reproduce (push) Failing after 8s
This commit is contained in:
59
Telegram/lib_base/base/thread_safe_wrap.h
Normal file
59
Telegram/lib_base/base/thread_safe_wrap.h
Normal file
@@ -0,0 +1,59 @@
|
||||
// This file is part of Desktop App Toolkit,
|
||||
// a set of libraries for developing nice desktop applications.
|
||||
//
|
||||
// For license and copyright information please follow this link:
|
||||
// https://github.com/desktop-app/legal/blob/master/LEGAL
|
||||
//
|
||||
#pragma once
|
||||
|
||||
#include <utility>
|
||||
|
||||
namespace base {
|
||||
|
||||
template <typename T>
|
||||
class thread_safe_wrap {
|
||||
public:
|
||||
template <typename ...Args>
|
||||
thread_safe_wrap(Args &&...args) : _value(std::forward<Args>(args)...) {
|
||||
}
|
||||
|
||||
template <typename Callback>
|
||||
auto with(Callback &&callback) {
|
||||
QMutexLocker lock(&_mutex);
|
||||
return callback(_value);
|
||||
}
|
||||
|
||||
template <typename Callback>
|
||||
auto with(Callback &&callback) const {
|
||||
QMutexLocker lock(&_mutex);
|
||||
return callback(_value);
|
||||
}
|
||||
|
||||
private:
|
||||
T _value;
|
||||
mutable QMutex _mutex;
|
||||
|
||||
};
|
||||
|
||||
template <typename T, template<typename...> typename Container = std::deque>
|
||||
class thread_safe_queue {
|
||||
public:
|
||||
template <typename ...Args>
|
||||
void emplace(Args &&...args) {
|
||||
_wrap.with([&](Container<T> &value) {
|
||||
value.emplace_back(std::forward<Args>(args)...);
|
||||
});
|
||||
}
|
||||
|
||||
Container<T> take() {
|
||||
return _wrap.with([&](Container<T> &value) {
|
||||
return std::exchange(value, Container<T>());
|
||||
});
|
||||
}
|
||||
|
||||
private:
|
||||
thread_safe_wrap<Container<T>> _wrap;
|
||||
|
||||
};
|
||||
|
||||
} // namespace base
|
||||
Reference in New Issue
Block a user