Programming · Latest articles

30 new today · 2,696 articles · from 79 blogs

Preshing on Programming··Jeff Preshing

You Can Do Any Kind of Atomic Read-Modify-Write Operation

AI summaryThe article explains that C++11's atomic library provides several read-modify-write (RMW) operations, and that compare_exchange_weak (compare-and-swap) is the essential primitive from which any custom atomic RMW operation—such as a fetch_multiply—can be implemented via a CAS loop. It demonstrates that CAS loops can combine multiple computation steps, or even multiple variables packed into a single 32- or 64-bit integer (e.g., as a bitfield), into one atomic, lock-free operation. However, it cautions that the C++11 standard doesn't guarantee lock-free behavior, so developers should verify compiler and platform support, such as for std::atomic, std::atomic, or pointer specializations on x86, x64, or ARMv7.

Preshing on Programming··Jeff Preshing

Safe Bitfields in C++

AI summaryThe article presents a macro-based technique for defining 'safe bitfields' in C++, implemented with unions and class templates so that every operation—such as assignment, increment/decrement, and packed array indexing—is checked with runtime asserts to catch out-of-range values during development. It explains how the macros expand into a union whose members share a common initial sequence, which the C++ standard permits for standard-layout types, making the approach legal and free of undefined behavior. Since compilers inline the hidden function calls, the resulting machine code is as efficient as manual bit twiddling.

Preshing on Programming··Jeff Preshing

Semaphores are Surprisingly Versatile

AI summaryThe article demonstrates that semaphores, though invented by Dijkstra in the early 1960s, are surprisingly versatile building blocks in multithreaded programming: using only semaphores and atomic operations, one can implement lightweight mutexes, auto-reset event objects, read-write locks, a solution to the dining philosophers problem, and a lightweight semaphore with partial spinning. The author describes a reusable 'box office/bouncer' pattern in which an atomic counter at the 'box office' makes decisions in userspace while the semaphore acts as the 'bouncer' managing waiting threads, allowing these primitives to avoid expensive system calls and optionally spin briefly before sleeping in the kernel. All implementations are provided as portable C++11 source code on GitHub, including a Semaphore class that maps to native semaphores on Windows, MacOS, iOS, Linux and other POSIX environments.

Lea Verou··Lea Verou

Awesomplete: 2KB autocomplete with zero dependencies

AI summaryFrustrated by the HTML5 element's browser inconsistencies and lack of customizability, and finding existing autocomplete scripts too bloated and often jQuery-dependent, the author wrote their own widget called Awesomplete. The widget is extremely lightweight—under 1.5KB minified and gzipped—with zero dependencies, and it works in IE9 (sorta), IE10+, Chrome, Firefox, Safari 5+, and Mobile Safari. It's available on GitHub with documentation, and while it may not suit complex needs out of the box, it's designed to be hackable and extensible.

Preshing on Programming··Jeff Preshing

C++ Has Become More Pythonic

AI summaryThe article argues that modern C++, particularly C++11 and C++14, has adopted many features that give it a distinctly Pythonic flavor, such as range-based for loops, type deduction with auto, tuples, uniform initialization, lambda expressions, and parameter packs. The author compares each of these C++ additions to their earlier Python counterparts, raising the question of whether Python directly influenced C++ or whether both languages simply converged on similar useful constructs. Ultimately, the author muses that Python's reputation as a friendly, approachable language may have rubbed off on C++, and asks readers whether the new features make C++ simpler and more expressive.

Preshing on Programming··Jeff Preshing

Fixing GCC's Implementation of memory_order_consume

AI summaryThe article documents a bug in GCC 4.9.2's implementation of C++11's memory_order_consume, where the AArch64 compiler neither promotes consume to acquire (the 'heavy' strategy) nor preserves dependency chains (the 'efficient' strategy), allowing out-of-thin-air values like 0xbadf00d to be read in violation of the standard. The author demonstrates the bug with an example, applies Andrew Macleod's patch that conservatively promotes memory_order_consume to memory_order_acquire early in compilation, and verifies the resulting assembly uses the acquire-semantics ldar instruction. Notably, the PowerPC backend does not exhibit the bug because its machine description files treat consume like acquire, whereas AArch64's treat it like relaxed, and the article closes by discussing the uncertain future of memory_order_consume in the C++ standard.

Preshing on Programming··Jeff Preshing

How to Build a GCC Cross-Compiler

AI summaryThis article provides a step-by-step tutorial for building a GCC cross-compiler targeting AArch64 (64-bit ARM) on a Debian Linux system, starting from source packages including Binutils, GCC, Linux kernel headers, Glibc, and supporting libraries like MPFR, GMP, and MPC. It explains how the components depend on each other, details seven sequential build steps (from cross-assembler tools through the standard C and C++ libraries), and covers troubleshooting build errors. The author also mentions an alternative C library called Newlib for embedded targets and provides a bash script on GitHub to automate the entire build process.

Preshing on Programming··Jeff Preshing

How to Install the Latest GCC on Windows

