0

The following code is returning the error:

error C2664: 'pthread_create' : cannot convert parameter 3 from 'void *(__cdecl *)(void)' to 'void *(__cdecl *)(void *)'

error C2664: 'pthread_create' : cannot convert parameter 3 from 'void *(__cdecl *)(void)' to 'void *(__cdecl *)(void *)'

Code:

#include <windows.h> 
#include <stdio.h>
#include <pthread.h> 
int main()  {
  pthread_t f2_thread, f1_thread; 
  void *f2(), *f1();
  int i1,i2;
  i1 = 1;
  i2 = 2;
  pthread_create(&f1_thread,NULL,f1,&i1);
  pthread_create(&f2_thread,NULL,f2,&i2);
  pthread_join(f1_thread,NULL);
  pthread_join(f2_thread,NULL);
  
  return 0;

}
void *f1(int *x){
  int i;
  i = *x;
 Sleep(1);
  printf("f1: %d",i);
  pthread_exit(0); 
}
void *f2(int *x){
  int i;
  i = *x;
 Sleep(1);
  printf("f2: %d",i);
  pthread_exit(0); 
}

Environment:

2 Answers 2

0

Not sure if this answers your question (or what your question was), but here is some code that compiles and gives what you might expect for the output:

    #include <windows.h> 
    #include <stdio.h>
    #include <pthread.h> 
    int main()  {
      pthread_t f2_thread, f1_thread; 
      void *f2(void*), *f1(void*);
      int i1,i2;
      i1 = 1;
      i2 = 2;
      pthread_create(&f1_thread,NULL,f1,&i1);
      pthread_create(&f2_thread,NULL,f2,&i2);
      pthread_join(f1_thread,NULL);
      pthread_join(f2_thread,NULL);

      return 0;

    }
    void *f1(void *x){
  int* data = static_cast<int*>(x);
      int i = *data;
      Sleep(1);
      printf("f1: %d",i);
      pthread_exit(0); 
      return 0;
    }
    void *f2(void *x){
      int* data = static_cast<int*>(x);
      int i = *data;
      Sleep(1);
      printf("f2: %d",i);
      pthread_exit(0); 
      return 0;
    }

So

  1. Having void* arguments in the prototypes then casting those to int*
  2. Having each function return 0
Sign up to request clarification or add additional context in comments.

Comments

0

Please add "return NULL:" before exit your thread functions.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.