<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[POSIX Thread aufrufe werden nicht richtig synchronisiert]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich habe im Internet ein schönes Beispiel gesehen mit dem sich Threadprogrammierung lernen lässt.<br />
Also wir habe einen Park(oder Museum; ähnlich wie im Jurassic Park). Sechs Besucher. Diese betreten den Park und steigen dann in zwei PKW, werden herum gefahren und betreten den Park erneut. Pro Person dreimal.<br />
Ich möchte entscheiden, ob die Besucher alleine fahren oder zu zweit ode zu dritt.<br />
Leider gelingt mir nur alleine.</p>
<p>Ok genug der Prosa jetzt kommt ein Code...</p>
<pre><code class="language-cpp">//globale Deklarationen.....

#define VISITORS 6 
#define CARS   2 
#define CYCLES   3

const char* VisitorNames[VISITORS+1] = {&quot;Mike&quot;, &quot;Jeff&quot;, &quot;John&quot;, &quot;Sandra&quot;, &quot;Michelle&quot;, &quot;Jack&quot;, &quot;Amy&quot;};
const char* CarNames[CARS+1] = {&quot;Explorer1&quot;, &quot;Explorer2&quot;};
char* VisitorName = NULL;
char* CarName = NULL;

sem_t sem_car, sem_visitor; 
pthread_t thread_id_visitor[VISITORS],  thread_id_car[CARS]; 

void *Car (void *p) 
{ 
  int *index /*, paseos = 0*/; 
  extern sem_t sem_car, sem_visitor; 
  index = (int *) p; 

  /*The cars are always waiting for passengers*/ 
  while(1) 
    { 
	  CarName = const_cast&lt;char*&gt;(CarNames[*index]);	  

	  sem_wait (&amp;sem_car); // car -1
	  sem_wait (&amp;sem_car); // car -1

	  sleep (rand()%3);  

	  sem_post (&amp;sem_visitor);  //visitor +1
	  sem_post (&amp;sem_visitor);  //visitor +1

	  fprintf (stdout, &quot;Two of them join a ride!!.\n&quot;); 	

    }  
}

void *Visitor (void *p) 
{ 
  extern sem_t sem_car, sem_visitor;
  int i, *index; 
  index = (int *)p;

  for (i = 0; i &lt; CYCLES; i++) 
    { 
	  //VisitorName = const_cast&lt;char*&gt;(VisitorNames[*index+1]);
      /* Visitor watching / waiting at the museum. */      
      fprintf (stdout, &quot;Visitor %s enter the museum\n&quot;, /*VisitorName*/VisitorNames[*index+1]);            
      sleep (rand()%4); 
      /* Waiting for a ride by car. */        
      sem_post (&amp;sem_car); //car+1
      sem_post (&amp;sem_car); //car+1   
      sem_wait (&amp;sem_visitor);//visitor -1
      sem_wait (&amp;sem_visitor);//visitor -1
      fprintf (stdout, &quot;Visitor %s climbs in the car.\n&quot;, /*VisitorName*/VisitorNames[*index+1]); 
      sleep (rand()%3);	 
    } 
    //VisitorName = const_cast&lt;char*&gt;(VisitorNames[*index+1]);
    fprintf (stdout, &quot;Visitor %s has terminated his %d cycles.\n&quot;, /*VisitorName*/VisitorNames[*index+1], CYCLES); 

    return 0;
} 

void main() {
  void *Car (void *), *Visitor (void *); 
				  int i, v[VISITORS], c[CARS]; 
				  extern sem_t sem_car, sem_visitor; 

				  sem_init (&amp;sem_car,  //semaphore 
						  		   0,  //no interprocess sharing
						  		   0); //Intialer Wert

				  sem_init (&amp;sem_visitor,  //semaphore 
						  			   0,  //no interprocess sharing
						  			   0); //Intialer Wert

				  srand (time (NULL));    

				  for (i=0; i&lt;VISITORS; i++) 
				  { 
				    v[i] = i; 
				    if (pthread_create (&amp;thread_id_visitor[i], NULL, Visitor, &amp;v[i])) 
				      exit (1); 
				  } 				  				  	

				  for (i = 0; i &lt; CARS; i++) 
				  { 
				      c[i] = i; 
				      if (pthread_create (&amp;thread_id_car[i], NULL, Car, &amp;c[i])) 
				   exit (1); 
				  } 

				  //wait for thread termination 
				  for (i=0; i&lt;VISITORS; i++)
				    pthread_join (thread_id_visitor[i], NULL); 

				  printf(&quot;\n\t**- All visitors have toured the safari -**\n&quot;); 
}
</code></pre>
<p>1)Was muß ich tun damit drei Passagiere zusammen fahren können.<br />
2)Visitorname liefert Mist wenn er gobal deklariert wird. Muß ich das mit Mutex arbeiten?</p>
<p>Gruß<br />
Frank</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/263143/posix-thread-aufrufe-werden-nicht-richtig-synchronisiert</link><generator>RSS for Node</generator><lastBuildDate>Sun, 06 Sep 2026 01:27:53 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/263143.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 16 Mar 2010 15:02:31 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to POSIX Thread aufrufe werden nicht richtig synchronisiert on Tue, 16 Mar 2010 15:02:31 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich habe im Internet ein schönes Beispiel gesehen mit dem sich Threadprogrammierung lernen lässt.<br />
Also wir habe einen Park(oder Museum; ähnlich wie im Jurassic Park). Sechs Besucher. Diese betreten den Park und steigen dann in zwei PKW, werden herum gefahren und betreten den Park erneut. Pro Person dreimal.<br />
Ich möchte entscheiden, ob die Besucher alleine fahren oder zu zweit ode zu dritt.<br />
Leider gelingt mir nur alleine.</p>
<p>Ok genug der Prosa jetzt kommt ein Code...</p>
<pre><code class="language-cpp">//globale Deklarationen.....

