Skip to content
Merged
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@ Examples include (and will expand to):
* [thread-safe-queue](./thread-safe-queue/)
* Smart pointers
* [unique-ptr-basics](./unique-ptr-basics/)
* [smart-ptr](./smart-ptr/)
* Lock‑free / wait‑free data structures
* Views
* [views-zip-enumerate](./views-zip-enumerate/)
* Atomics and memory ordering
* Folding
* [fold-left-fold-right](./fold-left-fold-right/)
* RAII and ownership patterns
* Parallelism
* [parallel-transform](./parallel-transform/)
* Performance‑oriented C++ idioms

---
Expand Down
2 changes: 1 addition & 1 deletion fold-left-fold-right/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ include ../common.mk
# per-example flags
CXXFLAGS += -pthread

TARGET := fold_left_fold_right
TARGET := $(notdir $(CURDIR))
SRCS := main.cpp
OBJS := $(SRCS:.cpp=.o)

Expand Down
30 changes: 30 additions & 0 deletions parallel-transform/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# pull in shared compiler settings
include ../common.mk

# per-example flags
CXXFLAGS += -pthread

## get it from the folder name
TARGET := $(notdir $(CURDIR))
SRCS := main.cpp
OBJS := $(SRCS:.cpp=.o)

all: $(TARGET)

$(TARGET): $(OBJS)
$(CXX) $(CXXFLAGS) -o $@ $^

%.o: %.cpp
$(CXX) $(CXXFLAGS) -c $< -o $@

run: $(TARGET)
./$(TARGET) $(ARGS)

clean:
rm -f $(OBJS) $(TARGET)

# Delegates to top-level Makefile
check-format:
$(MAKE) -f ../Makefile check-format DIR=$(CURDIR)

.PHONY: all clean run check-format
30 changes: 30 additions & 0 deletions parallel-transform/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
This shows using parallel transform_reduce from C++17
to perform a fold (reduce) operation on a range of values. e.g. sum up 100 values in parallel
*/
#include <vector>
#include <numeric> // std::transform_reduce
#include <functional> // std::plus
#include <iostream>
#include <execution> // std::execution::par

int
main()
{
std::vector<int> numbers(100);
// Initialize the vector with values 1 to 100
std::iota(numbers.begin(), numbers.end(), 1);

// Use parallel transform_reduce to sum the values in parallel
int sum = std::transform_reduce(
std::execution::par, // parallel execution policy parallel or std::execution::seq for sequential
numbers.begin(),
numbers.end(),
0, // initial value
std::plus<int>{}, // binary operation to combine results
[](int v) { return v; } // unary operation to transform each element
);
std::cout << "Sum of numbers from 1 to 100: " << sum << '\n';

return 0;
}
2 changes: 1 addition & 1 deletion smart-ptr/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
CXX = g++
CXXFLAGS = -std=c++23 -Wall -Wextra -g -I.

TARGET = main
TARGET := $(notdir $(CURDIR))
SRCS = main.cpp

all: $(TARGET)
Expand Down
2 changes: 1 addition & 1 deletion thread-safe-queue/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ include ../common.mk
# per-example flags
CXXFLAGS += -pthread

TARGET := thread_safe_queue
TARGET := $(notdir $(CURDIR))
SRCS := main.cpp
OBJS := $(SRCS:.cpp=.o)

Expand Down
2 changes: 1 addition & 1 deletion unique-ptr-basics/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# shared compiler configuration
include ../common.mk

TARGET := unique_ptr_basics
TARGET := $(notdir $(CURDIR))
SRCS := main.cpp
OBJS := $(SRCS:.cpp=.o)

Expand Down
2 changes: 1 addition & 1 deletion views-zip-enumerate/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ include ../common.mk
# per-example flags
CXXFLAGS += -pthread

TARGET := views-zip-enumerate
TARGET := $(notdir $(CURDIR))
SRCS := main.cpp
OBJS := $(SRCS:.cpp=.o)

Expand Down