1 /* Stress test for the --free-is-write command-line option. */
12 #define MALLOC_SIZE 22816
13 #define THREAD_COUNT 10
14 #define MALLOC_COUNT 1000
16 static pthread_cond_t cond
= PTHREAD_COND_INITIALIZER
;
17 // 'mutex' protects 'count'.
18 static pthread_mutex_t mutex
= PTHREAD_MUTEX_INITIALIZER
;
19 static unsigned count
;
21 void* thread_func(void* arg
)
25 for (i
= 0; i
< MALLOC_COUNT
; ++i
) {
28 ptr
= malloc(MALLOC_SIZE
);
29 memset(ptr
, 0, MALLOC_SIZE
);
33 pthread_mutex_lock(&mutex
);
35 pthread_cond_signal(&cond
);
36 pthread_mutex_unlock(&mutex
);
41 int main(int argc
, char **argv
)
43 pthread_t thread
[THREAD_COUNT
];
47 for (i
= 0; i
< THREAD_COUNT
; i
++) {
49 pthread_attr_init(&attr
);
50 pthread_attr_setstacksize(&attr
, PTHREAD_STACK_MIN
+ 4096);
51 result
= pthread_create(&thread
[i
], &attr
, thread_func
, 0);
52 pthread_attr_destroy(&attr
);
56 pthread_mutex_lock(&mutex
);
57 while (count
< THREAD_COUNT
&& pthread_cond_wait(&cond
, &mutex
) == 0)
59 pthread_mutex_unlock(&mutex
);
61 for (i
= 0; i
< THREAD_COUNT
; i
++)
62 pthread_join(thread
[i
], 0);
66 fprintf(stderr
, "Done.\n");