Template
64 lines
1.6 KiB
C
64 lines
1.6 KiB
C
/*
|
|
* main.c - integration-fixture entry point (plan todo 26).
|
|
*
|
|
* A REAL C23 program exercising both fixture features end to end:
|
|
* - pthread: pthread_create()/pthread_join() run a worker thread (guarded
|
|
* by the HAVE_PTHREAD define the generated config.h wrote), printing a
|
|
* grep-able marker the runner asserts on;
|
|
* - math: sin(0.5) from libm, printed with a fixed format the runner
|
|
* greps (the link would fail without -lm, so a successful run proves
|
|
* the LIBS accumulation reached the Makefile).
|
|
*
|
|
* _POSIX_C_SOURCE must be defined BEFORE the first include: under strict
|
|
* -std=c23 glibc sets __STRICT_ANSI__ and hides pthread_create's
|
|
* declaration, which C23 turns into an implicit-declaration ERROR
|
|
* (the same trap recorded for src/ in the project learnings).
|
|
*/
|
|
|
|
#ifndef _POSIX_C_SOURCE
|
|
#define _POSIX_C_SOURCE 200809L
|
|
#endif
|
|
|
|
#include "config.h"
|
|
#include "demo.h"
|
|
|
|
#include <math.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
#ifdef HAVE_PTHREAD
|
|
#include <pthread.h>
|
|
|
|
static void *
|
|
worker(void *arg)
|
|
{
|
|
(void)arg;
|
|
printf("worker thread ran\n");
|
|
return NULL;
|
|
}
|
|
#endif
|
|
|
|
int
|
|
main(void)
|
|
{
|
|
double s = sin(0.5);
|
|
|
|
printf("hellothreads demo: sin(0.5) = %.4f\n", s);
|
|
printf("demo_compute(2) = %d\n", demo_compute(2));
|
|
#ifdef HAVE_PTHREAD
|
|
{
|
|
pthread_t t;
|
|
|
|
if (pthread_create(&t, NULL, worker, NULL) != 0) {
|
|
fprintf(stderr, "pthread_create failed\n");
|
|
return EXIT_FAILURE;
|
|
}
|
|
if (pthread_join(t, NULL) != 0) {
|
|
fprintf(stderr, "pthread_join failed\n");
|
|
return EXIT_FAILURE;
|
|
}
|
|
}
|
|
#endif
|
|
return EXIT_SUCCESS;
|
|
}
|