1 /* Stress test for the --free-is-write command-line option. */
11 #define MALLOC_SIZE 22816
12 #define THREAD_COUNT 10
13 #define MALLOC_COUNT 1000
15 static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
16 // 'mutex' protects 'count'.
17 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
18 static unsigned count;
20 void* thread_func(void* arg)
24 for (i = 0; i < MALLOC_COUNT; ++i) {
27 ptr = malloc(MALLOC_SIZE);
28 memset(ptr, 0, MALLOC_SIZE);
32 pthread_mutex_lock(&mutex);
34 pthread_cond_signal(&cond);
35 pthread_mutex_unlock(&mutex);
40 int main(int argc, char **argv)
42 pthread_t thread[THREAD_COUNT];
46 for (i = 0; i < THREAD_COUNT; i++) {
47 result = pthread_create(&thread[i], 0, thread_func, 0);
51 pthread_mutex_lock(&mutex);
52 while (count < THREAD_COUNT && pthread_cond_wait(&cond, &mutex) == 0)
54 pthread_mutex_unlock(&mutex);
56 for (i = 0; i < THREAD_COUNT; i++)
57 pthread_join(thread[i], 0);
61 fprintf(stderr, "Done.\n");