int main(void)
{
#if NTHREADS > 0
int i, n;
# ifdef GC_PTHREADS
int err;
pthread_t th[NTHREADS_INNER];
# else
HANDLE th[NTHREADS_INNER];
# endif
GC_INIT();
for (i = 0; i < NTHREADS_INNER; ++i) {
# ifdef GC_PTHREADS
err = pthread_create(&th[i], NULL, entry, 0);
if (err) {
fprintf(stderr, "Thread creation failed, error: %s\n",
strerror(err));
if (i > 0 && EAGAIN == err) break;
exit(1);
}
# else
{
DWORD thread_id;
th[i] = CreateThread(NULL, 0, entry, 0, 0, &thread_id);
}
if (th[i] == NULL) {
fprintf(stderr, "Thread creation failed, errcode= %d\n",
(int)GetLastError());
exit(1);
}
# endif
}
n = i;
for (i = 0; i < n; ++i) {
# ifdef GC_PTHREADS
void *res;
err = pthread_join(th[i], &res);
if (err) {
fprintf(stderr, "Failed to join thread, error: %s\n",
strerror(err));
# if defined(__HAIKU__)
/* The error is just ignored (and the test is ended) to */
/* workaround some bug in Haiku pthread_join. */
/* TODO: The thread is not deleted from GC_threads. */
if (ESRCH == err) break;
# endif
exit(1);
}
# else
if (WaitForSingleObject(th[i], INFINITE) != WAIT_OBJECT_0) {
fprintf(stderr, "Failed to join thread, errcode= %d\n",
(int)GetLastError());
CloseHandle(th[i]);
exit(1);
}
CloseHandle(th[i]);
# endif
}
#endif
printf("subthread_create: created %d threads (%d ended)\n",
(int)AO_load(&thread_created_cnt), (int)AO_load(&thread_ended_cnt));
return 0;
}
#else
int main(void)
{
printf("subthread_create test skipped\n");
return 0;
}