C2011 'timespec': 'struct' type redefinition error.

closed account (Lv7SvCM9)
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

long long sum = 0;

// Thread function to generate sum of 0 to N

void* sum_runner(void* arg) {
long long *limit_ptr = (long long*) arg;
long long limit = *limit_ptr;

for (long long 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);
}

long long 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);
}
> C2011 'timespec': 'struct' type redefinition error.

See https://stackoverflow.com/a/49705348
Works for me (gcc / Ubuntu).
1
2
3
4
$ gcc main.c -pthread
$ ./a.out 1000
Sum is 500500
$ 


Remember to use code tags in future.
https://www.cplusplus.com/articles/jEywvCM9/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

// The Sum computed by the background thread

long long sum = 0;

// Thread function to generate sum of 0 to N

void *sum_runner(void *arg)
{
  long long *limit_ptr = (long long *) arg;
  long long limit = *limit_ptr;

  for (long long 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);
  }

  long long 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);
}

Topic archived. No new replies allowed.