Preshing on Programming

https://preshing.com/ (RSS)
visit blog
How C++ Resolves a Function Call
15 Mar 2021 | original ↗

C is a simple language. You’re only allowed to have one function with each name. C++, on the other hand, gives you much more flexibility: You can have multiple functions with the same name (overloading). You can overload built-in operators like + and ==. You can write function templates. Namespaces help you avoid naming conflicts. I like...

Flap Hero Code Review
10 Dec 2020 | original ↗

Flap Hero is a small game written entirely in C++ without using an existing game engine. All of its source code is available on GitHub. I think it can serve as an interesting resource for novice and intermediate game developers to study. In this post, I’ll explain how Flap Hero’s code is organized, how it differs from larger game projects, why it...

A Small Open Source Game In C++
26 Nov 2020 | original ↗

I just released a mobile game called Flap Hero. It’s a Flappy Bird clone with cartoony graphics and a couple of twists: You can go in the pipes (wow!) and it takes two collisions to end the game. Flap Hero is free, quick to download (between 3 - 5 MB) and opens instantly. Give it a try! Flap Hero is open source, too. Its source code is...

Automatically Detecting Text Encodings in C++
27 Jul 2020 | original ↗

Consider the lowly text file. This text file can take on a surprising number of different formats. The text could be encoded as ASCII, UTF-8, UTF-16 (little or big-endian), Windows-1252, Shift JIS, or any of dozens of other encodings. The file may or may not begin with a byte order mark (BOM). Lines of text could be terminated with a linefeed...

I/O in Plywood
8 Jul 2020 | original ↗

Plywood is an open-source C++ framework I released a few weeks ago. It includes, among other things, a runtime module that exposes a cross-platform API for I/O, memory, threads, process management and more. This post is about the I/O part. For those who don’t know, I/O stands for input/output, and refers to the part of a computer system that...

A New Cross-Platform Open Source C++ Framework
26 May 2020 | original ↗

For the past little while – OK, long while – I’ve been working on a custom game engine in C++. Today, I’m releasing part of that game engine as an open source framework. It’s called the Plywood framework. View the documentation View on GitHub Please note that Plywood, by itself, is not a game engine! It’s a framework for building all kinds of...

A Flexible Reflection System in C++: Part 2
24 Jan 2018 | original ↗

In the previous post, I presented a basic system for runtime reflection in C++11. The post included a sample project that created a type descriptor using a block of macros: // Define Node's type descriptor REFLECT_STRUCT_BEGIN(Node) REFLECT_STRUCT_MEMBER(key) REFLECT_STRUCT_MEMBER(value) REFLECT_STRUCT_MEMBER(children) REFLECT_STRUCT_END() At...

A Flexible Reflection System in C++: Part 1
16 Jan 2018 | original ↗

In this post, I’ll present a small, flexible system for runtime reflection using C++11 language features. This is a system to generate metadata for C++ types. The metadata takes the form of TypeDescriptor objects, created at runtime, that describe the structure of other runtime objects. I’ll call these objects type descriptors. My initial...

How to Write Your Own C++ Game Engine
18 Dec 2017 | original ↗

Lately I’ve been writing a game engine in C++. I’m using it to make a little mobile game called Hop Out. Here’s a clip captured from my iPhone 6. (Unmute for sound!) Hop Out is the kind of game I want to play: Retro arcade gameplay with a 3D cartoon look. The goal is to change the color of every pad, like in Q*Bert. Hop Out is still in...

Can Reordering of Release/Acquire Operations Introduce Deadlock?
12 Jun 2017 | original ↗

I wasn’t planning to write about lock-free programming again, but a commenter named Mike recently asked an interesting question on my Acquire and Release Semantics post from 2012. It’s a question I wondered about years ago, but could never really reconcile until (possibly) now. A quick recap: A read-acquire operation cannot be reordered, either...

Here's a Standalone Cairo DLL for Windows
29 May 2017 | original ↗

Cairo is an open source C library for drawing vector graphics. I used it to create many of the diagrams and graphs on this blog. Cairo is great, but it’s always been difficult to find a precompiled Windows DLL that’s up-to-date and that doesn’t depend on a bunch of other DLLs. I was recently unable to find such a DLL, so I wrote a script to...

Learn CMake's Scripting Language in 15 Minutes
22 May 2017 | original ↗

As explained in my previous post, every CMake-based project must contain a script named CMakeLists.txt. This script defines targets, but it can also do a lot of other things, such as finding third-party libraries or generating C++ header files. CMake scripts have a lot of flexibility. Every time you integrate an external library, and often when...

How to Build a CMake-Based Project
11 May 2017 | original ↗

