int thread_id;
GThread* gthread;
- GMutex* to_free_mutex;
+ GMutex to_free_mutex;
void* to_free [N_THREADS * N_ALLOCS];
int bytes_to_free [N_THREADS * N_ALLOCS];
int n_to_free;
int n_freed;
} tdata[N_THREADS];
-void*
+static void *
thread_func (void *arg)
{
struct ThreadData *td = arg;
/* g_print ("Thread %d starting\n", td->thread_id); */
for (i = 0; i < N_ALLOCS; i++)
{
+ int bytes;
+ char *mem;
+ int f;
+ int t;
+
if (rand() % (N_ALLOCS / 20) == 0)
g_print ("%c", 'a' - 1 + td->thread_id);
/* allocate block of random size and randomly fill */
- int bytes = rand() % MAX_BLOCK_SIZE + 1;
- char *mem = g_slice_alloc (bytes);
- int f;
+ bytes = rand() % MAX_BLOCK_SIZE + 1;
+ mem = g_slice_alloc (bytes);
+
for (f = 0; f < bytes; f++)
mem[f] = rand();
/* associate block with random thread */
- int t = rand() % N_THREADS;
- g_mutex_lock (tdata[t].to_free_mutex);
+ t = rand() % 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;
tdata[t].n_to_free++;
- g_mutex_unlock (tdata[t].to_free_mutex);
+ g_mutex_unlock (&tdata[t].to_free_mutex);
/* shuffle thread execution order every once in a while */
if (rand() % 97 == 0)
}
/* free a block associated with this thread */
- g_mutex_lock (td->to_free_mutex);
+ g_mutex_lock (&td->to_free_mutex);
if (td->n_to_free > 0)
{
td->n_to_free--;
g_slice_free1 (td->bytes_to_free[td->n_to_free], td->to_free[td->n_to_free]);
td->n_freed++;
}
- g_mutex_unlock (td->to_free_mutex);
+ g_mutex_unlock (&td->to_free_mutex);
}
return NULL;
}
int
-main()
+main (void)
{
int t;
- g_thread_init (NULL);
-
for (t = 0; t < N_THREADS; t++)
{
tdata[t].thread_id = t + 1;
tdata[t].n_to_free = 0;
tdata[t].n_freed = 0;
- tdata[t].to_free_mutex = g_mutex_new();
}
g_print ("Starting %d threads for concurrent GSlice usage...\n", N_THREADS);
for (t = 0; t < N_THREADS; t++)