使用typedef / struct命名线程

时间:2013-10-05 19:13:34

标签: c++ multithreading

我正在创建一个练习使用线程的程序。我试图命名它们,以便在程序运行时,你可以清楚地看到“飞行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);
}

1 个答案:

答案 0 :(得分:0)

下面有一段代码片段,代表我将如何实现它。

您还应该查看 pthread_key_create pthread_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);
    }
}

此外,我不知道我是否正确理解您的代码,或者您是否只有一些编码错误,所以我给您留下一些可能是错误/错误的问题或评论:< / p>

1)在start callback函数中调用pthread_exit:

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

2)您将一个没有返回值的函数传递给&lt;&lt; 运算符:

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

3)您只需调用相同的函数3次即可获得返回值。难道不是 int state = StartState(flights [i])然后测试 state 变量值吗? 将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]){