Blogs

C++, Threads and Linux Audio: A Brief Retrospective

tung's picture

I just spent the last few days trawling through someone else's multi-threaded C++ Linux audio code, so I thought I'd share a few of my experiences.

First: The commit.

That code converts direct audio hardware access via ALSA into newer plugin-based output. It also fixes a race condition that occurs at the seam of the two subsystems involved: the client code provided a mixer callback that used a buffer that is initialised immediately after the server spawns the thread that needs it. It's a mistake made by the code's author, but to be fair I wouldn't have spotted it either if the code hadn't segfaulted on me.

If you ever find yourself in this situation, here are some tips:

  • Print statements really work. Yes, even with threads. Especially with C++, where control flow isn't obvious.

  • In threaded code, if you just insert print statements and behaviour changes, that's almost certainly a sign of a race condition.

  • C++ is harder to grep through than C, but it still works, if less effectively. C++ adds a lot of context that makes individual identifiers less useful on the isolated lines printed by grep.

  • Persistence pays. Even if the best programmers in the world say that threaded debugging is difficult, it can still be done.

  • If you're in college/university and going the computer/software route, take the Operating Systems course(s). Threads and concurrency get a lot of good exposure in them.

Read more »

Building Thatcher Ulrich's luaSDL for Lua 5.1 on Windows with MinGW

tung's picture

luaSDL is a library that can be loaded by Lua to make use of SDL. To get a fresh new luaSDL.dll that Lua 5.1 can load, read on!

Ingredients

Read on to get a fresh new luaSDL.dll for you to load and use!

Read more »

Making an RPG with Quipkit (Hypothetically)

tung's picture

Quipkit is a work-in-progress (read: not even near finished) indie RPG dev kit. This blog entry describes how we might make an RPG using Quipkit.

1. Vague Game Description

Most games start as vague ideas, and indie RPGs are no exception. Here's one for our hypothetical RPG:

You play a guy in a traditional fantasy RPG. You walk around a map of grass and water, and every so often you encounter a monster. You exchange blows with said monster in the battle system until either you or it dies. If you win, you gain experience. With enough experience, the guy can gain a level and grow in strength. This continues until the player chooses to exit.

It shall be dubbed... Island Dude.

How would be breathe life into this little RPG? Read on!

Read more »

Installing Git on Windows, as of June 2010

tung's picture

As of June 2010, there's no one-click installer of Git for Windows. Following this blog entry will get you:

  • msysgit, without its shell integration
  • TortoiseGit shell integration instead

msysgit's shell integration is nasty to uninstall, and if you do it wrong it'll pop up a dialog every 10 seconds.

Here's how you do it...

Read more »

How do I autoconf?

tung's picture

I was working on a project when general upgrade instability caused it to stop working on my development machine (read: starting OpenGL kills X for some reason). Off of Arch and onto Ubuntu 9.04, and I find that my Linux-based project won't build under Linux.

Autoconf is a set of tools that smooths over differences in build environments, like different file system conventions, library names, search paths, tool chains, and so on.

The following is based on a series of long articles over at the Free Software Magazine: the only tutorial/guide that didn't attempt to shove 20 years of history down my throat from the get-go. It's my personal reference and I'll update it as I learn.

Read more »

libpng's man page makes a crap tutorial

tung's picture

If there were a competition for world's worst man page, libpng's would be right up there. Instead of individual man pages for each function and struct, it rolls everything into the one huge document. That would be acceptable if it gave descriptions for each of the functions, but it doesn't, instead providing an nigh-impenetrable wall of text which is borderline useless as a guide or a reference.

Misgivings aside, I've managed to extract the useful information for the simple loading of a PNG image and put it in the code sample below:

Read more »

Where in the world is me?

tung's picture

Another 1 hour upgrade for Drupal. More voodoo rain dances required, but this time, the core Update Status checker module doesn't work: enabling it causes all admin pages to blank out.

It's just as well, because I've done most of my updating elsewhere.

@tungtn is where I post interesting articles that I find on Hacker News, and the occasional thought.

I talk with people on Google Buzz. If you've got Gmail, check it out, and feel free to follow.

I think that's about it.

Read more »

JavaScript: Does removing comments from a function make it faster?

tung's picture

A few days ago putting comments in a function slow them down. I decided to put this to the test: I couldn't believe it unless I saw it.

The method I used was a statistical hypothesis test. I'm not here to teach maths (I wouldn't be very good at it anyway), but the process involves gathering raw data, setting up hypotheses about what distribution those data fit, and calculating how likely the data fit that distribution.

Read more »

Max-heap in Go

tung's picture

I was helping somebody on a forum a few days ago with a C++ max-heap implementation. I thought it might be fun to try it out in Go.

package main
 
import (
    "container/vector";
    "flag";
    "rand";
    "time";
)
 
var num = flag.Int("n", 10, "number of elements to heap sort")
 
func main() {
    flag.Parse();
    rand.Seed(time.Nanoseconds());
    v := vector.NewIntVector(*num);
    for i := 1; i <= *num; i++ {
        v.Set(i - 1, rand.Int() % 100)
    }
    heapSort(v);
    printHeap(v, 0, 0);
}
 
func left(i int) int {
    return i * 2 + 1
}
 
func right(i int) int {
    return i * 2 + 2
}
 
func parent(i int) int {
    return (i - 1) / 2
}
 
// Swap vec[i] with its parent if its larger. Stop at the top.
func maxHeapify(vec *vector.IntVector, i int) {
    if i <= 0 {
        return
    }
    p := parent(i);
    if vec.At(p) < vec.At(i) {
        temp := vec.At(p);
        vec.Set(p, vec.At(i));
        vec.Set(i, temp);
        maxHeapify(vec, p);
    }
}
 
// Given a list of numbers in any order, max heap sort it.
func heapSort(vec *vector.IntVector) {
    for i := 0; i < vec.Len(); i++ {
        maxHeapify(vec, i)
    }
}
 
func printHeap(vec *vector.IntVector, i int, nest int) {
    if i >= vec.Len() {
        return
    }
    for n := 0; n < nest; n++ {
        print("    ")
    }
    print(vec.At(i));
    print("\n");
    printHeap(vec, left(i), nest + 1);
    printHeap(vec, right(i), nest + 1);
}
Read more »

goblog: A proof-of-concept blog in Go

tung's picture

As if I wasn't bored enough, I've written a proof-of-concept blog using Go, that new language out of Google. I blogged about this language before, but I wanted to give it a real test drive, so I came up with this.

goblog on github

Pointers ahoy

The first thing I noticed when I got started was pointer trouble on my part. Go still has pointers like in C, but some of the syntax smooths over the differences.

Read more »
Syndicate content