Wednesday, 15 January 2014

pthreads - Can't pass in struct into function for pthread_create correctly -


I am trying to pass in Professor Strait in my professor function, but I did not get the information stored in it properly To pass in the function, I suspect that there is something to do with how I used to drink maulcad but I thought that after the completion of this, that problem would be solved. When I try to print * Professor-> ID, I get a sygft, because apparently it decides to read the PD as the memory location 0x0, even if it is not the main

  typedef struct {int * id; Int * assigning; Int * min_wait; Int * max_wait; Int * min_assignments; Int * max_assignments; Int * min_hrs; Int * max_hrs; } Professor; Professor * McFafosser () {Professor * Professor = Molok (Psychophos * Professor); Return professor; } Zero * Professorfunk (Zero * P) {Professor * Professor = (Professor *) P; Fprintf (stdout, "initial professor% d \ n", * professor-> ID); Pthread_exit (0); } Int main (int argc, char ** argv) {// formulating pthread_t professor [num_professors]; Professor * P; Int i; Int id; For (i = 0; i  ID = & amp; Id; If (pthread_create (& professor [i], NULL, professorfunk, (zero *) p)! = 0) {false ("pthread_create"); Exit (1); } Free (P); } For (i = 0; i    

You are allocating an array of Professor Stracts, Before your thread gets the chance to work on them. A better way to apply this, to allocate the whole array, to execute them, and to free up the memory, once you know that the threads are out (examples below).

  pthread_t professor [num_professors]; Professor * P; Int i; Int id; P = Mallok (size (* p) * num_professors); For (i = 0; i  ID = & amp; Id; If (pthread_create (& professor [i], NULL, professorfunk, (zero *) p)! = 0) {false ("pthread_create"); Exit (1); }} For (i = 0; i & lt; num_professors; ++ i) {if (pthread_join (professor [i], null)! = 0) {perror ("pthread_join"); Exit (1); }} Free (P);  

No comments:

Post a Comment