Skip to content

Fix issue #352 #523

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 22, 2024
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
7 changes: 7 additions & 0 deletions exercises/basicTypes/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Fundamental types and expressions

## Tasks:
- Compile the program and analyse the output of the different expressions
- Discuss with other students or your tutor in case the result of an expression is a surprise
- Fix the marked expressions by changing types such that they produce meaningful results
- Answer the questions in the code
16 changes: 9 additions & 7 deletions talk/concurrency/condition.tex
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,22 @@
\onslide<1>
\begin{block}{Mechanics of wait}
\begin{itemize}
\item Many threads are waiting for shared data
\item Pass a \cppinline{unique_lock} and a predicate for wakeup to \cppinline{wait()}
\item \cppinline{wait()} sends threads to sleep while predicate is \cppinline{false}
\item Many threads can wait for shared data
\item Pass \cppinline{unique_lock} and optional predicate to \cppinline{wait()}
\item \cppinline{wait()} keeps threads asleep while predicate is \cppinline{false}
\begin{itemize}
\item Threads might wake up spuriously, but \cppinline{wait()} returns only when lock available \emph{and} predicate \cppinline{true}
\end{itemize}
\item \cppinline{wait()} will only lock when necessary; unlocked while sleeping
\item Threads might wake up spuriously, but \cppinline{wait()} returns only when lock available \emph{and} predicate \cppinline{true}
\end{itemize}
\end{block}
\onslide<2->
\begin{block}{Waiting / waking up}
\begin{itemize}
\item \cppinline{notify_all()} is called, threads wake up
\item When \cppinline{notify_all()} is called, threads wake up
\item Threads try to lock mutex, and evaluate predicate
\item One thread succeeds to acquire mutex, starts data processing
\item {\color{red} Problem}: Thread holds mutex now, other threads are blocked!
\item One thread succeeds to acquire mutex, starts processing data
\item {\color{red} Problem}: One thread holds mutex, other threads are blocked!
\end{itemize}
\end{block}
\end{overprint}
Expand Down
4 changes: 2 additions & 2 deletions talk/concurrency/mutexes.tex
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,8 @@
\frametitlecpp[20]{Synchronised \texttt{cout}}
\begin{block}{Parallel writing to streams}
\begin{itemize}
\item All parallel writes to streams become garbled when not synchronised
\item \cppinline{std::osyncstream} is a wrapper to synchronise output to streams
\item Parallel writes to streams become garbled when not synchronised
\item \cppinline{std::osyncstream} wraps and synchronises output to streams
\item Multiple instances of the osyncstream synchronise globally
\end{itemize}
\end{block}
Expand Down
9 changes: 5 additions & 4 deletions talk/concurrency/threadlocal.tex
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
\begin{exampleblock}{}
\begin{cppcode*}{}
thread_local int a{0};
std::jthread t1([&] { a++; });
std::jthread t2([&] { a++; });
a += 2;
t1.join(); t2.join();
{
std::jthread t1([&] { a++; });
std::jthread t2([&] { a++; });
a += 2;
}
assert( a == 2 ); // Guaranteed to succeed
\end{cppcode*}
\end{exampleblock}
Expand Down
58 changes: 26 additions & 32 deletions talk/morelanguage/lambda.tex
Original file line number Diff line number Diff line change
@@ -1,32 +1,5 @@
\subsection[$\lambda$]{Lambdas}

