我是 pthreads 的新手并将它们与互斥锁一起使用。谁能指出我如何使用它们的正确方向?

问题描述 投票:0回答:0

我有一个使用 pthreads 和 mutex 的编程作业。该程序的重点是接受一个输入(一个 .txt 文件),输入看起来像这样:

Bridge digit1 digit2
CarOrBoat CoB_name dig1 dig2
CarOrBoat CoB_name dig1 dig2
...

基本上,列表的第一行(总是)是“桥”这个词,后面跟着两个数字,第一个是桥上去所需的时间,第二个是下桥所需的时间。对于任何其他行,其中显示“CarOrBoat”的地方,将是“汽车”或“船”(声明正在过桥的是什么),然后是所述 CoB 的名称(汽车或船),最后是接下来的两个数字分别是自上次 CoB 到达以来经过的时间和穿过所述桥梁所需的时间。我已经输入了数据,现在我正在读取每一行并将其放入结构节点,它们的数据是字符串类型(CoB)、字符串名称、int dig1、int dig2。然后我暂时将节点放入列表中以进行测试。我想稍后在读入数据后立即调用一个线程,但我可以稍后再进行。无论如何,我的读取输入被放入一个包含每条指令的向量中(我忽略了第一行,只使用它的 dig1 和 dig2)。

这整个任务的目标是(通过使用 pthreads 和 1 mutex [the bridge])让汽车和船一次只能通过 1 个桥并轮流(例如,如果排队有 5 辆汽车和 5 艘船,我们[取决于桥的状态,但如果我们处于船先通过的状态,我们为汽车降低桥]降低桥[这需要 dig1 时间,以及我们正在等待的任何时间a action to happen, we sleep for that time, we only sleep for bridge up and down and car or ship crossing time], then let the car pass through. 汽车通过后,如果有船在排队,则提高桥让船通过,反之亦然,如果有船通过,如果有汽车在排队。如果有 > 1 辆汽车而没有船,则让汽车通过而不举起桥,反之亦然如果有 > 1 艘船。

我将如何使用 pthreads 和互斥锁(桥)来解决这个问题?如果需要,我可以提供作业的确切细节

我没有尝试过任何东西,因为我不知道如何使用 pthreads 和 mutex,我已经尝试阅读它们并且我理解我正在阅读的示例和文本中发生了什么,但我不能接受并应用它到我的任务。这是我的代码(在 C++ 中,这都是在 C++ 中):

#include <string>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <fstream>
#include <pthread.h>
#include <mutex>
#include <condition_variable>

using namespace std;
/*Functions----------------*/




/*                          */
static pthread_mutex_t theLock;
static pthread_cond_t carCanGo;
static pthread_cond_t
int nCars = 0;
int nShips = 0;
int bridge_up;
int bridge_down;
bool draw_bridge_status = false;
//False means it is up, unavailable to cross the channel as a ship

struct node {
    string carORboat;
    int first_time;
    int second_time;
    string thing_name;


    node(string type, int ft,int st, string name){
        this->carORboat = type;
        this->first_time = ft;
        this->second_time = st;
        this->thing_name = name;
    }
    node(){
        ;
    }
};
//Functions for the car crossing the bridge
void CarPassing(string nameOfCar, int time_to_cross){
    pthread_mutex_lock(&theLock);




    pthread_mutex_unlock(&theLock);
}

void ShipPassing(string nameOfShip, int time_to_cross){
    pthread_mutex_lock;
}

int main(){
    pthread_mutex_init(&theLock, NULL);
    vector<node*> theList;
    string fileName;
    cin >> fileName;
    string firstLine;
    string currLine;
    

    fstream theFile;
    fileName.erase(remove(fileName.begin(), fileName.end(), '\n'), fileName.end());
    fileName.erase(remove(fileName.begin(), fileName.end(), '\0'), fileName.end());
    fileName.erase(remove(fileName.begin(), fileName.end(), ' '), fileName.end());
    theFile.open("input1.txt");
    if(!theFile.is_open()){
        cout << "Error opening file!" << endl;
    }
    getline(theFile, firstLine);
    string garbo = firstLine.substr(0,firstLine.find(' '));
    firstLine = firstLine.substr(firstLine.find(' ') + 1);
    bridge_up = stoi(firstLine.substr(0, firstLine.find(' ')));
    firstLine = firstLine.substr(firstLine.find(' ') + 1);
    string bridge_going_down = firstLine.substr(0);
    bridge_down = stoi(bridge_going_down);
    while(getline(theFile, currLine)){
        currLine.erase(remove(currLine.begin(), currLine.end(), '\n'), currLine.end());
        currLine.erase(remove(currLine.begin(), currLine.end(), '\0'), currLine.end());
        string cob = currLine.substr(0, currLine.find(' '));
        if(cob == "Car"){
            nCars++;
        }
        else{
            nShips++;
        }
        currLine = currLine.substr(currLine.find(' ') + 1);
        string cob_name = currLine.substr(0, currLine.find(' '));
        currLine  = currLine.substr(currLine.find(' ') + 1);
        int ft = stoi(currLine.substr(0, currLine.find(' ')));
        int st = stoi(currLine);
        node* temp_node = new node(cob, ft, st, cob_name);
        theList.push_back(temp_node);
    }

    /*------------EVERYTHING HAS BEEN READ IN FROM THE FILE UP TO THIS POINT*/








    return 0;
}```
c++ multithreading pthreads mutex
© www.soinside.com 2019 - 2024. All rights reserved.