work on logger

master
polymurph 4 years ago
parent 16f9e9c0b4
commit ee517ad053

@ -0,0 +1,20 @@
cpp_src = $(wildcard *.cpp)
cpp_obj = $(cpp_src:.cpp=.o)
c_obj = $(c_src:.c=.o)
CC = g++
CFLAGS = -Wall -pedantic
LDFLAGS =
EXEC = runtest
all : $(EXEC)
$(EXEC): $(cpp_obj) $(c_obj)
$(CC) -pthread -o $@ $^ $(LDFLAGS)
clean:
rm -rf $(c_obj) $(cpp_obj) $(EXEC)
cleanall:
rm -rf $(c_obj) $(cpp_obj) $(EXEC)

@ -0,0 +1,33 @@
#include "logger.hpp"
Logger::Logger()
{
if(pthread_create(&thread_handle,
NULL,
_static_run,
this) != 0) {
std::cout << "failed to ctrate logger tast!" <<std::endl;
exit(-1);
}
}
pthread_t Logger::get_thread_handle()
{
return thread_handle;
}
void Logger::_run()
{
while(1) {
usleep(10000);
}
}
void Logger::_static_run(void* arg)
{
reinterpret_cast<Logger*>(arg)->_run();
}

@ -0,0 +1,22 @@
#ifndef _LOGGER_HPP_
#define _LOGGER_HPP_
#include <iostream>
#include <unistd.h>
#include <pthread.h>
class Logger
{
public:
Logger();
pthread_t get_thread_handle();
private:
pthread_t thread_handle;
void _run();
static void _static_run(void* arg);
};
#endif // LOGGER_HPP__

@ -0,0 +1,70 @@
#include <stdint.h>
#include <unistd.h>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <cstdlib>
#include "logger.hpp"
static void* task_1(void* arg)
{
int i = 0;
//for(i = 0; i < 10; i++) {
while(1) {
std::cout << "Hello, world!" << std::endl;
usleep(1000000);
}
while(1) usleep(10000);
};
static void* task_2(void* arg)
{
while(1)
{
std::cout << "Task 2"<< std::endl;
usleep(10000000);
}
};
int main(void)
{
pthread_t th_task_1;
pthread_t th_task_2;
int check = 0;
Logger logger;
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);
pthread_join(logger.get_handle());
return 0;
}
Loading…
Cancel
Save