Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to access info about a thread throughout a program
    primarykey
    data
    text
    <p>Question 1: I am trying to name each thread so it can be used throughout the program but am receiving errors like "request for member 'fid' in 'planes[t]' which is of non-class type 'pthread_t'. It is referring to use of planes[t].tid or use of planes[t].startState. I'm not sure how else to get/save these values for each individual thread.</p> <p>Question 2: once I have a thread's startState, how can I send the thread to a takeoff() function (series of switch states and printfs).</p> <p>Question 3: What should be passed into the StartState function? I'm trying to have each thread remember its start state so that later in the code I can have a flight be "landing" or "taking off." </p> <p>Here is the relevant code: <strong>Updated 10/06 at 22:37</strong></p> <pre><code>#include &lt;pthread.h&gt; #include &lt;stdio.h&gt; #include &lt;cstdlib&gt; #include &lt;iostream&gt; #include &lt;queue&gt; #include &lt;unistd.h&gt; #include &lt;algorithm&gt; #include &lt;time.h&gt; #include &lt;ctime&gt; #define NUM_THREADS 3 //3 flights using std::queue; using std::vector; pthread_mutex_t runway1lock; pthread_mutex_t runway2lock; bool run1occupied; bool run2occupied; struct flight_data{ //each plane has these characteristics //void *FlightID; long fid; int startState; // if start=1 ==&gt; taking off ::: if start=2 ==&gt; landing int flyTime; //fly == randomly generated time (order) of takeoff/land }; struct flight_data flightinfo[NUM_THREADS]; void *FlightID(void *flightid){ struct flight_data *my_flights; my_flights = (struct flight_data*)flightid; int taskid; taskid = my_flights-&gt;fid; // long fid; // fid = (long)flightid; // printf("Flight #%1d\n", tid); pthread_exit(NULL); } void Start(struct flight_data my_flights){ my_flights = (struct flight_data)my_flights; int startState; srand(time(0)); my_flights.startState = rand() % 2+1; std::string startstring; if(my_flights.startState == 1){ startstring = "Taking off"; } if(my_flights.startState == 2){ startstring = "Landing"; } for(int i = 1; i&lt;NUM_THREADS+1; i++){ std::cout &lt;&lt; "Start state for Flight # " &lt;&lt; i &lt;&lt; " is " &lt;&lt; startstring &lt;&lt; std::endl; } } void takeoff(struct flight_data my_flights){ my_flights = (struct flight_data)my_flights; for(int i = 1; i&lt;NUM_THREADS+1; i++){ int state = my_flights.startState; switch(state){ case 1:{ //G (GATE) std::cout &lt;&lt; "Flight # " &lt;&lt; flightinfo[i].fid &lt;&lt; " is listed as waiting at the gate." &lt;&lt; std::endl; std::cout &lt;&lt; "Flight # " &lt;&lt; flightinfo[i].fid &lt;&lt; "'s position in queue for runway: " &lt;&lt; flightinfo[i].flyTime &lt;&lt; std::endl; sleep(3); state = 2; break; } case 2: { //Q (queue) -- sets order for use of runway queue&lt;pthread_t&gt; queue; vector&lt;int&gt; flightTimes; int soonestFlightTime = 10; flightTimes.push_back(flightinfo[i].flyTime); //put all flight times into a vector called flightTimes std::sort(flightTimes.begin(), flightTimes.end()); //sort the vector of flightTimes low to high std::reverse(flightTimes.begin(), flightTimes.end()); //flips vector -- high(front) to low(back) while(!flightTimes.empty()){ if (flightinfo[i].flyTime == flightTimes.back()){ //if a thread has the soonest flight time queue.push(i); //then put the flight in the runway queue flightTimes.pop_back(); //pop off the soonest flight time } } while(!queue.empty()){ if(flightinfo[i].fid == queue.front()){ state = 3; queue.pop(); sleep(3); } } break; } case 3: { //CLR (clearance for runway) std::cout &lt;&lt; "Flight # " &lt;&lt; flightinfo[i].fid &lt;&lt; " has clearance to move to the runway." &lt;&lt; std::endl; //will go in order of queue if(run1occupied){ sleep(3); } // else if(collide){ // state = 7; // } else{ state = 4; } break; } case 4: { //RTO (runway takeoff) pthread_mutex_lock(&amp;runway1lock); run1occupied = true; std::cout &lt;&lt; "Flight # " &lt;&lt; flightinfo[i].fid &lt;&lt; " is taking off. Runway occupied. Stand by." &lt;&lt; std::endl; sleep(3); pthread_mutex_unlock(&amp;runway1lock); run1occupied = false; state = 5; break; } case 5: { //CZ (cruise) std::cout &lt;&lt; "Flight # " &lt;&lt; flightinfo[i].fid &lt;&lt; " is reaching proper altitude and cruising toward destination." &lt;&lt; std::endl; sleep(3); // if(!collide){ state = 6; // } // else{ // state = 7; //collision!!! // } break; } case 6: { //RMV (remove from monitoring list) std::cout &lt;&lt; "Flight # " &lt;&lt; flightinfo[i].fid &lt;&lt; " has been removed from the monitoring list." &lt;&lt; std::endl; break; } case 7:{ //COLL (collision) std::cout &lt;&lt; "Collision in the air. There were many casualties." &lt;&lt; std::endl; break; } } } } void landing(struct flight_data my_flights){ my_flights = (struct flight_data)my_flights; for (int i = 0; i&lt;NUM_THREADS; i++){ int state = my_flights.startState; switch(state){ case 1:{ //ENTR (enter monitoring list) state = 2; break; } case 2:{ //Q (queue) //if not the first thing in the queue then state = 4; //otherwise state = 3; } case 3:{ //RWL (runway land) state = 5; break; } case 4:{ //HVR (hover) //if first in queue then state = 3; //otherwise stay here //if collision state = 7; } case 5:{ //CLR (clearance to move to gate) //if collision state = 7 //otherwise state = 6; } case 6:{ //G (gate) } case 7:{ //COLL (collision) } } } } /* bool collision(){ bool collide; //random if(){ collide = true; } else{ collide = false; } return collide; }*/ int main(int argc, char *argv[]){ pthread_t flights[NUM_THREADS]; //pthread_t keeps a thread ID after the thread is created with pthread_create() //it's like an index on a vector of threads int *taskids[NUM_THREADS]; int rc; long t; for (t=1; t&lt;=NUM_THREADS; t++){ //loop creates threads(flights) printf("In main: Creating flight %1d\n", t); flightinfo[t].fid= t; rc = pthread_create(&amp;flights[t], NULL, FlightID, (void *)&amp;flights[t]); if (rc){ printf("ERROR: return code from pthread_create() is %d\n", rc); return (-1); } printf("Created flight %1d\n", t); // Start(flightinfo[t]); flightinfo[t].startState = rand() % 2+1; std::cout &lt;&lt; flightinfo[t].startState &lt;&lt; std::endl; if((flightinfo[t].startState)==1){ std::cout &lt;&lt; "Flight # " &lt;&lt; flightinfo[t].fid &lt;&lt; " is listed with start state as " &lt;&lt; flightinfo[t].startState &lt;&lt; std::endl; takeoff(flightinfo[t]); //go to takeoff function and go through switch case } if((flightinfo[t].startState)==2){ std::cout &lt;&lt; "Flight # " &lt;&lt; flightinfo[t].fid &lt;&lt; " is listed with start state as " &lt;&lt; flightinfo[t].startState &lt;&lt; std::endl; landing(flightinfo[t]); //go to landing function and go through switch case } } pthread_exit(NULL); } </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.
 

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