#include<algorithm>
#include<iostream>
#include<iterator>
#include<mutex>
#include<thread>
#include<vector>
std::mutex mutex;
#define PRINT(VALUE) std::cout << __FUNCTION__ << "(" << std::this_thread::get_id() << "): " << VALUE << "\n"
namespace Util
{
template<class Class,
class Mutex = std::mutex>
class Concurrent : private Class
{
public: using Class::Class;
private: class Safe
{
public: Safe (Concurrent* const this_,
Mutex& rMutex) :
m_This(this_),
m_rMutex(rMutex)
{ m_rMutex.lock(); }
public: ~Safe () { m_rMutex.unlock(); }
public: Class* operator-> () { return m_This; }
public: const Class* operator-> () const { return m_This; }
public: Class& operator* () { return *m_This; }
public: const Class& operator* () const { return *m_This; }
private: Concurrent* const m_This;
private: Mutex& m_rMutex;
};
public: Safe ScopeLocked () { return Safe(this, m_Mutex); }
public: const Class* Unsafe () const { return this; }
public: Safe operator-> () { return ScopeLocked(); }
public: const Class* operator-> () const { return this; }
public: const Class& operator* () const { return *this; }
private: Mutex m_Mutex;
};
}
Util::Concurrent<std::vector<int>> vi{8,9};
void
Thread ()
{
PRINT("started...");
vi->push_back(2);
PRINT((*vi)[0]);
auto viLocked = vi.ScopeLocked();
std::copy(viLocked->begin(), viLocked->end(), std::ostream_iterator<int>(std::cout, ", "));
std::sort(viLocked->begin(), viLocked->end());
PRINT("copied...");
}
int main ()
{
std::thread run(Thread);
PRINT("started...");
vi->push_back(1);
{
PRINT("looping...");
auto viLocked = vi.ScopeLocked();
for(auto it = viLocked->begin(); it != viLocked->end(); ++it)
PRINT(*it);
}
auto viLocked = vi.ScopeLocked();
auto it = std::begin(*viLocked);
PRINT(*++it);
run.join();
}
I2luY2x1ZGU8YWxnb3JpdGhtPgojaW5jbHVkZTxpb3N0cmVhbT4KI2luY2x1ZGU8aXRlcmF0b3I+CiNpbmNsdWRlPG11dGV4PgojaW5jbHVkZTx0aHJlYWQ+CiNpbmNsdWRlPHZlY3Rvcj4KCnN0ZDo6bXV0ZXggbXV0ZXg7CiNkZWZpbmUgUFJJTlQoVkFMVUUpIHN0ZDo6Y291dCA8PCBfX0ZVTkNUSU9OX18gPDwgIigiIDw8IHN0ZDo6dGhpc190aHJlYWQ6OmdldF9pZCgpIDw8ICIpOiAiIDw8IFZBTFVFIDw8ICJcbiIKCm5hbWVzcGFjZSBVdGlsCnsKICB0ZW1wbGF0ZTxjbGFzcyBDbGFzcywKICAgICAgICAgICBjbGFzcyBNdXRleCA9IHN0ZDo6bXV0ZXg+CiAgY2xhc3MgQ29uY3VycmVudCA6IHByaXZhdGUgQ2xhc3MKICB7CiAgICBwdWJsaWM6IHVzaW5nIENsYXNzOjpDbGFzczsKCiAgICBwcml2YXRlOiBjbGFzcyBTYWZlCiAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgIHB1YmxpYzogU2FmZSAoQ29uY3VycmVudCogY29uc3QgdGhpc18sCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgTXV0ZXgmIHJNdXRleCkgOgogICAgICAgICAgICAgICAgICAgICAgIG1fVGhpcyh0aGlzXyksCiAgICAgICAgICAgICAgICAgICAgICAgbV9yTXV0ZXgock11dGV4KQogICAgICAgICAgICAgICAgICAgICAgIHsgbV9yTXV0ZXgubG9jaygpOyB9CiAgICAgICAgICAgICAgIHB1YmxpYzogflNhZmUgKCkgeyBtX3JNdXRleC51bmxvY2soKTsgfQoKICAgICAgICAgICAgICAgcHVibGljOiBDbGFzcyogb3BlcmF0b3ItPiAoKSB7IHJldHVybiBtX1RoaXM7IH0KICAgICAgICAgICAgICAgcHVibGljOiBjb25zdCBDbGFzcyogb3BlcmF0b3ItPiAoKSBjb25zdCB7IHJldHVybiBtX1RoaXM7IH0KICAgICAgICAgICAgICAgcHVibGljOiBDbGFzcyYgb3BlcmF0b3IqICgpIHsgcmV0dXJuICptX1RoaXM7IH0KICAgICAgICAgICAgICAgcHVibGljOiBjb25zdCBDbGFzcyYgb3BlcmF0b3IqICgpIGNvbnN0IHsgcmV0dXJuICptX1RoaXM7IH0KCiAgICAgICAgICAgICAgIHByaXZhdGU6IENvbm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