parent
cdd710ad16
commit
073fdadfad
@ -0,0 +1,26 @@
|
||||
#ifndef _RESOURCES_LOCK_HPP_
|
||||
#define _RESOURCES_LOCK_HPP_
|
||||
|
||||
#include <memory>
|
||||
|
||||
/**
|
||||
* Resource Lock interface class
|
||||
*
|
||||
* Used for multithreading purpouses
|
||||
*/
|
||||
class ResourceLock
|
||||
{
|
||||
public:
|
||||
ResourceLock();
|
||||
~ResourceLock();
|
||||
|
||||
void lock();
|
||||
void unlock();
|
||||
|
||||
private:
|
||||
class ResourceLock_Impl;
|
||||
std::unique_ptr<ResourceLock_Impl> impl;
|
||||
|
||||
};
|
||||
|
||||
#endif // _RESOURCES_LOCK_HPP_
|
@ -0,0 +1,60 @@
|
||||
#include "iresource_lock.hpp"
|
||||
#include <pthread.h>
|
||||
#include <memory>
|
||||
#include <iostream>
|
||||
|
||||
//
|
||||
// POSIX implementation
|
||||
//
|
||||
|
||||
class ResourceLock::ResourceLock_Impl
|
||||
{
|
||||
public:
|
||||
ResourceLock_Impl() :
|
||||
mutex(PTHREAD_MUTEX_INITIALIZER)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void lock()
|
||||
{
|
||||
pthread_mutex_lock(&mutex);
|
||||
std::cout << "locked" << std::endl;
|
||||
}
|
||||
|
||||
void unlock()
|
||||
{
|
||||
pthread_mutex_unlock(&mutex);
|
||||
std::cout << "unlocked" << std::endl;
|
||||
}
|
||||
|
||||
private:
|
||||
pthread_mutex_t mutex;
|
||||
};
|
||||
|
||||
|
||||
//
|
||||
// information relay
|
||||
//
|
||||
|
||||
ResourceLock::ResourceLock() :
|
||||
impl{std::make_unique<ResourceLock_Impl>()}
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
ResourceLock::~ResourceLock()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void ResourceLock::lock()
|
||||
{
|
||||
impl->lock();
|
||||
}
|
||||
|
||||
void ResourceLock::unlock()
|
||||
{
|
||||
impl->unlock();
|
||||
}
|
||||
|
@ -0,0 +1,25 @@
|
||||
#include "resources.hpp"
|
||||
|
||||
Resources::Resources() :
|
||||
pLock(new ResourceLock())
|
||||
{
|
||||
std::cout << "Ctor resources" << std::endl;
|
||||
}
|
||||
|
||||
Resources::~Resources()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void Resources::print(const int& number)
|
||||
{
|
||||
// threadsafe usage of resources
|
||||
|
||||
pLock->lock();
|
||||
|
||||
std::cout << "resources in use" << std::endl;
|
||||
std::cout << "printing number: " << number << std::endl;
|
||||
|
||||
pLock->unlock();
|
||||
}
|
||||
|
@ -0,0 +1,21 @@
|
||||
#ifndef _RESOURCES_HPP_
|
||||
#define _RESOURCES_HPP_
|
||||
|
||||
#include <iostream>
|
||||
#include "iresource_lock.hpp"
|
||||
|
||||
class Resources
|
||||
{
|
||||
public:
|
||||
Resources();
|
||||
~Resources();
|
||||
|
||||
//void use();
|
||||
void print(const int& number);
|
||||
private:
|
||||
ResourceLock* pLock;
|
||||
|
||||
|
||||
};
|
||||
|
||||
#endif // _RESOURCES_HPP_
|
Binary file not shown.
Loading…
Reference in new issue