Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,4 @@ tp2/
fbcode/
fbcode
buckifier/*.pyc
cmake-build-debug
17 changes: 17 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
cmake_minimum_required(VERSION 3.20)
project(threadPool)

set(CMAKE_CXX_STANDARD 11)
include_directories(include)

add_library(thread_pool STATIC
src/env.cc
src/env_posix.cc
src/threadpool_imp.cc)
add_definitions(-DOS_LINUX)

add_executable(thread_pool_test example/env_thread_test.cc)
target_link_libraries(thread_pool_test thread_pool -lpthread)

add_executable(env_thread_test example/threadpool_test.cc)
target_link_libraries(env_thread_test thread_pool -lpthread)
33 changes: 0 additions & 33 deletions Makefile

This file was deleted.

28 changes: 0 additions & 28 deletions example/Makefile

This file was deleted.

114 changes: 53 additions & 61 deletions example/env_thread_test.cc
Original file line number Diff line number Diff line change
@@ -1,94 +1,86 @@
//
// Created by zhanghuigui on 2021/3/17.
//

#include <iostream>
#include <unistd.h>
#include <atomic>

#include "env.h"
#include "threadpool.h"

#define THREAD_COUNT 10
#define THREAD_COUNT 3

using std::cout;
using std::endl;

struct Cxt{
int thread_id;
std::atomic<int>* last_id;
struct Cxt {
int thread_id;
std::atomic<int> *last_id;

Cxt(std::atomic<int>* p, int i) :
thread_id(i),last_id(p) {}
Cxt(std::atomic<int> *p, int i) :
thread_id(i), last_id(p) {}
};

static void Thread1(void *ptr) {
Cxt *cxt = reinterpret_cast<Cxt*>(ptr);
int count = THREAD_COUNT;
while(count --) {
printf("------- *%d* running ------- \n", cxt->thread_id);
sleep(2);
}
Cxt *cxt = reinterpret_cast<Cxt *>(ptr);
int count = THREAD_COUNT;
while (count--) {
printf("------- *%d* running ------- \n", cxt->thread_id);
sleep(2);
}
}

static void Thread2(void *ptr) {
Cxt *cxt = reinterpret_cast<Cxt*>(ptr);
int count = THREAD_COUNT;
while(count --) {
printf("------- *%d* running ------- \n", cxt->thread_id);
sleep(2);
}
Cxt *cxt = reinterpret_cast<Cxt *>(ptr);
int count = THREAD_COUNT;
while (count--) {
printf("------- *%d* running ------- \n", cxt->thread_id);
sleep(2);
}
}

static void finish1(void *ptr) {
Cxt *cxt = reinterpret_cast<Cxt*>(ptr);
printf("Finish excute %d\n", cxt->thread_id);
delete cxt;
Cxt *cxt = reinterpret_cast<Cxt *>(ptr);
printf("Finish excute %d\n", cxt->thread_id);
delete cxt;
}

void PrintEnvInfo(Env *env) {
if (env == nullptr) {
return;
}
if (env == nullptr) {
return;
}

int low_thread_nums;
int high_thread_nums;
uint64_t time;
int low_thread_nums;
int high_thread_nums;
uint64_t time;

time = env->NowMicros();
low_thread_nums = env->GetThreadPoolQueueLen(Env::Priority::LOW);
high_thread_nums = env->GetThreadPoolQueueLen(Env::Priority::HIGH);
time = env->NowMicros();
low_thread_nums = env->GetThreadPoolQueueLen(Env::Priority::LOW);
high_thread_nums = env->GetThreadPoolQueueLen(Env::Priority::HIGH);

cout << "time : " << env->TimeToString(time) << endl
<< "low thread nums: " << low_thread_nums << endl
<< "high thread nums: " << high_thread_nums << endl
<< "thread id: " << env->GetThreadID() << endl
<< endl;
cout << "time : " << env->TimeToString(time) << endl
<< "low thread nums: " << low_thread_nums << endl
<< "high thread nums: " << high_thread_nums << endl
<< "thread id: " << env->GetThreadID() << endl
<< endl;

}

int main(int argc, char *argv[]) {
Env *env = Env::Default();
std::atomic<int> last_id(0);

env->SetBackgroundThreads(3, Env::Priority::LOW);
env->SetBackgroundThreads(7, Env::Priority::HIGH);

for (int i = 0, j = 0;i < 10; j++,i ++) {
Cxt cxt_i(&last_id, i);
Cxt cxt_j(&last_id, j);
if (i % 2 == 0 ) {
env->Schedule(&Thread1, &cxt_i, Env::Priority::LOW, &cxt_i, &finish1);
} else {
env->Schedule(&Thread2, &cxt_j, Env::Priority::HIGH, &cxt_j, &finish1);
}

PrintEnvInfo(env);
}

Cxt cxt_us(&last_id, 1);
env->UnSchedule(&cxt_us, Env::Priority::LOW);

return 0;
Env *env = Env::Default();
std::atomic<int> last_id(0);

env->SetBackgroundThreads(3, Env::Priority::LOW); // 设置低优先级有3个后台线程
env->SetBackgroundThreads(7, Env::Priority::HIGH);// 设置高优先级有7个后台线程

for (int i = 0, j = 0; i < 10; j++, i++) {
Cxt cxt_i(&last_id, i);
Cxt cxt_j(&last_id, j);
if (i % 2 == 0) env->Schedule(&Thread1, &cxt_i, Env::Priority::LOW, &cxt_i, &finish1);
else env->Schedule(&Thread2, &cxt_j, Env::Priority::HIGH, &cxt_j, &finish1);
// PrintEnvInfo(env);
}

Cxt cxt_us(&last_id, 1);
env->UnSchedule(&cxt_us, Env::Priority::LOW);
delete env;
return 0;
}

Loading