Entries in the ‘Code’ Category:

Explorations on Random Numbers, Part 4

Astute readers will notice that the sample code in the previous post passes in a constant value as the seed to the random number generator. What would happen if we tried passing in the current time? Obviously, that would completely prevent the optimization we saw last time that just calculates the result, since the compiler [...]

Leave a Comment

Explorations on Random Numbers, Part 2.5

Before we delve into the nitty-gritty details of how the compiler optimizes our MWC RNG, I’d like to present our full code from the GSDEngine (modified slightly for public consumption). All of this code is released into the public domain. Here’s a C-style version (but still compiled with C++ compiler): // This is based off [...]

Leave a Comment

Explorations on Random Numbers, Part 3

The last few posts, we’ve explored various random number generators, and ended up with a very simple and solid implementation of George Marsaglia’s Multiply-With-Carry RNG. At this point, we’re done talking about random number generators. We’re going to switch gears and talk about how the compiler actually takes the RNG code and turns it into [...]

Leave a Comment

Explorations on Random Numbers, Part 2

Last time, we saw how to use rand() and why rand() is unacceptable. We also took a look at the Mersenne Twister, which is definitely a huge improvement over rand(), but we were put off by its memory footprint of 2.5K. So, how can we do better than Mersenne Twister? Enter George Marsaglia, in a [...]

Leave a Comment

Explorations on Random Numbers, Part 1

In C, since time immemorial, the easiest way of getting a random number is thus: #include <stdio.h> #include <stdlib.h> #include <time.h> int main() { srand(time(NULL)); int nRandom = rand(); printf(“%d\n”, nRandom); return 0; } Of course, this only gets you part of the way there. The problems with rand() are well-known and well-documented, but the [...]

Leave a Comment

Multithreaded Programming Part 3: Going Lockless

Ultimately, the whole point of a lock is to take a multi-part operation and make it atomic. The AddDagron() function from the previous post in the series, for example, takes the operations of incrementing the Dagron count, reallocating the array of Dagron pointers, and adding the new Dagron to the array, and effectively turns it [...]

Leave a Comment

Multithreaded Programming Part 2.5: MRSW Lock Code

<– Part 2. As promised, here is our modified version of Glenn Slayden‘s Multi-Reader-Single-Writer lock code. We’d like to thank Glenn for posting the original code. // // Multi-reader Single-writer concurrency base class for Win32 // // (c) 1999-2003 by Glenn Slayden (glenn@glennslayden.com) // // http://www.glennslayden.com/code/win32/reader-writer-lock // This code has been modified from the original, [...]

Comments (1)

Multithreaded Programming Part 2: Multiple Reader/Single Writer Lock

<–Part 1 Last time, we saw how to protect a shared resource with a critical section. Today we’ll look at why a critical section, though technically correct, can cause problems. Let’s write some critical-section-protected code and see what might happen: SharedResource gSharedResource; CRITICAL_SECTION g_csResourceLock; // Assume that this has been initialized properly Glob GlobulateSharedResource() { [...]

Comments (2)

Multithreaded Programming Part 1: The Critical Section Lock

Most of the time, when we’re programming, we can think about our programs in a very sequential fashion: First do this, then do that, then finish with the other. This is even true with modern, multicore architectures, including the PC, XBox 360, and PlayStation 3. Usually, it’s for one of two reasons: Either there is [...]

Leave a Comment

Tips For Working With Unity #4: Coding and General Tips

Editor’s note: This is part 4 in a 5 part series outlining our experiences with Unity & Unity iPhone. Be sure to check out Part 1, Part 2, and Part 3. Unity is like a big, old playground.  It’s easy to play with all of the toys and have fun, but you can very easily [...]

Comments (2)