-1

我正在使用 pthreads 和处理器类来实现 Lamport 的烘焙算法,以充当共享内存。使用单个线程它工作正常,使用 2 个线程在线程 2 运行所有 30 次访问“面包店”的尝试后,我得到了 seg 错误:

dl-tls.c:没有这样的文件或目录。

使用 3 个或更多线程时,我从 bakeryAlgo 函数输出“here”两次后出现段错误:

0x0804ae52 in Processor::getNumber (this=0x5b18c483) at Processor.cpp:33

面包店.cpp

struct argStruct {
    vector<Processor>* processors;
    Processor* processor;
};

int findMax(vector<Processor>* processors) {
    int max = -99;
    for (int i = 0; i < processors->size(); i++) {
        if (processors->at(i).getNumber() > max) {
            max = processors->at(i).getNumber();
        }
    }
    return max;
}

void* bakeryAlgo(void* arg) {
    struct argStruct* args = static_cast<struct argStruct *>(arg);
    cout << "here" << endl;
    for (int i = 0; i < 30; i++) {
        args->processor->setChoosing(1);
        args->processor->setNumber(findMax(args->processors));
        args->processor->setChoosing(0);
        for (int j = 0; j < args->processors->size(); j++) {
            int jChoosing = args->processors->at(j).getChoosing();
            int jNumber = args->processors->at(j).getNumber();
            int jId = args->processors->at(j).getId();
            int pNumber = args->processor->getNumber();
            int pId = args->processor->getId();
            if (jId != pId) {
                while (jChoosing != 0) {}
                while (jNumber != 0 && ((jNumber < pNumber) || ((jNumber == pNumber) && (jId < pId)))) { }
            }
        }
        cout << "Processor: " << args->processor->getId() << " executing critical section!" << endl;
        args->processor->setNumber(0);
    }
}

int main(int argc, char *argv[]) {

    // Check that a command line argument was provided
    if (2 == argc) {
        int numProcessors = atoi(argv[1]);
        vector<Processor> processors;
        vector<argStruct> argVect;
        vector < pthread_t > threads;
        for (int i = 0; i < numProcessors; i++) {
            Processor p = Processor(i);
            processors.push_back(p);
        }
        for (int i = 0; i < numProcessors; i++) {
            pthread_t processorThread;
            struct argStruct args;
            args.processors = &processors;
            args.processor = &processors.at(i);
            argVect.push_back(args);
            threads.push_back(processorThread);
            pthread_create(&threads.at(i), NULL, &bakeryAlgo, &argVect.at(i));
        }
        for (int i = 0; i < numProcessors; i++) {
            pthread_join(threads.at(i), NULL);
        }
    } 
    else {
        cout << "Usage: bakery num, num is number of threads." << endl;
    }
    return 0;
}

Processor.cpp / Processor.h 中的代码很简单,它只是值 id、choice 和 number 的几个 getter 和 setter,带有一个默认构造函数和一个采用 int id 的构造函数。

Processor::Processor() {

}

Processor::Processor(int idval) {
    id = idval;
    choosing = 0;
    number = 0;
}

Processor::~Processor() {

}

int Processor::getChoosing() {
    return choosing;
}

int Processor::getNumber() {
    return number;
}

int Processor::getId() {
    return id;
}

void Processor::setChoosing(int c) {
    choosing = c;
}

void Processor::setNumber(int n) {
    number = n;
}

有谁知道为什么会发生这些段错误?gdb 说它们发生的地方对我来说就像是无辜的代码行。

4

2 回答 2

2

您是否使用指向vector定义的指针main作为您的数据?堆栈不在线程之间共享,因此访问此内存的其他线程充其量是未定义的行为。我想这是你麻烦的根源。

于 2013-01-21T06:15:58.220 回答
0

您正在获取正在更改的向量中元素的地址:

    for (int i = 0; i < numProcessors; i++) {
        pthread_t processorThread;
        struct argStruct args;
        args.processors = &processors;
        args.processor = &processors.at(i);
        argVect.push_back(args);
        threads.push_back(processorThread);

        // danger! 
        pthread_create(&threads.at(i), NULL, &bakeryAlgo, &argVect.at(i));
    }

每次将新线程推入 时threads vector,向量可能会在内存中重新定位,因此当添加下一个线程时,传递给 pthread_create 的指针可能指向垃圾。

你想这样做:

    for (int i = 0; i < numProcessors; i++) {
        pthread_t processorThread;
        struct argStruct args;
        args.processors = &processors;
        args.processor = &processors.at(i);
        argVect.push_back(args);
        threads.push_back(processorThread);
    }
    for (int i = 0; i < numProcessors; i++) {
        pthread_create(&threads.at(i), NULL, &bakeryAlgo, &argVect.at(i));
    }

通过在创建线程之前等到所有元素都添加到向量中,您现在传递的指针在线程运行时保持良好。

于 2013-01-21T06:22:16.203 回答