Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is a code snippet below that represents how I would implement it.</p> <p>You should also take a look at <strong>pthread_key_create</strong>, <strong>pthread_getspecific</strong> and <strong>pthread_setspecific</strong>. This is a set of functions that allow you to have data, specific to each thread, stored in the thread's memory context. It may come in handy for in your code, later on.</p> <pre><code>typedef struct{ long fid; int StartState; int flyTime; } FLIGHTS; FLIGHTS** flights = new FLIGHTS*[NUM_THREADS]; pthread_key_t pkey: void *FlightID(void *flightid){ long fid; fid = (long)flightid; FLIGHTS* flight = new FLIGHTS(); flight-&gt;fid = fid; flights[fid] = flight; pthread_setspecific(pkey, flight); int startState; for (int i=0; i&lt;=NUM_THREADS; i++){ startState = rand() % 1+2; } std::string start; if(startState == 1){ start = "Taking off"; } if(startState == 2){ start = "Landing"; } for (int t=0; t&lt;NUM_THREADS; t++){ std::cout &lt;&lt; "Start State for Flight# " &lt;&lt; fid &lt;&lt; " is " &lt;&lt; start &lt;&lt; std::endl; } flight-&gt;StartState = startState; } int main(int argc, char* argv[]) { pthread_key_create(&amp;pkey, NULL); for (t=1; t&lt;=NUM_THREADS; t++){ rc = pthread_create(&amp;flights[t], NULL, FlightID, (void *)t); if (rc){ printf("ERROR: return code from pthread_create() is %d\n", rc); return (-1); } printf("Created flight %1d\n", t); } } </code></pre> <p>Also, I don't know if I'm understanding your code correctly or if you just have some coding errors on it, so I leave you with some questions or remarks that could be mistakes/bugs:</p> <p>1) You invoke pthread_exit in the start callback function:</p> <pre><code>void *FlightID(void *flightid){ long fid; fid = (long)flightid; pthread_exit(NULL); } </code></pre> <p>2) You pass to the <strong>&lt;&lt;</strong> operator a function with no return value:</p> <pre><code> std::cout &lt;&lt; "Start State for Flight# " &lt;&lt; FlightID &lt;&lt; " is " &lt;&lt; start &lt;&lt; std::endl; </code></pre> <p>3) You invoke the same function 3 times just to get the return value. Shouldn't it be <em>int state = StartState(flights[i])</em> and then test the <em>state</em> variable value? StartState(flights[t]); //gives every flight a start state</p> <pre><code> if(StartState(flights[t])==1){ std::cout &lt;&lt; "Flight # " &lt;&lt; &amp;flights[t] &lt;&lt; " is listed as waiting at the gate." &lt;&lt; std::endl; //go to takeoff function and go through switch case } if(StartState(flights[t])==2){`enter code here` std::cout &lt;&lt; "Flight # " &lt;&lt; &amp;flights[t] &lt;&lt; " is listed as waiting to land." &lt;&lt; std::endl; //go to landing function and go through switch case } </code></pre> <p>4) You can't define a function like this:</p> <pre><code>int StartState(flights[NUM_THREADS]){ </code></pre>
    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.
    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