\begin{frame}[fragile]
\frametitlecpp[11]{Trailing function return type}
\begin{block}{An alternate way to specify a function's return type}
\begin{cppcode*}{linenos=false}
int f(float a); // classic
auto f(float a) -> int; // trailing
auto f(float a) { return 42; } // deduced, C++14
\end{cppcode*}
\end{block}
\pause
\begin{block}{Advantages}
\begin{itemize}
\item Allows to simplify inner type definition
\begin{cppcode*}{gobble=4}
class Equation {
using ResultType = double;
ResultType evaluate();
}
Equation::ResultType Equation::evaluate() {...}
auto Equation::evaluate() -> ResultType {...}
\end{cppcode*}
\item Used by lambda expressions
\end{itemize}
\end{block}
\end{frame}


\begin{frame}[fragile]
\frametitlecpp[11]{Lambda expressions}
\begin{block}{Definition}
Expand Down Expand Up @@ -72,6 +45,31 @@
\end{exampleblock}
\end{frame}

\begin{frame}[fragile]
\frametitlecpp[11]{Trailing function return type}
\begin{block}{An alternate way to specify a function's return type}
\begin{cppcode*}{linenos=false}
int f(float a); // classic
auto f(float a) -> int; // trailing
auto f(float a) { return 42; } // deduced, C++14
\end{cppcode*}
\end{block}
\pause
\begin{block}{When to use trailing return type}
\begin{itemize}
\item Only way to specify return type for lambdas
\item Allows to simplify inner type definition
\begin{cppcode*}{gobble=4}
class Equation {
using ResultType = double;
ResultType evaluate();
}
Equation::ResultType Equation::evaluate() {...}
auto Equation::evaluate() -> ResultType {...}
\end{cppcode*}
\end{itemize}
\end{block}
\end{frame}

\begin{frame}[fragile]
\frametitlecpp[11]{Capturing variables}
Expand Down Expand Up @@ -143,7 +141,7 @@
\begin{block}{Explanation}
\begin{itemize}
\item By default, variables are captured by value
\item The lambda's \cppinline{operator()} is \cppinline{const}
\item The lambda's \cppinline{operator()} is \cppinline{const inline}
\end{itemize}
\end{block}
\end{frame}
Expand Down Expand Up @@ -339,10 +337,6 @@
} s;
s.f(42); // prints "42"

template <typename T>
using CudaPtr = std::unique_ptr<T,
decltype([](T* p){ cudaFree(p); })>;

std::set<T, decltype([](T a, T b) { ... })> s2;
\end{cppcode*}
\end{exampleblock}
Expand Down
10 changes: 5 additions & 5 deletions talk/morelanguage/morestl.tex
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@

\begin{frame}[fragile]
\frametitlecpp[20]{\texttt{std::span<T>} - Usage}
\begin{exampleblockGB}{Some example}{https://godbolt.org/z/bTWjM8ba1}{\texttt{span}}
\begin{exampleblockGB}{Some example}{https://godbolt.org/z/nP3jr388z}{\texttt{span}}
\scriptsize
\begin{cppcode*}{}
void print(std::span<int> c) {
void print(std::span<const int> c) {
for (auto a : c) { std::cout << a << " "; }
std::cout << "\n";
}
Expand All @@ -70,16 +70,16 @@
print(a); // 23 45 67 89

std::vector v{1, 2, 3, 4, 5};
std::span<int> sv = v;
std::span<const int> sv = v;
print(sv.subspan(2, 2)); // 3 4

std::array a2{-14, 55, 24};
times2(a2);
print(a2); // -28 110 48

std::span<int, 3> sa2 = a2;
std::span<const int, 3> sa2 = a2;
std::cout << sizeof(sv) << " " << sizeof(sa2) << "\n"; // 16 8
std::span<int, 3> s2a2 = a; // compilation failure, invalid conversion
std::span<const int, 3> s2a2 = a; // compilation failure, invalid conversion
\end{cppcode*}
\end{exampleblockGB}
\end{frame}
Expand Down
28 changes: 22 additions & 6 deletions talk/objectorientation/adl.tex
Original file line number Diff line number Diff line change
Expand Up @@ -171,20 +171,36 @@
\item Avoid accidental implicit conversions
\end{itemize}
\end{block}
\begin{alertblock}{Accidental conversions - two examples in one}
\begin{alertblock}{Accidental conversions - two counterexamples}
\footnotesize
\begin{overprint}
\onslide<1>
\begin{cppcode*}{gobble=2}
std::ostream & operator<< // out-of-class definition
(std::ostream & os, Complex const & c) { ... }
struct Fraction {
int num, denom;
operator Complex() const { ... } // case 1: conversion op
operator Complex() const { ... } // conversion op
};
Complex::Complex(Fraction f); // or case 2: converting ctor
std::cout << Fraction{2,4}; // Prints 0.5 + 0i, would call:
//case 1: operator<<(std::cout, Fraction{2,4}.operator Complex());
//case 2: operator<<(std::cout, Complex(Fraction{2, 3}));

std::cout << Fraction{2,4}; // Prints 0.5 + 0i
//calls operator<<(std::cout, Fraction{2,4}.operator Complex());
\end{cppcode*}

\onslide<2>
\begin{cppcode*}{gobble=2}
std::ostream & operator<< // out-of-class definition
(std::ostream & os, Complex const & c) { ... }
struct Fraction {
int num, denom;
};

Complex::Complex(Fraction f); // converting ctor

std::cout << Fraction{2,4}; // Prints 0.5 + 0i
//calls operator<<(std::cout, Complex(Fraction{2, 3}));
\end{cppcode*}
\end{overprint}
\end{alertblock}
\end{frame}

Expand Down
2 changes: 1 addition & 1 deletion talk/tools/formatting.tex
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
\item \mintinline{bash}{clang-format -i <file.cpp>} (looks for .clang-format file)
\item \mintinline{bash}{git clang-format} (formats local changes)
\item \mintinline{bash}{git clang-format <ref>} (formats changes since git \textless{}ref\textgreater{})
\item Some editors/IDEs find a .clang-format file and adapt
\item Most editors/IDEs can find a .clang-format file and adapt
\end{itemize}
\end{block}
\end{frame}
Expand Down
5 changes: 2 additions & 3 deletions talk/tools/webtools.tex
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
An online generic compiler with immediate feedback.
Allows:
\begin{itemize}
\item compiling online any code against any version of any compiler
\item trying various compilers in the browser
\item inspecting the assembly generated
\item use of external libraries (over 50 available !)
\item running the code produced
\item using tools, e.g. ldd, include-what-you-use, ...
\item running the produced code
\item sharing small pieces of code via permanent short links
\end{itemize}
\end{block}
Expand Down