Skip to content

Commit 4003392

Browse files
committed
initial commit
0 parents  commit 4003392

File tree

4 files changed

+490
-0
lines changed

4 files changed

+490
-0
lines changed

.gitignore

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# Created by https://www.toptal.com/developers/gitignore/api/clion+all
2+
# Edit at https://www.toptal.com/developers/gitignore?templates=clion+all
3+
4+
### CLion+all ###
5+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
6+
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
7+
8+
# User-specific stuff
9+
.idea/**/workspace.xml
10+
.idea/**/tasks.xml
11+
.idea/**/usage.statistics.xml
12+
.idea/**/dictionaries
13+
.idea/**/shelf
14+
15+
# AWS User-specific
16+
.idea/**/aws.xml
17+
18+
# Generated files
19+
.idea/**/contentModel.xml
20+
21+
# Sensitive or high-churn files
22+
.idea/**/dataSources/
23+
.idea/**/dataSources.ids
24+
.idea/**/dataSources.local.xml
25+
.idea/**/sqlDataSources.xml
26+
.idea/**/dynamic.xml
27+
.idea/**/uiDesigner.xml
28+
.idea/**/dbnavigator.xml
29+
30+
# Gradle
31+
.idea/**/gradle.xml
32+
.idea/**/libraries
33+
34+
# Gradle and Maven with auto-import
35+
# When using Gradle or Maven with auto-import, you should exclude module files,
36+
# since they will be recreated, and may cause churn. Uncomment if using
37+
# auto-import.
38+
# .idea/artifacts
39+
# .idea/compiler.xml
40+
# .idea/jarRepositories.xml
41+
# .idea/modules.xml
42+
# .idea/*.iml
43+
# .idea/modules
44+
# *.iml
45+
# *.ipr
46+
47+
# CMake
48+
cmake-build-*/
49+
50+
# Mongo Explorer plugin
51+
.idea/**/mongoSettings.xml
52+
53+
# File-based project format
54+
*.iws
55+
56+
# IntelliJ
57+
out/
58+
59+
# mpeltonen/sbt-idea plugin
60+
.idea_modules/
61+
62+
# JIRA plugin
63+
atlassian-ide-plugin.xml
64+
65+
# Cursive Clojure plugin
66+
.idea/replstate.xml
67+
68+
# SonarLint plugin
69+
.idea/sonarlint/
70+
71+
# Crashlytics plugin (for Android Studio and IntelliJ)
72+
com_crashlytics_export_strings.xml
73+
crashlytics.properties
74+
crashlytics-build.properties
75+
fabric.properties
76+
77+
# Editor-based Rest Client
78+
.idea/httpRequests
79+
80+
# Android studio 3.1+ serialized cache file
81+
.idea/caches/build_file_checksums.ser
82+
83+
### CLion+all Patch ###
84+
# Ignore everything but code style settings and run configurations
85+
# that are supposed to be shared within teams.
86+
87+
.idea/*
88+
89+
!.idea/codeStyles
90+
!.idea/runConfigurations
91+
92+
# End of https://www.toptal.com/developers/gitignore/api/clion+all
93+

CMakeLists.txt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
cmake_minimum_required(VERSION 3.24)
2+
3+
project(
4+
thread_pool
5+
LANGUAGES
6+
CXX
7+
)
8+
9+
set(TARGET thread-pool)
10+
add_executable(${TARGET})
11+
12+
target_compile_features(
13+
${TARGET}
14+
PUBLIC
15+
cxx_std_20
16+
)
17+
18+
target_sources(
19+
${TARGET}
20+
PRIVATE
21+
main.cpp
22+
thread_pool.hpp
23+
)
24+
25+
target_link_libraries(
26+
${TARGET}
27+
PRIVATE
28+
pthread
29+
)

main.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include <chrono>
2+
#include <iostream>
3+
4+
#include "thread_pool.hpp"
5+
6+
using namespace jbo;
7+
8+
int
9+
long_running_task()
10+
{
11+
using namespace std::chrono_literals;
12+
13+
std::this_thread::sleep_for(5s);
14+
15+
return 4;
16+
}
17+
18+
void
19+
print_state(const thread_pool::status& s, std::ostream& out = std::cout)
20+
{
21+
std::cout << "pool size : " << s.pool_size << "\n";
22+
std::cout << "queue size: " << s.queue_size << "\n";
23+
for (std::size_t i = 0; i < s.thread_states.size(); i++)
24+
std::cout << "thread [" << i << "] state: " << static_cast<int>(s.thread_states[i]) << "\n";
25+
}
26+
27+
int
28+
main()
29+
{
30+
thread_pool tp(4);
31+
32+
std::vector<std::future<int>> results;
33+
for (std::size_t i = 0; i < 10; i++) {
34+
35+
auto future = tp.enqueue([i]() -> int {
36+
std::this_thread::sleep_for(std::chrono::seconds(1));
37+
return i * i;
38+
});
39+
40+
results.emplace_back(std::move(future));
41+
}
42+
43+
for (int i = 0; i < 10; i++) {
44+
print_state(tp.get_status());
45+
std::cout << std::endl;
46+
std::this_thread::sleep_for(std::chrono::milliseconds(250));
47+
}
48+
49+
std::cout << "waiting..." << std::endl;
50+
for (auto& result : results)
51+
std::cout << "result: " << result.get() << std::endl;
52+
53+
while (true)
54+
std::this_thread::sleep_for(std::chrono::seconds(1));
55+
return EXIT_SUCCESS;
56+
}

0 commit comments

Comments
 (0)