r/cprogramming

Should I Replace a Ring Buffer with a Lock-Free Queue in My Web Framework?

I have an experimental web framework I'm currently working on.

While going through the code to clean up styling before tagging a stable release, I've been debating this specific part in the HTTPS module:

https://github.com/c4punks/CWIST/blob/dev/src/net/http/https.c

Right now, this code just uses a dumb, massive ring buffer to absorb connection bursts, and I'm wondering if I should switch it to a lock-free queue.

There are three main things that make me hesitate:

* State can get pretty tangled and messy in a lock-free queue.

* We have a tiny team and I'm maintaining almost all of this alone. If something breaks with CAS (even if the odds are low, if I mess up the code during refactoring), the blast radius on the dev branch will just be too big.

* I also wonder how often real-world traffic actually warrants lock-free. Plenty of web frameworks handle traffic just fine without this level of extreme optimization.

Considering that I'm still a junior developer and lack experience, is it really worth swapping this out for a lock-free structure, and would it actually make a noticeable difference?

reddit.com
u/Whole-Low-2995 — 5 days ago

I'm building a game in C

I've been working on a game since about August 2020 but only recently published my Steam page.

For the Common Wealth on Steam

The game is entirely written in C, with one third party dependency, SDL3.

I chose C because I wanted to leave my options open to build for retro platforms like RISC OS and Amiga, but as the game grew, I wanted GPU acceleration and that meant leaving behind some of the retros. I'm not ruling out any ports though, if SDL3 is available on the platform.

The engine beyond SDL3 is my own, it's a 2.5D game, with levels edited in my own level editor (not in C, that's C# and Avalonia). The level editor exports levels into just plain memory dumps that are easy to read in C.

I'm trying to keep on top of a devlog here too:

Podule · Independent game studio

Writing this game in C has been enormous fun, and I have no plans to change my choice of C for future games.

u/podule-games — 5 days ago
▲ 38 r/cprogramming+1 crossposts

How can I improve my thought process in C interviews instead of relying on memorized solutions?

I was asked an endianness question in a recent Embedded C interview.

For example:

uint32_t n = 0x12345678;

I had previously learned the typical byte-swap approach and also i can do it for 16 bit

n = ((n & 0x000000FF) << 24) |
    ((n & 0x0000FF00) << 8)  |
    ((n & 0x00FF0000) >> 8)  |
    ((n & 0xFF000000) >> 24);

However, the interviewer asked me to convert the endian representation and store the result into a byte array using a pointer to the destination.

During the interview, my mind immediately went to the bit-manipulation code I had memorized, and I failed to do the simple program. How can I improve my thought process?

reddit.com
u/sudheerpaaniyur — 13 days ago
▲ 13 r/cprogramming+3 crossposts

LibLayaX: run the Laya AI decision model inside your own app

Dear LLM developers community,

I have just released four open-source projects today that let an application use the Laya model directly, with no server and no Python.

What Laya is?

If you build agents, this is the step where you ask a big model "should I route this to billing?" and wait a second for one word. Laya answers that kind of question in a fraction of a second, on your own machine, with a probability you can put a threshold on.

Laya is an AI model that does not chat. You give it a text and a question, like "is this customer asking for a refund?", and it answers yes or no with a probability, in a fraction of a second. It can also pick one of several options, or rate something on a scale. Quick decisions, no essays.

What I released

- LibLayaX: a library you load into your own program, like any other. Windows, Linux and macOS, on the CPU or the GPU.

https://github.com/DaragonTech/LibLayaX

- RLaya, for Rust: https://github.com/DaragonTech/RLaya

- LLaya, for Lua: https://github.com/DaragonTech/LLaya

- DLaya, for Delphi and Free Pascal: https://github.com/DaragonTech/DLaya

On a laptop GPU it makes about 670 decisions per second.

Everything is MIT licensed.

Why

On Thursday I wanted to use Laya inside one of my applications and found there was no way to do it. It only came as a Python library and as a separate server program. This kind of AI belongs inside ordinary software, routing a ticket or flagging a message, and for that it has to be something an app can simply call.

How it was built

I did not write the code. Claude did. My job was to decide what we were building, guide Claude and run every version on real machines, and report back what happened.

The first version arrived within an hour and crashed on my PC. A few rounds later it worked. By the end of the first evening it ran on Windows, Linux and Mac, and on the GPU. The worst bug was on Windows on ARM, where the program simply froze. It could not be reproduced anywhere except on my actual machine, so we hunted it by adding log lines and reading where it stopped.

The second day went into the Rust and Lua versions, documentation, and testing on more machines. From my first question to Claude to this post: about 32 hours on the clock.

I could not have built this in two days by myself, and Claude could not have found those bugs without someone running the code on real hardware. It took both.

Status

Tested on five platforms and three GPUs. The docs say plainly what has not been tested yet, and test reports are welcome.

Credit

None of this would exist without two projects by other people:

- Laya itself, the model, by Nandakishor Mukkunnoth of ConvAI Innovations: https://github.com/NandhaKishorM/laya

- laya.cpp, the C++ port this is built on, by Lars Karlslund: https://github.com/lkarlslund/laya.cpp

The code and docs of the four projects above were written by Claude (Anthropic)

