35

I'm learning Pthreads. My code executes the way I want it to, I'm able to use it. But it gives me a warning on compilation.

I compile using:

gcc test.c -o test -pthread

with GCC 4.8.1. And I get the warning

test.c: In function ‘main’:
test.c:39:46: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
     pthread_create(&(tid[i]), &attr, runner, (void *) i);
                                              ^
test.c: In function ‘runner’:
test.c:54:22: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
   int threadnumber = (int) param;
                      ^

This error comes for the following code:

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

#define MAX_THREADS 10

int sum; /* this data is shared by the thread(s) */
void *runner(void * param);

int main(int argc, char *argv[])
{
  int num_threads, i;
  pthread_t tid[MAX_THREADS];     /* the thread identifiers  */
  pthread_attr_t attr; /* set of thread attributes */

  if (argc != 2) {
    fprintf(stderr, "usage:  test <integer value>\n");
    exit(EXIT_FAILURE);
  }

  if (atoi(argv[1]) <= 0) {
    fprintf(stderr,"%d must be > 0\n", atoi(argv[1]));
    exit(EXIT_FAILURE);
  }

  if (atoi(argv[1]) > MAX_THREADS) {
    fprintf(stderr,"%d must be <= %d\n", atoi(argv[1]), MAX_THREADS);
    exit(EXIT_FAILURE);
  }

  num_threads = atoi(argv[1]);
  printf("The number of threads is %d\n", num_threads);

  /* get the default attributes */
  pthread_attr_init(&attr);

  /* create the threads */
  for (i=0; i<num_threads; i++) {
    pthread_create(&(tid[i]), &attr, runner, (void *) i);
    printf("Creating thread number %d, tid=%lu \n", i, tid[i]);
  }

  /* now wait for the threads to exit */
  for (i=0; i<num_threads; i++) {
    pthread_join(tid[i],NULL);
  }
  return 0;
}

/* The thread will begin control in this function */
void *runner(void * param)
{
  int i;
  int threadnumber = (int) param;
  for (i=0; i<1000; i++) printf("Thread number=%d, i=%d\n", threadnumber, i);
  pthread_exit(0);
}

How can I fix this warning?

user159
  • 1,343
  • 2
  • 12
  • 20

4 Answers4

63

A quick hacky fix might just to cast to long instead of int. On a lot of systems, sizeof(long) == sizeof(void *).

A better idea might be to use intptr_t.

int threadnumber = (intptr_t) param;

and

pthread_create(&(tid[i]), &attr, runner, (void *)(intptr_t)i);
tangrs
  • 9,709
  • 1
  • 38
  • 53
  • 1
    or cast to `long long` on 64-bit machines / compilers. – thejinx0r Sep 11 '17 at 06:11
  • @thejinx0r Sure but it's still a hack in the sense that you can't rely on `sizeof(long long) == sizeof(void *)`. – tangrs Sep 11 '17 at 07:02
  • 1
    @NikhilWagh again, you can't always rely on `sizeof(size_t) == sizeof(void *)`. – tangrs Oct 23 '17 at 03:30
  • Nothing worked for me on 64-bit machine. I had to declare variable of long type before passing as an argument to thread function. `long i; pthread_create(&pid[i], NULL, displayThread, (void *)i);` – Fahad Naeem Nov 01 '19 at 08:27
2
pthread_create(&(tid[i]), &attr, runner, (void *) i);

You are passing the local variable i as an argument for runner, sizeof(void*) == 8 and sizeof(int) == 4 (64 bits).

If you want to pass i, you should wrap it as a pointer or something:

void *runner(void * param) {
  int id = *((int*)param);
  delete param;
}

int tid = new int; *tid = i;
pthread_create(&(tid[i]), &attr, runner, tid);

You may just want i, and in that case, the following should be safe (but far from recommended):

void *runner(void * param) {
  int id = (int)param;
}

pthread_create(&(tid[i]), &attr, runner, (void*)(unsigned long long)(i));
Jamal
  • 763
  • 7
  • 22
  • 32
ichramm
  • 6,437
  • 19
  • 30
1

I was also getting the same warning. So to resolve my warning I converted int to long and then this warning just vanished. And about the warning "cast to pointer from integer of different size" you can leave this warning because a pointer can hold the value of any variable because pointer in 64x is of 64 bit and in 32x is of 32 bit.

newbie
  • 25
  • 4
-4

Try passing

pthread_create(&(tid[i]), &attr, runner, (void*)&i);
Pang
  • 9,564
  • 146
  • 81
  • 122