Lamport的Bakery算法的实现具有多于1个线程的seg错误

时间:2013-01-21 05:20:28

标签: c++ multithreading segmentation-fault

我正在使用pthreads和Processor类来实现Lamport的Bakery算法,以充当共享内存。使用单个线程它工作正常,有2个线程我在线程2通过所有30次尝试访问'面包店'后得到了seg错误:

  

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

有3个或更多个线程,我在从BakeryAlgo函数输出“here”两次后得到了seg错误:

  Processor.cpp中处理器:: getNumber(this = 0x5b18c483)中的

0x0804ae52:33

bakery.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,选择和数字的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;
}

有谁知道为什么会发生这些seg故障? gdb说他们正在发生的地方看起来像无辜的代码行。

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分: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));
    }

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