简体   繁体   English

C ++ pthreads和struct的损坏数据

[英]c++ pthreads and struct's corrupting data

I'm new to pthreads and having a hard time creating a thread with a struct and keeping it's data intact while re-casting it from a void pointer. 我是pthreads的新手,很难从结构指针创建线程并保持数据完整,同时从void指针重铸它。

I've spent days searching around trying to find a reason for this and haven't had much luck. 我花了几天的时间在寻找解决这个问题的原因,但运气并不好。 Out of my two structures (using two different threads) one re-casts correctly in the thread, but for whatever reason, the second doesn't. 在我的两个结构中(使用两个不同的线程),其中一个正确地在该线程中重新铸造,但是出于某种原因,第二个却没有。

Structs: 结构:

struct Arguments {
    List linkedList;
    Node node;
    Arguments(){}
    Arguments (List *newList, Node *newNode){
        linkedList = *newList;
        pcb = *newPCB;
    }
};

struct ClockControl {
    int counter = 0;
    pthread_mutex_t lock;
};

Threads: 主题:

void *schedule(void *args){

    //Arguments *newArgs = static_cast<Arguments*>(args); <-- Tried this, doesn't work either.
    arguments *newArgs = (arguments *) args;
    List tempList = (newArgs ->linkedList);  //DATA HERE IS CORRUPTED/WRONG
    Node tempNode = (newArgs ->node);  //DATA HERE IS CORRUPTED/WRONG

    cout << "Flagged" << Lendl;

    return NULL;
}

void *clockTime(void *clock){
    //This thread works fine

    clockControl *newClock = (clockControl*) clock;
    int localVariable = 0;

    localVariable = (newClock -> counter);

    pthread_mutex_lock(&(newClock -> lock));
    localVariable++;
    newClock->counter = localVariable;
    pthread_mutex_unlock(&(newClock -> lock));

    return NULL;
}

Main: 主要:

int main(int argc, char** argv)
{  
    pthread_t threads[NUM_THREADS];  //Defined as 5

    clockControl clock;
    clock.counter = 0;
    pthread_mutex_init(&clock.lock, NULL);

    //Lists are initialized with variables.    
    List pendingList = initializeList();
    List readyList = initializeList();


    Arguments *args = new Arguments(&readyList, &pendingList.head->info);

    while (clock.counter < 6000){
        pthread_create(&threads[1], NULL, clockTime, (void*) &clock);

        if (clock.counter == pendingList.head->info.timeCreated){

            pthread_create(&threads[0], NULL, schedule, (void*) &args);
                          //INSPECTING args HERE HAS ACCURATE DATA

    }

        //Clean up threads
        for (int i = 0; i < 2; i++){
            pthread_join(threads[i],NULL);
        }
    }
}

Like I said, I've searched around and pretty much spinning my wheels at this point. 就像我说的那样,我已经四处搜寻,几乎旋转了我的车轮。 I have a suspicion it might be that the memory is being freed or cleaned up prior to the thread getting executed, but I can't find a way around it. 我怀疑它可能是在执行线程之前释放或清理了内存,但是我找不到解决方法。

Any help would be appreciated. 任何帮助,将不胜感激。

You are passing the address of the pointer to args to your schedule function. 您正在将指向args的指针的地址传递给您的Schedule函数。 When you use &args , args is already an Arguments* so you are passing an Arguments** . 当使用&argsargs已经是Arguments*因此您要传递Arguments** To solve this you can simply pass args by itself to the function. 为了解决这个问题,您可以简单地将args本身传递给函数。

Also just so you know, reinterpret_cast<Arguments*> is probably more appropriate than static_cast in this circumstance. 同样要知道,在这种情况下, reinterpret_cast<Arguments*>可能比static_cast更合适。

These two lines of code should be swapped 这两行代码应该交换

localVariable = (newClock -> counter);

pthread_mutex_lock(&(newClock -> lock)); z

ie

pthread_mutex_lock(&(newClock -> lock));
localVariable = (newClock -> counter);

So that localVariable will be the right value when more that one thread is reading the counter and updating it 这样,当有多个线程正在读取counter并对其进行更新时, localVariable将是正确的值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM