added resource lock interface + posix example

master
polymurph 4 years ago
parent cdd710ad16
commit 073fdadfad

@ -3,7 +3,7 @@ cpp_src = $(wildcard *.cpp)
cpp_obj = $(cpp_src:.cpp=.o)
c_obj = $(c_src:.c=.o)
CC = g++
CFLAGS = -Wall -pedantic -li2c
CFLAGS = -Wall -pedantic #-li2c -lpthread
LDFLAGS =
EXEC = runtest
@ -11,7 +11,7 @@ EXEC = runtest
all : $(EXEC)
$(EXEC): $(cpp_obj) $(c_obj)
$(CC) -o $@ $^ $(LDFLAGS)
$(CC) -pthread -o $@ $^ $(LDFLAGS)
clean:
rm -rf $(c_obj) $(cpp_obj) $(EXEC)

@ -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_

@ -1,9 +1,78 @@
#include <stdint.h>
#include <unistd.h>
#include <iostream>
#include <functional>
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <cstdlib>
#include "resources.hpp"
Resources resources;
static void* task_1(void* arg)
{
int i = 0;
//for(i = 0; i < 10; i++) {
while(1) {
std::cout << std::endl << "Task 1 wanting to use resources"<< std::endl;
resources.print(1);//std::cout << "Hello, world!" << std::endl;
usleep(1000000);
}
while(1) usleep(10000);
};
static void* task_2(void* arg)
{
while(1)
{
std::cout << std::endl << "Task 2 wanting to use resources"<< std::endl;
resources.print(1000);
usleep(10000000);
}
};
int main(void)
{
pthread_t th_task_1;
pthread_t th_task_2;
int check = 0;
std::cout << "threads test" << std::endl;
check = pthread_create(&th_task_1,
NULL,
&task_1,
NULL);
if(check != 0) {
std::cout << "Failed to create thread!" << std::endl;
exit(-1);
}
check = pthread_create(&th_task_2,
NULL,
&task_2,
NULL);
if(check != 0) {
std::cout << "Failed to create thread!" << std::endl;
exit(-1);
}
pthread_join(th_task_1,NULL);
pthread_join(th_task_2,NULL);
return 0;

@ -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…
Cancel
Save