0

スレッドを使って練習するプログラムを作成しています。プログラムが実行されたときに「フライト 1 が離陸中...」または「フライト 6 が着陸中...」などと明確にわかるように名前を付けようとしています。すべてのスレッドに、ランダムに生成される flyTime が必要です (したがって、滑走路を使用する順序がわかります)。私は struct/typedef を使用して各 pthread にこれらの特性を与えることを試みましたが、苦労しています。たとえば、flight.flyTime と言ってプログラム全体で使用できます。着陸/離陸機能のないコードの関連部分は次のとおりです。

#include <pthread.h>
#include <stdio.h>
#include <cstdlib>
#include <iostream>
#include <queue>

#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 ==> taking off:::if start=2 ==> 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<=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<NUM_THREADS; t++){
            std::cout << "Start State for Flight# " << FlightID << " is " << start << 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<=NUM_THREADS; t++){          //loop creates threads(flights)
            printf("In main: Creating flight %1d\n", t);
            rc = pthread_create(&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 << "Flight # " << &flights[t] << " is listed as waiting at the gate." << std::endl;
                    //go to takeoff function and go through switch case     
            }
            if(StartState(flights[t])==2){`enter code here`
                    std::cout << "Flight # " << &flights[t] << " is listed as waiting to land." << std::endl;
                    //go to landing function and go through switch case     
            }
    }
    pthread_exit(NULL);
}
4

1 に答える 1

0

以下に、私がそれを実装する方法を表すコード スニペットがあります。

pthread_key_createpthread_getspecific、およびpthread_setspecificも参照してください。これは、各スレッドに固有のデータをスレッドのメモリ コンテキストに格納できるようにする一連の関数です。後でコード内で役立つ場合があります。

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->fid = fid;
    flights[fid] = flight;
    pthread_setspecific(pkey, flight);

    int startState;
    for (int i=0; i<=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<NUM_THREADS; t++){
            std::cout << "Start State for Flight# " << fid << " is " << start << std::endl;
    }

    flight->StartState = startState;
}

int main(int argc, char* argv[]) {
    pthread_key_create(&pkey, NULL);
    for (t=1; t<=NUM_THREADS; t++){
        rc = pthread_create(&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);
    }
}

また、あなたのコードを正しく理解しているのか、それともコーディング エラーがあるだけなのかわかりません。そのため、間違いやバグである可能性があるいくつかの質問やコメントを残します。

1) start コールバック関数で pthread_exit を呼び出します。

void *FlightID(void *flightid){
    long fid;
    fid = (long)flightid;
    pthread_exit(NULL);
}

2) <<演算子に戻り値のない関数を渡します。

    std::cout << "Start State for Flight# " << FlightID << " is " << start << std::endl;

3) 戻り値を取得するためだけに、同じ関数を 3 回呼び出します。int state = StartState(flights[i])にして、状態変数の値をテストするべきではありませんか? StartState(フライト[t]); //すべてのフライトに開始状態を与える

    if(StartState(flights[t])==1){
            std::cout << "Flight # " << &flights[t] << " is listed as waiting at the gate." << std::endl;
            //go to takeoff function and go through switch case     
    }
    if(StartState(flights[t])==2){`enter code here`
            std::cout << "Flight # " << &flights[t] << " is listed as waiting to land." << std::endl;
            //go to landing function and go through switch case     
    }

4) 次のような関数を定義することはできません。

int StartState(flights[NUM_THREADS]){
于 2013-10-07T09:43:41.960 に答える