CMake is a versatile tool that helps you build C/C++ projects on just about any platform you can think of. It’s used by many popular open source projects including LLVM, Qt, KDE and Blender. All CMake-based projects contain a script named CMakeLists.txt, and this post is meant as a guide for configuring and building such projects. This post won’t...

Using Quiescent States to Reclaim Memory
26 Jul 2016 | original ↗

If you want to support multiple readers for a data structure, while protecting against concurrent writes, a read-write lock might seem like the only way – but it isn’t! You can achieve the same thing without a read-write lock if you allow several copies of the data structure to exist in memory. You just need a way to delete old copies when...

Leapfrog Probing
14 Mar 2016 | original ↗

A hash table is a data structure that stores a set of items, each of which maps a specific key to a specific value. There are many ways to implement a hash table, but they all have one thing in common: buckets. Every hash table maintains an array of buckets somewhere, and each item belongs to exactly one bucket. To determine the bucket for a...

A Resizable Concurrent Map
22 Feb 2016 | original ↗

In an earlier post, I showed how to implement the “world’s simplest lock-free hash table” in C++. It was so simple that you couldn’t even delete entries or resize the table. Well, a few years have passed since then, and I’ve recently written some concurrent maps without those limitations. You’ll find them in my Junction project on GitHub....

New Concurrent Hash Maps for C++
1 Feb 2016 | original ↗

A map is a data structure that maps a collection of keys to a collection of values. It’s a common concept in computer programming. You typically manipulate maps using functions such as find, insert and erase. A concurrent map is one that lets you call some of those functions concurrently – even in combinations where the map is modified. If it...

You Can Do Any Kind of Atomic Read-Modify-Write Operation
2 Apr 2015 | original ↗

Atomic read-modify-write operations – or “RMWs” – are more sophisticated than atomic loads and stores. They let you read from a variable in shared memory and simultaneously write a different value in its place. In the C++11 atomic library, all of the following functions perform an RMW: std::atomic<>::fetch_add() std::atomic<>::fetch_sub()...

Safe Bitfields in C++
24 Mar 2015 | original ↗

In my cpp11-on-multicore project on GitHub, there’s a class that packs three 10-bit values into a 32-bit integer. I could have implemented it using traditional bitfields… struct Status { uint32_t readers : 10; uint32_t waitToRead : 10; uint32_t writers : 10; }; Or with some bit twiddling… uint32_t status = readers | (waitToRead ...

Semaphores are Surprisingly Versatile
16 Mar 2015 | original ↗

In multithreaded programming, it’s important to make threads wait. They must wait for exclusive access to a resource. They must wait when there’s no work available. One way to make threads wait – and put them to sleep inside the kernel, so that they no longer take any CPU time – is with a semaphore. I used to think semaphores were strange and...

C++ Has Become More Pythonic
2 Dec 2014 | original ↗

C++ has changed a lot in recent years. The last two revisions, C++11 and C++14, introduce so many new features that, in the words of Bjarne Stroustrup, “It feels like a new language.” It’s true. Modern C++ lends itself to a whole new style of programming – and I couldn’t help noticing it has more of a Python flavor. Ranged-based for loops, type...

Fixing GCC's Implementation of memory_order_consume
24 Nov 2014 | original ↗

As I explained previously, there are two valid ways for a C++11 compiler to implement memory_order_consume: an efficient strategy and a heavy one. In the heavy strategy, the compiler simply treats memory_order_consume as an alias for memory_order_acquire. The heavy strategy is not what the designers of memory_order_consume had in mind, but...

How to Build a GCC Cross-Compiler
19 Nov 2014 | original ↗

GCC is not just a compiler. It’s an open source project that lets you build all kinds of compilers. Some compilers support multithreading; some support shared libraries; some support multilib. It all depends on how you configure the compiler before building it. This guide will demonstrate how to build a cross-compiler, which is a compiler that...

How to Install the Latest GCC on Windows
8 Nov 2014 | original ↗

Several modern C++ features are currently missing from Visual Studio Express, and from the system GCC compiler provided with many of today’s Linux distributions. Generic lambdas – also known as polymorphic lambdas – are one such feature. This feature is, however, available in the latest versions of GCC and Clang. The following guide will help you...

My Multicore Talk at CppCon 2014
24 Oct 2014 | original ↗

Last month, I attended CppCon 2014 in Bellevue, Washington. It was an awesome conference, filled with the who’s who of C++ development, and loaded with interesting, relevant talks. It was a first-year conference, so I’m sure CppCon 2015 will be even better. I highly recommend it for any serious C++ developer. While I was there, I gave a talk...

↑ these items are from RSS. Visit the blog itself at https://preshing.com/ to find other articles and to appreciate the author's digital home.