AI summaryThis article provides a step-by-step guide for installing the latest GCC compiler on Windows by building it from source within the Cygwin environment, enabling access to modern C++14 features like generic lambdas that are missing from Visual Studio Express and older system compilers. The process involves installing Cygwin, adding the required packages, downloading and compiling GCC with time-saving configure options, and then testing the new compiler with a C++14 program. The author also notes that native GCC compilers from the MinGW-w64 project offer an alternative that doesn't require Cygwin.

Preshing on Programming··Jeff Preshing

My Multicore Talk at CppCon 2014

AI summaryThe author recounts attending CppCon 2014 and giving a talk on how Ubisoft Montreal develops games for multicore, explaining that their game engines build on three common threading patterns and use custom concurrent objects optimized with atomic operations similar to C++11's low-level atomic library. Notable attendees like Michael Wong and Lawrence Crowl, who authored the C++11 atomic operations section, chimed in during the talk. The author also offers corrections, clarifying that Microsoft's x86 compiler treats volatile variables specially (explaining the absence of compiler reordering bugs) and proposing an assert-based macro to help enforce single-threaded usage of lock-free data structures.

Fabien Sanglard·

Let's compile like it's 1992

I have been tinkering with the vanilla source code of Wolfenstein 3D from 1992. Even though it is more than 20 years old: It still compile and here is how to do it (with plenty of screenshots).

Fabien Sanglard·

Game Engine Black Books

I am almost done with the first volume of what I hope will become a serie called "Game Engine Black Book". Each book would take further what I tried to do with my articles: Explain simply, yet in great details, a legendary game engine. For

Elixir Blog··José Valim

Elixir v0.15.0 released

Elixir v0.15.0 introduces Elixir's Logger, Mix aliases and is the last stop before Elixir v1.0. We are also glad to welcome Alexei into our team.

Lea Verou··Lea Verou

An easy notation for grayscale colors

AI summaryThe CSS Working Group is debating how to name a new function for creating grayscale colors (with optional transparency), and the author is soliciting feedback on four competing proposals: gray(), white(), black(), and a one-argument shorthand for rgb()/rgba(). Each proposal has different trade-offs regarding intuitiveness, such as whether 100% means white or black and how alpha transparency is handled. The author also provides SCSS code to emulate the functions so developers can experiment before voting, though the rgb()/rgba() shorthand approach cannot be polyfilled.

Lea Verou··Lea Verou

Image comparison slider with pure CSS

AI summaryThe author, while writing the O'Reilly book 'CSS Secrets', demonstrates a Before/After image comparison slider built with pure CSS by using the underused resize property on a div overlaid on an image, requiring no JavaScript and offering more semantic markup than earlier scripted solutions. Drawbacks include limited keyboard accessibility, dragging only from the bottom-right corner, and lack of mobile support. A similar alternative approach uses a separate textarea element stretched via a scaleY() transform to achieve a full-height resizer, though it requires hardcoding the image dimensions in the CSS.

Andrew Kelley·

Turn Your Raspberry Pi into a Music Player Server

AI summaryThis article is a step-by-step tutorial for turning a Raspberry Pi into a music player server running Groove Basin, an open-source music player server with a web-based client that allows remote control and streaming. The author cautions that the Raspberry Pi is overhyped and recommends alternatives like the Beagle Bone Black, but proceeds to guide readers through installing Raspbian, setting up SSH access, and compiling dependencies such as SDL2, libav, and libebur128. Because some compilations take many hours on the Pi's limited hardware, the guide suggests running lengthy build steps in parallel or overnight.

Elixir Blog··José Valim

Elixir v0.14.0 released

Elixir v0.14.0 is released and expands the work on structs and bringing more of OTP right into Elixir and Mix.

Andrew Kelley·

Laptop Review - Bonobo Extreme

AI summaryThe author reviews the Bonobo Extreme laptop with Ubuntu pre-installed, praising its powerful performance, out-of-the-box driver support, and surprisingly good battery life for a heavy machine. However, they criticize numerous flaws, including dead pixels, periodic hardware failures, an awkward keyboard layout, and touchpad issues. The reviewer concludes that while guaranteed Linux compatibility is valuable, the laptop is overpriced and a machine with better quality assurance would be a smarter purchase.

Fabien Sanglard·

Trespasser: Jurassic Park CG Source Code Review

AI summaryJurassic Park: Trespasser, released in October 1998 after a three-year development cycle, was panned by critics yet achieved cult status thanks to a dedicated fan community that produced patches, tools, and even a remake. From a technology standpoint, its engine was a milestone in real-time CG, demonstrating that physics and outdoor environments were possible. The author found that the game's C++ code reflected its final result: genius and talent ultimately impaired by ambition and its resulting complexity.

Andrew Kelley·

My Quest to Build the Ultimate Music Player

AI summaryThe author chronicles their multi-year quest to build the ultimate music player, motivated by a deep love for Amarok 1.4 and its shortcomings, particularly its flawed ReplayGain plugin and lack of built-in loudness compensation, which they explain in detail including challenges like clipping and pre-gain. They lay out a feature wishlist covering loudness compensation with cached scanning, ignoring unreliable duration tags, gapless playback, robust codec support, remote streaming, and a web-based shared jukebox interface inspired by a college project called 'PartyBeat.' The article concludes with their early technology experiments, including tinkering with Qt and Phonon before discovering Music Player Daemon.