25147

Print arguments inside thread

Question:

void * consumer_child(void *arguments){ Range rng = *((Range *) arguments); //prinnting with the range to easily identify each thread printf("consumer_child[%d-%d] started\n", rng.start, rng.end ); pthread_exit(0); }

When I print it, it prints memory location, not the value. I need to print the value.

In main thread start and end values are set properly. I have checked them.

in main I have set the argument as following

Range *rng = malloc(sizeof(*rng)); rng->start = i * numbersPerChild; rng->end = (numbersPerChild * (i + 1)) -1 ; printf("Range for thread %d is %d to %d\n", i, rng->start, rng->end ); printf("test print %d\n",rng->start); pthread_create(&tid[i], NULL, consumer_child, (void *)&rng );

Range is a struct

typedef struct { int start; int end; } Range;

Answer1:

You need to change:

pthread_create(&tid[i], NULL, consumer_child, (void *)&rng );

to:

pthread_create(&tid[i], NULL, consumer_child, rng);

since rng is already a pointer, and you want to pass that, not its address. You don't need to cast an object pointer to void * in C, unless you have a variadic function that expects one and you're trying to pass it a different kind of object pointer.

Recommend

  • Problem Linking “static” Methods in C++
  • How to reuse threads using pthread_exit()
  • “Un-casting” from (void *) and de-referencing to char array
  • what is the correct behavior of pthread_mutex_destroy when destroying a locked mutex
  • Recursion Control flow
  • Lua: Line breaks in strings
  • Where these are stored?
  • Error in installing package: fatal error: stdlib.h: no such file or directory
  • In-place sed command not working
  • How to make R's read_csv2() recognise the text characters properly
  • Is there a way to save the selected text and highlight it again once the page is refreshed?
  • Spark job failing in YARN mode
  • Zoom in and out of jPanel
  • Firefox Extension - Monitor refresh and change of tab
  • Appending Character to Character Array In C
  • Saving Changes After In-App Purchase Has Been Purchased
  • D3 nodes and links from JSON with nested arrays of children
  • Deselecting radio buttons while keeping the View Model in synch
  • Nant, Vault & Windows Integrated Authentication
  • DotNetZip - Calculate final zip size before calling Save(stream)
  • output of program is not same as passed argument
  • To display the title for the current loaction in map in iphone
  • align graphs with different xlab
  • Akka Routing: Reply's send to router ends up as dead letters
  • Return words with double consecutive letters
  • How to pass list parameters for each object using Spring MVC?
  • How to format a variable of double type
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • Benchmarking RAM performance - UWP and C#
  • python regex in pyparsing
  • How to set the response of a form post action to a iframe source?
  • Change div Background jquery
  • Android Google Maps API OnLocationChanged only called once
  • Qt: Run a script BEFORE make
  • unknown Exception android
  • Checking variable from a different class in C#
  • reshape alternating columns in less time and using less memory
  • Reading document lines to the user (python)
  • Python/Django TangoWithDjango Models and Databases
  • How can I use threading to 'tick' a timer to be accessed by other threads?