From 9f558a2c5017860c92d69396d36dc7a6b6a4e2af Mon Sep 17 00:00:00 2001 From: Alexander Richardson Date: Fri, 16 Dec 2022 17:00:56 +0000 Subject: [PATCH] tests/slice-concurrent.c: avoid using rand() from multiple threads --- glib/tests/slice-concurrent.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/glib/tests/slice-concurrent.c b/glib/tests/slice-concurrent.c index 568e67cf3..b20a62e77 100644 --- a/glib/tests/slice-concurrent.c +++ b/glib/tests/slice-concurrent.c @@ -49,18 +49,18 @@ thread_func (void *arg) int bytes, f, t; char *mem; - if (rand() % (N_ALLOCS / 20) == 0) + if (g_random_int_range (0, N_ALLOCS / 20) == 0) g_test_message ("%c", 'a' - 1 + td->thread_id); /* allocate block of random size and randomly fill */ - bytes = rand() % MAX_BLOCK_SIZE + 1; + bytes = g_random_int_range (0, MAX_BLOCK_SIZE + 1); mem = g_slice_alloc (bytes); for (f = 0; f < bytes; f++) - mem[f] = rand(); + mem[f] = (char) g_random_int (); /* associate block with random thread */ - t = rand() % N_THREADS; + t = g_random_int_range (0, N_THREADS); g_mutex_lock (&tdata[t].to_free_mutex); tdata[t].to_free[tdata[t].n_to_free] = mem; tdata[t].bytes_to_free[tdata[t].n_to_free] = bytes; @@ -68,9 +68,9 @@ thread_func (void *arg) g_mutex_unlock (&tdata[t].to_free_mutex); /* shuffle thread execution order every once in a while */ - if (rand() % 97 == 0) + if (g_random_int_range (0, 97) == 0) { - if (rand() % 2) + if (g_random_boolean ()) g_thread_yield(); /* concurrent shuffling for single core */ else g_usleep (1000); /* concurrent shuffling for multi core */