9

While executing a Pthread program in C using Visual Studio 2015, I got the following error:

Error C2011 'timespec': 'struct' type redefinition

The following is my code:

#include<pthread.h>
#include<stdlib.h>
#include<stdio.h>


void *calculator(void *parameter);

int main(/*int *argc,char *argv[]*/)
{
    pthread_t thread_obj;
    pthread_attr_t thread_attr;
    char *First_string = "abc"/*argv[1]*/;
    pthread_attr_init(&thread_attr);
    pthread_create(&thread_obj,&thread_attr,calculator,First_string);
        
}
void *calculator(void *parameter)
{
    int x=atoi((char*)parameter);
    printf("x=%d", x);
}
Rachid K.
  • 4,490
  • 3
  • 11
  • 30
Vijay Manohar
  • 473
  • 1
  • 7
  • 22

5 Answers5

9

Despite this question is already answered correctly, there is also another way to solve this problem.

First, problem occurs because pthreads-win32 internally includes time.h which already declares timespec struct.

To avoid this error the only thing we should do is this:

#define HAVE_STRUCT_TIMESPEC
#include <pthread.h>
NutCracker
  • 11,485
  • 4
  • 44
  • 68
8

Add this compiler flag:

-DHAVE_STRUCT_TIMESPEC
user_0
  • 3,173
  • 20
  • 33
  • but now i have a new problem, int main(){pthread_create(&threads[t], NULL, PrintHello, (void *)t);} void *PrintHello(void *threadid){printf("Hello world");pthread_exit(NULL);} It is a Void method but still its asking me to return value – Vijay Manohar Nov 22 '15 at 02:24
1

The same problem happens when compiling programs in Visual Studio 2015 that include MariaDB 10 header files (saw it with 10.1.14).

The solution there is to define the following:

STRUCT_TIMESPEC_HAS_TV_SEC
STRUCT_TIMESPEC_HAS_TV_NSEC
Joao Costa
  • 2,563
  • 1
  • 21
  • 15
0

On Visual Studio 2015.

I solve the problem adding:

#define _TIMESPEC_DEFINED
-6

Delete all instances of 'TIMESPEC' in pthread.h (Make a backup first.)

If I understand it correctly, you probably downloaded pthreads and tried installing it into your VS.

But the pthreads.h file doesn't play nicely with the TIMESPEC defintions already defined in some other header file.

So, delete the portions of the pthreads.h file where TIMESPEC is defined.

  • 4
    I would strongly recommend not deleting parts of an included header file from a library, this is asking for incompatibility issues in deployment and when working with other developers. – TaRDy Dec 15 '15 at 15:31
  • Deleting from pthread.h could cause problems compiling other programs later on – Joao Costa Jun 03 '16 at 08:52