add PIProtectedVariable
This commit is contained in:
60
libs/main/thread/piprotectedvariable.h
Normal file
60
libs/main/thread/piprotectedvariable.h
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
/*! \file piprotectedvariable.h
|
||||||
|
* \ingroup Thread
|
||||||
|
* \~\brief
|
||||||
|
* \~english Thread-safe variable
|
||||||
|
* \~russian Потокобезопасная переменная
|
||||||
|
*/
|
||||||
|
/*
|
||||||
|
PIP - Platform Independent Primitives
|
||||||
|
Thread-safe variable
|
||||||
|
Ivan Pelipenko peri4ko@yandex.ru, Stephan Fomenko, Andrey Bychkov work.a.b@yandex.ru
|
||||||
|
|
||||||
|
This program is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public License
|
||||||
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef PIPROTECTEDVARIABLE_H
|
||||||
|
#define PIPROTECTEDVARIABLE_H
|
||||||
|
|
||||||
|
#include "pimutex.h"
|
||||||
|
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
class PIP_EXPORT PIProtectedVariable {
|
||||||
|
public:
|
||||||
|
void set(const T & v) {
|
||||||
|
PIMutexLocker _ml(mutex);
|
||||||
|
var = v;
|
||||||
|
}
|
||||||
|
void set(T && v) {
|
||||||
|
PIMutexLocker _ml(mutex);
|
||||||
|
var = std::move(v);
|
||||||
|
}
|
||||||
|
T get() const {
|
||||||
|
PIMutexLocker _ml(mutex);
|
||||||
|
return var;
|
||||||
|
}
|
||||||
|
T & lock() {
|
||||||
|
mutex.lock();
|
||||||
|
return var;
|
||||||
|
}
|
||||||
|
void unlock() { mutex.unlock(); }
|
||||||
|
|
||||||
|
|
||||||
|
private:
|
||||||
|
mutable PIMutex mutex;
|
||||||
|
T var;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -55,6 +55,7 @@
|
|||||||
#include "pigrabberbase.h"
|
#include "pigrabberbase.h"
|
||||||
#include "pimutex.h"
|
#include "pimutex.h"
|
||||||
#include "pipipelinethread.h"
|
#include "pipipelinethread.h"
|
||||||
|
#include "piprotectedvariable.h"
|
||||||
#include "pispinlock.h"
|
#include "pispinlock.h"
|
||||||
#include "pithread.h"
|
#include "pithread.h"
|
||||||
#include "pithreadnotifier.h"
|
#include "pithreadnotifier.h"
|
||||||
|
|||||||
46
main.cpp
46
main.cpp
@@ -8,12 +8,50 @@
|
|||||||
#include "pilog.h"
|
#include "pilog.h"
|
||||||
#include "pimathbase.h"
|
#include "pimathbase.h"
|
||||||
#include "pip.h"
|
#include "pip.h"
|
||||||
|
#include "piprotectedvariable.h"
|
||||||
#include "pitranslator_p.h"
|
#include "pitranslator_p.h"
|
||||||
#include "pivaluetree_conversions.h"
|
#include "pivaluetree_conversions.h"
|
||||||
|
|
||||||
using namespace PICoutManipulators;
|
using namespace PICoutManipulators;
|
||||||
|
|
||||||
|
class MyStr: public PIString {
|
||||||
|
public:
|
||||||
|
MyStr(): PIString() {}
|
||||||
|
MyStr(const char * o): PIString(o) { piCout << "MyStr *"; }
|
||||||
|
MyStr(const MyStr & o): PIString(o) { piCout << "MyStr &"; }
|
||||||
|
// MyStr(const MyStr & o): PIString(o) { piCout << "MyStr &"; }
|
||||||
|
MyStr(MyStr && o): PIString(o) { piCout << "MyStr &&"; }
|
||||||
|
MyStr & operator=(const MyStr & o) {
|
||||||
|
*this += o;
|
||||||
|
piCout << "MyStr =&";
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
MyStr & operator=(MyStr && o) {
|
||||||
|
*this += o;
|
||||||
|
piCout << "MyStr =&&";
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char * argv[]) {
|
int main(int argc, char * argv[]) {
|
||||||
|
PIProtectedVariable<MyStr> var;
|
||||||
|
// MyStr s("hello");
|
||||||
|
var.set("hello");
|
||||||
|
auto & v = var.lock();
|
||||||
|
// v = "world";
|
||||||
|
var.unlock();
|
||||||
|
piCout << var.get();
|
||||||
|
|
||||||
|
/*PICodeParser parser;
|
||||||
|
parser.parseFile("client_server.h");
|
||||||
|
for (auto m: parser.enums) {
|
||||||
|
piCout << "";
|
||||||
|
piCout << m.name; // << m.args << m.value;
|
||||||
|
// piCout << m.expand({"hello"});
|
||||||
|
}*/
|
||||||
|
|
||||||
|
return 0;
|
||||||
PITranslator::loadLang("ru");
|
PITranslator::loadLang("ru");
|
||||||
PISerial f("COM123");
|
PISerial f("COM123");
|
||||||
f.open();
|
f.open();
|
||||||
@@ -47,14 +85,6 @@ int main(int argc, char * argv[]) {
|
|||||||
piCout << "hello!"_tr("Co") << "hello!"_tr;*/
|
piCout << "hello!"_tr("Co") << "hello!"_tr;*/
|
||||||
// piCout << "hello!"_trNoOp;
|
// piCout << "hello!"_trNoOp;
|
||||||
|
|
||||||
// PICodeParser parser;
|
|
||||||
// parser.parseFile("cmg_test.h");
|
|
||||||
/*for (auto m: parser.macros) {
|
|
||||||
piCout << "";
|
|
||||||
piCout << m.name << m.args << m.value;
|
|
||||||
piCout << m.expand({"hello"});
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*PISet<int> set;
|
/*PISet<int> set;
|
||||||
piCout << set << piSerialize(set) << piDeserialize<PISet<int>>(piSerialize(set));
|
piCout << set << piSerialize(set) << piDeserialize<PISet<int>>(piSerialize(set));
|
||||||
|
|||||||
Reference in New Issue
Block a user