File tree Expand file tree Collapse file tree 8 files changed +68
-5
lines changed
Expand file tree Collapse file tree 8 files changed +68
-5
lines changed Original file line number Diff line number Diff line change @@ -15,13 +15,16 @@ Examples include (and will expand to):
1515* [ thread-safe-queue] ( ./thread-safe-queue/ )
1616* Smart pointers
1717 * [ unique-ptr-basics] ( ./unique-ptr-basics/ )
18+ * [ smart-ptr] ( ./smart-ptr/ )
1819* Lock‑free / wait‑free data structures
1920* Views
2021 * [ views-zip-enumerate] ( ./views-zip-enumerate/ )
2122* Atomics and memory ordering
2223* Folding
2324 * [ fold-left-fold-right] ( ./fold-left-fold-right/ )
2425* RAII and ownership patterns
26+ * Parallelism
27+ * [ parallel-transform] ( ./parallel-transform/ )
2528* Performance‑oriented C++ idioms
2629
2730---
Original file line number Diff line number Diff line change @@ -4,7 +4,7 @@ include ../common.mk
44# per-example flags
55CXXFLAGS += -pthread
66
7- TARGET := fold_left_fold_right
7+ TARGET := $( notdir $( CURDIR ) )
88SRCS := main.cpp
99OBJS := $(SRCS:.cpp=.o )
1010
Original file line number Diff line number Diff line change 1+ # pull in shared compiler settings
2+ include ../common.mk
3+
4+ # per-example flags
5+ CXXFLAGS += -pthread
6+
7+ # # get it from the folder name
8+ TARGET := $(notdir $(CURDIR ) )
9+ SRCS := main.cpp
10+ OBJS := $(SRCS:.cpp=.o )
11+
12+ all : $(TARGET )
13+
14+ $(TARGET ) : $(OBJS )
15+ $(CXX ) $(CXXFLAGS ) -o $@ $^
16+
17+ % .o : % .cpp
18+ $(CXX ) $(CXXFLAGS ) -c $< -o $@
19+
20+ run : $(TARGET )
21+ ./$(TARGET ) $(ARGS )
22+
23+ clean :
24+ rm -f $(OBJS ) $(TARGET )
25+
26+ # Delegates to top-level Makefile
27+ check-format :
28+ $(MAKE ) -f ../Makefile check-format DIR=$(CURDIR )
29+
30+ .PHONY : all clean run check-format
Original file line number Diff line number Diff line change 1+ /* *
2+ This shows using parallel transform_reduce from C++17
3+ to perform a fold (reduce) operation on a range of values. e.g. sum up 100 values in parallel
4+ */
5+ #include < vector>
6+ #include < numeric> // std::transform_reduce
7+ #include < functional> // std::plus
8+ #include < iostream>
9+ #include < execution> // std::execution::par
10+
11+ int
12+ main ()
13+ {
14+ std::vector<int > numbers (100 );
15+ // Initialize the vector with values 1 to 100
16+ std::iota (numbers.begin (), numbers.end (), 1 );
17+
18+ // Use parallel transform_reduce to sum the values in parallel
19+ int sum = std::transform_reduce (
20+ std::execution::par, // parallel execution policy parallel or std::execution::seq for sequential
21+ numbers.begin (),
22+ numbers.end (),
23+ 0 , // initial value
24+ std::plus<int >{}, // binary operation to combine results
25+ [](int v) { return v; } // unary operation to transform each element
26+ );
27+ std::cout << " Sum of numbers from 1 to 100: " << sum << ' \n ' ;
28+
29+ return 0 ;
30+ }
Original file line number Diff line number Diff line change 11CXX = g++
22CXXFLAGS = -std=c++23 -Wall -Wextra -g -I.
33
4- TARGET = main
4+ TARGET := $( notdir $( CURDIR ) )
55SRCS = main.cpp
66
77all : $(TARGET )
Original file line number Diff line number Diff line change @@ -4,7 +4,7 @@ include ../common.mk
44# per-example flags
55CXXFLAGS += -pthread
66
7- TARGET := thread_safe_queue
7+ TARGET := $( notdir $( CURDIR ) )
88SRCS := main.cpp
99OBJS := $(SRCS:.cpp=.o )
1010
Original file line number Diff line number Diff line change 11# shared compiler configuration
22include ../common.mk
33
4- TARGET := unique_ptr_basics
4+ TARGET := $( notdir $( CURDIR ) )
55SRCS := main.cpp
66OBJS := $(SRCS:.cpp=.o )
77
Original file line number Diff line number Diff line change @@ -4,7 +4,7 @@ include ../common.mk
44# per-example flags
55CXXFLAGS += -pthread
66
7- TARGET := views-zip-enumerate
7+ TARGET := $( notdir $( CURDIR ) )
88SRCS := main.cpp
99OBJS := $(SRCS:.cpp=.o )
1010
You can’t perform that action at this time.
0 commit comments