parent
7a190241ea
commit
335be7f8c8
@ -0,0 +1,29 @@
|
||||
cpp_src = $(wildcard *.cpp)
|
||||
cpp_src += $(wildcard ./utils/*.cpp)
|
||||
cpp_src += $(wildcard ./driver/*.cpp)
|
||||
|
||||
cpp_obj = $(cpp_src:.cpp=.o)
|
||||
c_obj = $(c_src:.c=.o)
|
||||
CC = g++
|
||||
CFLAGS = -Wall -pedantic -li2c
|
||||
LDFLAGS = -lpthread
|
||||
EXEC = runtest
|
||||
|
||||
|
||||
all : $(EXEC)
|
||||
|
||||
$(EXEC): $(cpp_obj) $(c_obj)
|
||||
$(CC) -o $@ $^ $(LDFLAGS)
|
||||
|
||||
debug : $(EXEC)
|
||||
|
||||
$(EXEC): $(cpp_obj) $(c_obj)
|
||||
$(CC) -g -o $@ $^ $(LDFLAGS)
|
||||
|
||||
clean:
|
||||
rm -rf $(c_obj) $(cpp_obj) $(EXEC)
|
||||
clear
|
||||
|
||||
cleanall:
|
||||
rm -rf $(c_obj) $(cpp_obj) $(EXEC)
|
||||
clear
|
@ -0,0 +1,77 @@
|
||||
#include <iostream>
|
||||
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <string>
|
||||
|
||||
pthread_mutex_t print_mtx;
|
||||
|
||||
class IThread
|
||||
{
|
||||
public:
|
||||
|
||||
private:
|
||||
|
||||
virtual void* _run() = 0;
|
||||
|
||||
static void* _staticRun(void* context)
|
||||
{
|
||||
return static_cast<IThread*>(context)->_run();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
void printer(std::string a)
|
||||
{
|
||||
pthread_mutex_lock(&print_mtx);
|
||||
|
||||
std::cout << a << std::endl;
|
||||
|
||||
pthread_mutex_unlock(&print_mtx);
|
||||
}
|
||||
|
||||
void* foo(void* arg)
|
||||
{
|
||||
unsigned int i = 100;
|
||||
for(;i > 0; i--){
|
||||
std::string a = "foo cnt: ";
|
||||
a += std::to_string(i);
|
||||
printer(a);
|
||||
sleep(1);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void* baa(void* arg)
|
||||
{
|
||||
unsigned int i = 5;
|
||||
|
||||
for(;i > 0; i--){
|
||||
std::string a = "baa cnt: ";
|
||||
a += std::to_string(i);
|
||||
printer(a);
|
||||
sleep(10);
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
pthread_t foo_th, baa_th;
|
||||
|
||||
|
||||
pthread_mutex_init(&print_mtx, NULL);
|
||||
|
||||
pthread_create(&foo_th, NULL, foo, NULL);
|
||||
pthread_create(&baa_th, NULL, baa, NULL);
|
||||
|
||||
|
||||
pthread_join(foo_th, NULL);
|
||||
pthread_join(baa_th, NULL);
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Binary file not shown.
Loading…
Reference in new issue