Skip to main content

Posts

Showing posts from March, 2015

Casting an int to void pointer in c++

I was a bit confused with the following piece of code which just creates a number of threads and assigns them an id. #include "pthread.h" #include "stdio.h" #include "stdlib.h" #define N 5 void *worker_thread(void *arg) {     printf("This is worker_thread #%ld\n", (long) arg);     pthread_exit(NULL); } int main() {     pthread_t my_thread[N];     long id;     for (id = 1; id <= N; id++) {         int ret = pthread_create(&my_thread[id], NULL, &worker_thread, (void*) id);         if (ret != 0) {             printf("Error: pthread_create() failed\n");             exit(EXIT_FAILURE);         }     }     pthread_exit(NULL); } The function prototype was; int pthread_create(pthread_t * thread , const pthread_attr_t * attr , void *(* start_routine ) (void *), void * arg );   The arg was void*, Shouldn't we have; int ret = pthread_create(&my_thread[id], NULL, &wor