#define VISITORS 6 
#define CARS   2 
#define CYCLES   3

const char* VisitorNames[VISITORS+1] = {&quot;Mike&quot;, &quot;Jeff&quot;, &quot;John&quot;, &quot;Sandra&quot;, &quot;Michelle&quot;, &quot;Jack&quot;, &quot;Amy&quot;};
const char* CarNames[CARS+1] = {&quot;Explorer1&quot;, &quot;Explorer2&quot;};
char* VisitorName = NULL;
char* CarName = NULL;

sem_t sem_car, sem_visitor; 
pthread_t thread_id_visitor[VISITORS],  thread_id_car[CARS]; 

void *Car (void *p) 
{ 
  int *index /*, paseos = 0*/; 
  extern sem_t sem_car, sem_visitor; 
  index = (int *) p; 

  /*The cars are always waiting for passengers*/ 
  while(1) 
    { 
	  CarName = const_cast&lt;char*&gt;(CarNames[*index]);	  

	  sem_wait (&amp;sem_car); // car -1
	  sem_wait (&amp;sem_car); // car -1

	  sleep (rand()%3);  

	  sem_post (&amp;sem_visitor);  //visitor +1
	  sem_post (&amp;sem_visitor);  //visitor +1

	  fprintf (stdout, &quot;Two of them join a ride!!.\n&quot;); 	

    }  
}

void *Visitor (void *p) 
{ 
  extern sem_t sem_car, sem_visitor;
  int i, *index; 
  index = (int *)p;

  for (i = 0; i &lt; CYCLES; i++) 
    { 
	  //VisitorName = const_cast&lt;char*&gt;(VisitorNames[*index+1]);
      /* Visitor watching / waiting at the museum. */      
      fprintf (stdout, &quot;Visitor %s enter the museum\n&quot;, /*VisitorName*/VisitorNames[*index+1]);            
      sleep (rand()%4); 
      /* Waiting for a ride by car. */        
      sem_post (&amp;sem_car); //car+1
      sem_post (&amp;sem_car); //car+1   
      sem_wait (&amp;sem_visitor);//visitor -1
      sem_wait (&amp;sem_visitor);//visitor -1
      fprintf (stdout, &quot;Visitor %s climbs in the car.\n&quot;, /*VisitorName*/VisitorNames[*index+1]); 
      sleep (rand()%3);	 
    } 
    //VisitorName = const_cast&lt;char*&gt;(VisitorNames[*index+1]);
    fprintf (stdout, &quot;Visitor %s has terminated his %d cycles.\n&quot;, /*VisitorName*/VisitorNames[*index+1], CYCLES); 

    return 0;
} 

void main() {
  void *Car (void *), *Visitor (void *); 
				  int i, v[VISITORS], c[CARS]; 
				  extern sem_t sem_car, sem_visitor; 

				  sem_init (&amp;sem_car,  //semaphore 
						  		   0,  //no interprocess sharing
						  		   0); //Intialer Wert

				  sem_init (&amp;sem_visitor,  //semaphore 
						  			   0,  //no interprocess sharing
						  			   0); //Intialer Wert

				  srand (time (NULL));    

				  for (i=0; i&lt;VISITORS; i++) 
				  { 
				    v[i] = i; 
				    if (pthread_create (&amp;thread_id_visitor[i], NULL, Visitor, &amp;v[i])) 
				      exit (1); 
				  } 				  				  	

				  for (i = 0; i &lt; CARS; i++) 
				  { 
				      c[i] = i; 
				      if (pthread_create (&amp;thread_id_car[i], NULL, Car, &amp;c[i])) 
				   exit (1); 
				  } 

				  //wait for thread termination 
				  for (i=0; i&lt;VISITORS; i++)
				    pthread_join (thread_id_visitor[i], NULL); 

				  printf(&quot;\n\t**- All visitors have toured the safari -**\n&quot;); 
}
</code></pre>
<p>1)Was muß ich tun damit drei Passagiere zusammen fahren können.<br />
2)Visitorname liefert Mist wenn er gobal deklariert wird. Muß ich das mit Mutex arbeiten?</p>
<p>Gruß<br />
Frank</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1869893</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1869893</guid><dc:creator><![CDATA[FrankTheFox]]></dc:creator><pubDate>Tue, 16 Mar 2010 15:02:31 GMT</pubDate></item></channel></rss>