Pthread功能无法打印

时间:2018-04-20 20:37:27

标签: c++ pthreads

我正在尝试创建一个模拟隧道的隧道和汽车线程,这些隧道只能单向运行。让我们说每条路都是W和B.通往W的路是5秒钟,然后隧道关闭另外5路,然后隧道B路开5秒钟然后关闭另外5路,然后重复。

我已经使用tunnelf函数创建了隧道线程,但除了打印第一行之外它没有做任何事情:"隧道现在对Whittier Bound流量开放了#34;。它有时只会这样做。每次编译代码时,它都可以打印任何内容或该行。它没有通过所需的输出。同时,如果我将完全相同的while循环从tunnelf线程转换为main,它可以正常工作

#include <iostream>
#include <cstring>
#include <string>
#include <cctype>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <pthread.h>
#include <conio.h>
#include <windows.h>
#include <ctime>
#include <cerrno>
#include <unistd.h>
using namespace std;
void *car(void *arg);
void *tunnelf();
static pthread_mutex_t traffic_lock = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t car_lock = PTHREAD_MUTEX_INITIALIZER;

static pthread_cond_t bbcan = PTHREAD_COND_INITIALIZER;
static pthread_cond_t wbcan =PTHREAD_COND_INITIALIZER;
static pthread_cond_t not_full = PTHREAD_COND_INITIALIZER;
static bool whittierBound = false;
static bool bbBound = false;

struct car2{
    int arrive;
    int cross;
    string bound;
};

int main(){
    ifstream in;
    in.open("Thrd.txt");
    if (in.fail()){
        cout<< "failed to open file";
        exit(1);
    }
    car2 record[50];
    int max_cars;
    int arrive;
    int cross;
    string bound;
    string data;
    int i = 0;
    in >> max_cars;
    cout<<"Num cars "<<max_cars<<endl;
    while(!in.eof()){
        in >> record[i].arrive >>record[i].bound >> record[i].cross;
        i++;
    }
    int size = i;
    for(int i= 0; i<size; i++){
        cout << record[i].arrive <<record[i].bound <<record[i].cross<<endl;
    }
    pthread_t cartid[max_cars];
    pthread_t tunnel;//just shared variable for the tunnel
    pthread_create(&tunnel, NULL, &tunnelf, NULL);
    in.close();
}

void *tunnelf(){
    static int done;
    while(done==0){
        pthread_mutex_lock(&traffic_lock);
        whittierBound = true;

        cout << "The tunnel is now open to Whiitier-bound traffic"<<endl;
        pthread_cond_broadcast(&wbcan);
        pthread_mutex_unlock(&traffic_lock);
        sleep(5);

        pthread_mutex_lock(&traffic_lock);
        whittierBound = false;
        cout << "The tunnel is now closed to all traffic"<<endl;
        pthread_mutex_unlock(&traffic_lock);
        sleep(5);

        pthread_mutex_lock(&traffic_lock);
        bbBound = true;
        cout << "The tunnel is now open to Bear-Valley-bound traffic"<<endl;
        pthread_cond_broadcast(&bbcan);
        pthread_mutex_unlock(&traffic_lock);
        sleep(5);

        pthread_mutex_lock(&traffic_lock);
        bbBound = false;
        cout << "The tunnel is now closed to all traffic"<<endl;
        pthread_mutex_unlock(&traffic_lock);

    }   
}

1 个答案:

答案 0 :(得分:0)

One thing to mention, you never guard against array overruns. In C++17, you can use std::size. You might consider using std::array for record, which would help in debug builds (but not release)

while(!in.eof() && i<std::size(record)){
    in >> record[i].arrive >>record[i].bound >> record[i].cross;
//      cout <<arrive<<" " << bound<<" " << cross<<endl;
    i++;
}