u/felipedaragon — 12 days ago

I have an interview involving C whereas I typically use Python.

I have programmed in C before including on the job, but those experiences never really gets me ready for the wideness of interviews. I was wondering if there's a resource that exposes the things I take for granted in Python (within the standard library) and explains how it works in C.

edit: I just had my interview, thanks for the responses! I went over every link I got

reddit.com
u/darexinfinity — 13 days ago

What are the good practices of managing small dependencies?

I'm new to C. I have a small SDL2 project. At some point I needed to load and draw image sprites and found nice library stb_image.h. Probably best way here would be to just download the stb_image.h file and commit it to the repo, but I thought it would be a nice exercise towards learning how to manage dependencies. Since C doesn't seem to have anything like cargo, PyPi or npm, I started to look for best practices on how to do that.

So, this is what I came up with so far, with a Makefile:

  • dependency file as a target, with commit pinned as separate var
STB_IMAGE_COMMIT := 013ac3beddff3dbffafd5177e7972067cd2b5083

vendor/stb/stb_image.h:
	curl -fsSL --create-dirs https://raw.githubusercontent.com/nothings/stb/$(STB_IMAGE_COMMIT)/stb_image.h -o $@
  • making it prerequisite to the main build target:
build/%.o: src/%.c | build vendor/stb/stb_image.h
	$(CC) $(CFLAGS) -c $< -o $@

This seems simple, reproducible and efficient to me. But I'm not sure it would scale well and also - maybe there are better approaches out there?

reddit.com
u/side2k — 14 days ago

Why your C program loops forever when you type a letter, and how to read input safely

A question that comes up a lot: "I used scanf("%d", &x) and when I type abc my program spins forever."

Why it happens: when scanf can't parse the input as a number, it doesn't consume it. abc stays in the input buffer, the next scanf fails on the same characters, and so on forever.

The fix: check the return value (the number of items read successfully), and throw away the bad line when it fails:

int age;
printf("Enter your age: ");
while (scanf("%d", &age) != 1) {
    int c;
    while ((c = getchar()) != '\n' && c != EOF) { }  /* discard the bad input */
    if (c == EOF) return 1;
    printf("That's not a number. Try again: ");
}

A few other input traps worth knowing:

  • Strings with spaces: scanf("%s") stops at the first space. Use fgets(buf, sizeof buf, stdin), which also can't overflow the buffer.
  • fgets keeps the newline: strip it with buf[strcspn(buf, "\n")] = '\0';
  • Never use gets(): it was removed in C11 because it has no length limit.
  • Missing &: scanf("%d", age) passes a value where an address is expected, which is undefined behavior and usually a crash.

I wrote this up in more detail for beginners, with a worked example and a section on using the input with if/else (including the classic if (x = 5) bug): https://blog.emreguzel.ca/2026/09/29/user-input-in-c-a-complete-guide-for-beginners/

u/PrimeEmre — 12 days ago

I'm creating a language that could replace C in low-level programming—want to join me?

I have been involved in low-level programming for a long time: I write operating systems and have created both a more user-friendly dialect of C and a programming language—designed for driver and firmware development—that can be learned in just a few hours. However, I am reaching out to you now because I cannot handle this task alone. Whether you know how to write low-level code, simply have an interest in the field, or possess a deep understanding of how such programs work, I invite you to join me in creating a programming language specifically designed for an operating system. Thank you for taking the time to read this message—the future is ours to shape.

reddit.com
u/TIRALMA — 13 days ago

Any recommendations for introduction material to using C++ for microcontrollers?

I have been programming Arduino & ESP32 for a while now using C++, but have never engaged with any formal introduction material. With AI/LLM generated content exploding among both major publishers and self-published authors, I am a bit wary to start buying books willy-nilly.

Anyone here that can give solid recommendations on introduction materials on C++ for microcontrollers (preferably that that they themselves have used and found worthwhile)?

I got one recommendation for "Embedded C" by Michael J. Pont (2002), but it is fairly old.

reddit.com
u/WillInternational364 — 13 days ago

How to use sigatomic_t volatile variables in signal handlers instead of calling several functions or making semops?

I'm sorry in advance if this isn't the space for this question, but I can't find much information on the subject. In my project I have a parent who forks several children and at one point, it will send SIGTERM to his still-alive kids. The kids, who are either working or "stuck" on a msgop. Immediately after receiving the signal, they get to... doing other things, including calling other functions and releasing or reserving sempahores, accessing a shared memory, doing other stuff based on whether they can make these semops. After these operations, the kids exit.

While my project still works like this, as it's a relatively small project, I'd like to code as correctly as possible and I read that signal handlers should be very short, preferably just few lines of code (like changing the sigatomic_t volatile variable's value). While it's my understanding that I'm supposed to check said variable's value somewhere in the code (and if it's changed, do everything I'm doing in the handler), I don't understand where I'm supposed to check its value because signals can interrupt the kids at any point in time.

Any sort of help, examples, explanations are extremely appreciated; I thank you in advance. Or even just someone pointing me to where I can find more examples, that specifically mention signal handlers and how my functions are supposed to be called immediately, that's also great.

(I can only use System V IPCs and none of my processes access the internet, in case it's relevant in any way)

reddit.com
u/Avi250 — 13 days ago