Note that there are some explanatory texts on larger screens.

plurals
  1. POnaming threads, using typedef/struct
    primarykey
    data
    text
    <p>I am creating a program to practice using threads. I am trying to name them so that when the program is run, you can clearly see "Flight 1 is taking off..." or "Flight 6 is landing..." and so on. I would like every thread to have a flyTime (so I know what order they will use the runway in) which will be randomly generated. I have tried and am having difficulty using struct/typedef to give each pthread these characteristics so i can say for example flight.flyTime and use it throughout the program. Here is the relevant part of my code without my landing/takeoff functions:</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; #define NUM_THREADS 8 //8 flights pthread_mutex_t runway1lock; void *FlightID(void *flightid){ long fid; fid = (long)flightid; pthread_exit(NULL); } typedef struct{ //each plane has these characteristics long fid; int StartState; // if start=1 ==&gt; taking off:::if start=2 ==&gt; landing int flyTime; //fly == randomly generated time (order) }FLIGHTS; FLIGHTS flights[NUM_THREADS]; int StartState(flights[NUM_THREADS]){ 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; FlightID &lt;&lt; " is " &lt;&lt; start &lt;&lt; std::endl; } return startState; } 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 rc; long t; for (t=1; t&lt;=NUM_THREADS; t++){ //loop creates threads(flights) printf("In main: Creating flight %1d\n", 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); StartState(flights[t]); //gives every flight a start state 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 } } pthread_exit(NULL); } </code></pre>
    singulars
    1. This table or related slice is empty.
    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