C/C++ Resources and FAQ
1.46K subscribers
1 photo
2 files
23 links
This is a sub channel for the C/C++ forum (https://t.me/programminginc). Here you can find links to various useful resources (including book recommendations and video resources) for C/C++ programmers and also answers to frequently asked C/C++ questions.
Download Telegram
The shared_ptr equivalent is similarly simple: just swap out the std::unique_ptr<T, Deletion> return value with std::shared_ptr<T>, and change the name to indicate a shared resource:

template <
typename T,
typename Deletion,
typename Acquisition,
typename...Args>
std::shared_ptr<T> make_c_shared_handler(
Acquisition acquisition,
Deletion deletion,
Args&&...args){
return {acquisition(std::forward<Args>(args)...), deletion};
}

Use is once again similar to the unique_ptr version:

auto resource = make_c_shared_handler<CStyleResource>(
acquireResource, releaseResource, "name", nullptr, 0);

and

func(
make_c_shared_handler<CStyleResource>(
acquireResource, releaseResource, "name", nullptr, 0),
ThrowsOnConstruction{});

There's a further improvement you can make to the use of std::unique_ptr: specifying the deletion mechanism at compile time so the unique_ptr doesn't need to carry a function pointer to the deleter when it's moved around the program. Making a stateless deleter templated on the function pointer you're using requires four lines of code, placed before make_c_handler:

template <typename T, void (*Func)(T*)>
struct CDeleter{
void operator()(T* t){Func(t);}
};

Then you can modify make_c_handler like so:

template <
typename T,
void (*Deleter)(T*),
typename Acquisition,
typename...Args>
std::unique_ptr<T, CDeleter<T, Deleter>> make_c_handler(
Acquisition acquisition,
Args&&...args){
return {acquisition(std::forward<Args>(args)...), {}};
}

The usage syntax then changes slightly, to

auto resource = make_c_handler<CStyleResource, releaseResource>(
acquireResource, "name", nullptr, 0);

make_c_shared_handler would not benefit from changing to a templated deleter, as shared_ptr does not carry deleter information available at compile time.
πŸ‘7
ImplementingCVs.pdf
151.9 KB
--
Implementing Condition Variables with Semaphores

The paper discusses the implementation in C# but it is a straightforward translation to C/C++.

The reason I shared this paper is to show the thought process and the things people have to keep in mind when designing a solution for concurrency/parallel programming primitives.

All the kernels provide a semaphore as a primitive that can be used to build all the necessary primitives that a concurrency library provides.
πŸ‘6
C/C++ Resources and FAQ pinned Β«List of books recommended for learning C (Beginners to Advanced) Beginner: C Programming - A Modern Approach Effective C Beej's Guide to C Programming - Free Online Book Intermediate Modern C 21st Century C - C tips from new school Advanced Linux…»
The Missing Semester of Your CS Education is a practical and hands-on computer science class that fills the gaps in traditional CS education. This course covers essential topics often overlooked in standard curriculum, empowering students and programmers to work efficiently and effectively within the computing ecosystem. Topics include mastering the command shell for task automation, harnessing version control for collaboration and problem-solving, optimizing text editing with advanced features, managing remote machines seamlessly, streamlining file searching, data wrangling from the command line, leveraging virtual machines for experimentation, and enhancing security practices online. With 12 engaging lectures and practical exercises, this class equips participants with indispensable skills to excel in the world of computer science and programming.

https://www.youtube.com/@MissingSemester/
πŸ‘30πŸ‘Ž1