0

我正在创建一个程序来练习使用线程。我试图给它们命名,以便在程序运行时,您可以清楚地看到“Flight 1 正在起飞……”或“Flight 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_getspecificpthread_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)您在启动回调函数中调用 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])然后测试状态变量值吗?开始状态(航班[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 回答