Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to print using g++ with printf correctly?
    primarykey
    data
    text
    <p>I am compiling this code with g++:</p> <pre><code>#include &lt;pthread.h&gt; #include &lt;iostream&gt; #include &lt;cstdlib&gt; #include &lt;string&gt; #include &lt;stdio.h&gt; using namespace std; #define num_threads 3 #define car_limit 4 pthread_mutex_t mutex; // mutex lock pthread_t cid; // thread id pthread_attr_t attr; // thread attrubutes void *OneCar(void *dir); void ArriveBridge(int *direction); void CrossBridge(); void ExitBridge(int *direction); int main() { int dir[3] = {0,1,1}; pthread_mutex_init(&amp;mutex, NULL); pthread_attr_init(&amp;attr); //cout&lt;&lt; "Pthread Create" &lt;&lt; endl; printf("Pthread Create\n"); for(int i = 0; i &lt; num_threads; i++) { pthread_create(&amp;cid, &amp;attr, OneCar, (void *)&amp;dir[i]); } return 0; } void ArriveBridge(int *direction) { //cout&lt;&lt;"Arrive"&lt;&lt;*direction &lt;&lt; endl; int dr; if(*direction == 0) dr=0; else dr=1; printf("Arrive%d", dr); } void CrossBridge(int *dir) { char d; if(*dir == 0) d = 'N'; else d = 'S'; //cout&lt;&lt;"Crossing Bridge going:"&lt;&lt;d&lt;&lt;endl; printf("Crossing Bridge going %c", d); } void ExitBridge(int *direction) { //cout&lt;&lt;"Exit" &lt;&lt;*direction&lt;&lt;endl; int dr; if(*direction == 0) dr=0; else dr=1; printf("Exit%d\n", dr); } void *OneCar(void *dir) { int *cardir; cardir = (int *) dir; //cout&lt;&lt;*cardir; ArriveBridge(cardir); CrossBridge(cardir); ExitBridge(cardir); return 0; } </code></pre> <p>and I am expecting this result printed to the screen:</p> <pre><code>&gt; Pthread Create &gt; Arrive0Crossing Bridge going NExit0 &gt; Arrive1Crossing Bridge going SExit1 &gt; Arrive1Crossing Bridge going NExit1 </code></pre> <p>But i get this instead:</p> <pre><code>Pthread Create Arrive0Crossing Bridge going NExit0 </code></pre> <p>Why doesnt it print the rest out?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload