Skip to content

Commit 2cc1fdc

Browse files
committed
Initial example and concepts
1 parent 6adfce4 commit 2cc1fdc

File tree

5 files changed

+178
-0
lines changed

5 files changed

+178
-0
lines changed

.gitignore

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
.clangd/
2+
build/
3+
bin/
4+
*.html
5+
6+
# Created by https://www.gitignore.io/api/c++,cmake,emacs
7+
# Edit at https://www.gitignore.io/?templates=c++,cmake,emacs
8+
9+
### C++ ###
110
# Prerequisites
211
*.d
312

@@ -30,3 +39,73 @@
3039
*.exe
3140
*.out
3241
*.app
42+
43+
### CMake ###
44+
CMakeLists.txt.user
45+
CMakeCache.txt
46+
CMakeFiles
47+
CMakeScripts
48+
Testing
49+
Makefile
50+
cmake_install.cmake
51+
install_manifest.txt
52+
compile_commands.json
53+
CTestTestfile.cmake
54+
_deps
55+
56+
### CMake Patch ###
57+
# External projects
58+
*-prefix/
59+
60+
### Emacs ###
61+
# -*- mode: gitignore; -*-
62+
*~
63+
\#*\#
64+
/.emacs.desktop
65+
/.emacs.desktop.lock
66+
*.elc
67+
auto-save-list
68+
tramp
69+
.\#*
70+
71+
# Org-mode
72+
.org-id-locations
73+
*_archive
74+
75+
# flymake-mode
76+
*_flymake.*
77+
78+
# eshell files
79+
/eshell/history
80+
/eshell/lastdir
81+
82+
# elpa packages
83+
/elpa/
84+
85+
# reftex files
86+
*.rel
87+
88+
# AUCTeX auto folder
89+
/auto/
90+
91+
# cask packages
92+
.cask/
93+
dist/
94+
95+
# Flycheck
96+
flycheck_*.el
97+
98+
# server auth directory
99+
/server/
100+
101+
# projectiles files
102+
.projectile
103+
104+
# directory configuration
105+
.dir-locals.el
106+
107+
# network security
108+
/network-security.data
109+
110+
111+
# End of https://www.gitignore.io/api/c++,cmake,emacs

CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
cmake_minimum_required(VERSION 3.16)
2+
project(cpp_concurrency)
3+
4+
if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX)
5+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror -Wall -Wno-long-long -pedantic")
6+
endif()
7+
8+
set(CMAKE_CXX_STANDARD 20)
9+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
10+
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
11+
12+
enable_testing()
13+
add_subdirectory(src)

README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,46 @@
11
# cpp_concurrency
22
Code for the Modern C++ Concurrency in Depth course from Udemy.
3+
4+
## Concepts
5+
6+
Process: Is defined by its instructions and current state.
7+
8+
Context Switching:
9+
- The OS can store the state of a process or thread, so that it can be restored and resume execution at a later point.
10+
- This allows multiple processes to share a single central processing unit (CPU), and is an essential feature of a multitasking operating system.
11+
12+
Round-Robin Scheduling:
13+
- Assigns time-slots to each process in equal portions and circular order, having no priorities.
14+
- Context switching is performed when a time slot is completed.
15+
- Simple and starvation-free.
16+
- See: https://en.wikipedia.org/wiki/Round-robin_scheduling
17+
18+
Task- and Data- Level Parallelism:
19+
- Task-Level: Threads perform different tasks (using the same or different data).
20+
- Data-Level: Threads perform the same task on different data.
21+
22+
Parallelism vs Concurrency:
23+
- Parallelism: When each process/thread runs on a dedicated processor at the same time.
24+
- Concurrency: Emulated parallelism based on context-switching (sequencial execution!).
25+
- True parallelism is difficult to achieve, due to the low number of cores usually available.
26+
27+
Heterogeneous Computing:
28+
- Refers to systems that use more than one kind of processor or cores.
29+
- These systems gain performance or energy efficiency by using specialized hardware.
30+
- https://en.wikipedia.org/wiki/Heterogeneous_computing
31+
32+
GPGPU (General Purpose GPU):
33+
- Is the use of a GPU to handle tasks a CPU would do.
34+
- The idea is to handle matrix-like data using the GPU.
35+
- See: https://en.wikipedia.org/wiki/General-purpose_computing_on_graphics_processing_units
36+
37+
## Building the code
38+
39+
```bash
40+
sudo apt install gcc-10 g++-10 # C++20
41+
42+
mkdir -p build && cd build
43+
cmake -D CMAKE_C_COMPILER=gcc-10 -D CMAKE_CXX_COMPILER=g++-10 .. && make
44+
45+
-std=c++20 -fcoroutines -pthread
46+
```

src/0_thread.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <cstdio>
2+
#include <iostream>
3+
#include <ostream>
4+
#include <thread>
5+
6+
void foo() {
7+
auto id = std::this_thread::get_id();
8+
std::cout << "Hello from funciton foo. Thread ID:" << id << std::endl;
9+
}
10+
11+
class Bar {
12+
public:
13+
void operator()() {
14+
auto id = std::this_thread::get_id();
15+
std::cout << "Hello from functor Bar.Thread ID:" << id << std::endl;
16+
}
17+
};
18+
19+
int main() {
20+
std::thread thread1(foo);
21+
22+
Bar bar;
23+
std::thread thread2(bar);
24+
25+
std::thread thread3([] {
26+
auto id = std::this_thread::get_id();
27+
std::cout << "Hello from lambda.Thread ID:" << id << std::endl;
28+
});
29+
30+
thread1.join();
31+
thread2.join();
32+
thread3.join();
33+
std::cout << "Hello from main." << std::endl;
34+
return 0;
35+
}

src/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
project(src) # can this be deleted?
2+
3+
#find_package(Boost REQUIRED)
4+
#include_directories(${Boost_INCLUDE_DIRS})
5+
6+
add_executable(0_thread 0_thread.cpp)
7+
target_link_libraries(0_thread pthread)

0 commit comments

Comments
 (0)