Skip to content

Commit

Permalink
add threads number test
Browse files Browse the repository at this point in the history
  • Loading branch information
senlinzhan committed Apr 30, 2017
1 parent dad94c3 commit 8bee8e9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
main: main.o
g++ -std=c++11 -Wall -o main main.o

main.o: main.cpp
g++ -std=c++11 -Wall -g -c main.cpp

clean:
rm main *.o
1 change: 0 additions & 1 deletion ThreadPool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

namespace dpool {


class ThreadPool
{
public:
Expand Down
16 changes: 9 additions & 7 deletions ThreadPoolTest.cpp → main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ void task(int taskId)
std::cout << "task-" << taskId << " begin!" << std::endl;
}

// executing task for 1 second
std::this_thread::sleep_for(std::chrono::seconds(5));
// executing task for 2 second
std::this_thread::sleep_for(std::chrono::seconds(2));

{
std::lock_guard<std::mutex> guard(coutMtx);
std::cout << "task-" << taskId << " end!" << std::endl;
}
}

// monitoring threads number for 20 seconds
void monitor(const ThreadPool &pool)

void monitor(const ThreadPool &pool, int seconds)
{
for (int i = 1; i < 200; ++i)
for (int i = 1; i < seconds * 10; ++i)
{
{
std::lock_guard<std::mutex> guard(coutMtx);
Expand All @@ -39,9 +39,11 @@ void monitor(const ThreadPool &pool)

int main(int argc, char *argv[])
{
ThreadPool pool(100); // 100 threads
// max threads number is 100
ThreadPool pool(100);

pool.submit(monitor, std::ref(pool));
// monitoring threads number for 13 seconds
pool.submit(monitor, std::ref(pool), 13);

// submit 100 tasks
for (int taskId = 0; taskId < 100; ++taskId)
Expand Down

0 comments on commit 8bee8e9

Please sign in to comment.