I have the following code and when I try to compiled it I get a C2011 'timespec': 'struct' type redefinition error. Can someone please tell me what is my error, I need it for a homework this Friday thanks.
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
// The Sum computed by the background thread
longlong sum = 0;
// Thread function to generate sum of 0 to N
void *sum_runner(void *arg)
{
longlong *limit_ptr = (longlong *) arg;
longlong limit = *limit_ptr;
for (longlong i = 0; i <= limit; i++) {
sum += i;
}
// sum is a global variable, so other threads can access.
pthread_exit(0);
}
int main(int argc, char **argv)
{
if (argc < 2) {
printf("Usage: %s <num>\n", argv[0]);
exit(-1);
}
longlong limit = atoll(argv[1]);
// Thread ID:
pthread_t tid;
// Create attributes
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_create(&tid, &attr, sum_runner, &limit);
// Wait until thread is done its work
pthread_join(tid, NULL);
printf("Sum is %lld\n", sum);
}