程序进入main之前C ++中的分段错误

时间:2018-04-02 17:57:32

标签: c++ linux

我正在编写一个程序,需要使用两个互斥锁和30 + 25个线程才能运行。 然而它造成了分段错误。 它成功编译,但未能运行。 分段错误从程序开始(不是主程序)开始 因为我在main中的第一个命令之后添加了一条消息,它不会打印出来。

但在我开始进入主要部分之前,我的程序在哪里导致内存错误?

#include <iostream>
#include <cstdlib>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>

using namespace std;
#define makingMachine   30
#define packingMachine  25

int *hotdog;
int total;
int pool = 0;
int packed = 0;
int **log;
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex2 = PTHREAD_MUTEX_INITIALIZER;

sem_t pack;
void* makeHotDog(void *arg)
{
    int id = *(int *)arg;
    int done = 0;
    long myMake = 0;
    while (!done) {
        pthread_mutex_lock(&mutex1);
        if (hotdog > 0) {
            sleep(rand()%3+4);
            hotdog--;
            myMake++;
            pool++;
            log[packed][0] = id;
            sleep(rand()%1+1);
            sem_post(&pack);
        }
        else {
            done = 1;
        }
        pthread_mutex_unlock(&mutex1);
    }

    pthread_exit((void *) myMake);
}

void* packHotDog(void *argc)
{
    int id = *(int *)argc;
    int done = 0;
    long myPack = 0;
    while (!done) {
        sem_wait(&pack);
        pthread_mutex_lock(&mutex2);
        if (packed <= total && pool > 0) {
            sleep(rand()%1+1);
            pool--;
            sleep(rand()%1+1);
            myPack++;
            log[packed][1] = id;
            sleep(rand()%1+1);
            packed++;
        }
        else {
            done = 1;
        }
        pthread_mutex_unlock(&mutex2);
    }

    pthread_exit((void *) myPack);
}

int main(int argc, char *argv[]) //master thread
{
    cin >> total;
    cout << "Success get number";
    hotdog = &total;
    log = new int*[total];
    for (int x = 0; x < total; x++)
        log[x] = new int[2];
    pthread_t maker[makingMachine], packer[packingMachine];
    int makerid[makingMachine], packerid[packingMachine];
    int i, rc;
    long total_make = 0, total_pack = 0;
    void *retval1, *retval2;
    cout<< "thread create";

    sem_init(&pack, 0, 0);

    cout << " sem ok " ;
    for (i = 0; i < makingMachine; i++) {
        makerid[i] = i;
        rc = pthread_create(&maker[i], NULL, makeHotDog, (void *)&makerid[i]);
        if (rc) {
            cout << "Error when creating threads!" << endl;
            exit(-1);
        }
    }

    for (i = 0; i < packingMachine; i++) {
        packerid[i] = i;
        rc = pthread_create(&packer[i], NULL, packHotDog, (void *)&packerid[i]);
        if (rc) {
            cout << "Error when creating threads!" << endl;
            exit(-1);
        }
    }

    for (i = 0; i < total; i++)
        cout << i+1 << log[i][0] << log[i][1];

    for (i = 0; i < makingMachine; i++) {
        rc = pthread_join(maker[i], &retval1);
        if (rc) {
            cout << "Error when joining threads!" << endl;
            exit(-1);
        }

        cout << "M, " << makerid[i]+1 << ", " << retval1 << endl;
        total_make += (long) retval1;
    }

    for (i = 0; i < packingMachine; i++) {
        rc = pthread_join(packer[i], &retval2);
        if (rc) {
            cout << "Error when joining threads!" << endl;
            exit(-1);           
        }

        cout << "P, " << packer[i] + 1 << ", " << retval2 << endl;
        total_pack += (long) retval2;
    }

    sem_destroy(&pack);
    pthread_exit(NULL);
}

0 个答案:

没有答案