ted777
ted777

Reputation: 3

Ansi C parameter to the thread function get compiler error

I get error with pararmeter argument of CreateThread function : error C2440: 'function' : cannot convert from 'int (__cdecl *)(BIGVAR *)' to 'LPTHREAD_START_ROUTINE' For every thread another list of variables for read/write and all other are readable only. Call thread function directly thread(bigvar[2]); works fine. What I have to do to give every thread a pointer for every variables list ?

#define NUM_THREADS 5    
int thread(BIGVAR *bv);    
    
typedef struct typBIGVAR
{
 
 int var1;
 int var2[30];
 
 char var3];
 ...
 // many other variables
 
 }  BIGVAR;
 
 
int main ( int argc, char *argv[] )
{

  buffer = (char *) malloc(sizeof(BIGVAR) * NUM_THREADS);
  Mem = buffer;

  BIGVAR *bigvar[NUM_THREADS]; 
  
  unsigned long ThreadId[NUM_THREADS];
  
  int arrayThread[NUM_THREADS];
  
  
 
  for ( t=0; t<NUM_THREADS; t++ )
  {
    bigvar[t] = (BIGVAR *) Mem;
    Mem += sizeof(BIGVAR);
  }


  thread( bigvar[2] ); // this works fine 



  for ( t=0; t < NUM_THREADS; t++ )
  {
    

    bv = bigvar[t];
    arrayThread[t] = CreateThread(NULL, 0, &thread, bigvar[t], 0, &ThreadId[t]);
    if (arrayThread[t] == NULL)
    {
      printf("Create Thread %d get failed.\n",arrayThread[t]); 
    }
  
  }
 }

Upvotes: 0

Views: 48

Answers (1)

dbush
dbush

Reputation: 225537

Your thread function is not of the correct type to pass to CreateThread.

The function thread is of type int (*)(BIGVAR *) but the function pointer type expected by CreateThread is DWORD WINAPI (*)(LPVOID lpParameter). These types are not compatible, and calling a function through an incompatible function pointer type triggers undefined behavior.

Change the signature of your function to be:

int thread(void *bv);

And inside of the function convert the parameter from void * to BIGVAR *.

Upvotes: 1

Related Questions