 #else
         _ = fn()
 #endif
-        usleep(100_000) // allocs/frees happen on multiple threads, allow some cool down time
+        waitForThreadsToQuiesce(shouldReachZero: !throwAway)
         let frees = AtomicCounter.read_free_counter()
         let mallocs = AtomicCounter.read_malloc_counter()
         let mallocedBytes = AtomicCounter.read_malloc_bytes_counter()
+        if mallocs - frees < 0 {
+            print("WARNING: negative remaining allocation count, skipping.")
+            return nil
+        }
         return [
             "total_allocations": mallocs,
             "total_allocated_bytes": mallocedBytes,

import Glibc
#endif

func measureAll(_ fn: () -> Int) -> [[String: Int]] {
    func measureOne(_ fn: () -> Int) -> [String: Int] {
        AtomicCounter.reset_free_counter()
        AtomicCounter.reset_malloc_counter()
        AtomicCounter.reset_malloc_bytes_counter()

        ]
    }

    _ = measureOne(throwAway: true, fn) /* pre-heat and throw away */

    var measurements: [[String: Int]] = []
    for _ in 0..<10 {
        if let results = measureOne(fn) {
            measurements.append(results)
        }
    }
    return measurements
}
