简体   繁体   English

C pthread打印“异常”

[英]C pthread printing “anomaly”

I've been doing a program in C which uses a series of threads to represent cars crossing a bridge, the function I'm doing right know is the one that prints the "state" of the bridge at a certain moment, so it shows where cars (threads) are in the bridge and at which position. 我一直在用C编写一个程序,该程序使用一系列线程来表示汽车过桥,我正确地知道的功能是在某个时刻打印桥的“状态”的函数,因此它显示汽车(螺纹)在桥中的位置以及位置。 The problem is this; 问题是这样的; when there is only ONE thread crossing, the function prints properly and you can see the representation of the car advancing through the bridge. 当只有一个线程交叉时,该功能会正确打印,您可以看到行进通过桥的汽车的图示。 But when there are more threads crossing the function gets crazy and sometimes you can see a bridge and a half, or two bridges or some other crazy things. 但是,当有更多线程通过该函数时,该函数会变得疯狂,有时您会看到一个半桥,两个桥或其他一些疯狂的东西。 I tried to solve it creating a pthread_mutex for that function but it wasn't the solution, or at least I'm not using it at the right place. 我试图解决该问题,为此函数创建了一个pthread_mutex,但这不是解决方案,或者至少我没有在正确的位置使用它。 Every thread call this function as it advances through the bridge (the bridge is an array) Sorry if I failed to make it clear; 每个线程在桥接器中前进时都会调用此函数(桥接器是一个数组)对不起,如果我不清楚的话; here is the function and thanks for your time: 这是函数,感谢您的宝贵时间:

void printBridge(bridge *pBridge, int direction) {

system("clear");
int i;

if (direction == WEST) {
    for (i = 0; i < bridgeSize; i++) {
        pthread_mutex_lock(&print);
        if (pBridge->cellState[i] == 0) { //this means the position at the array isn't occupied
            fprintf(stderr, "\t");
        } else if (pBridge->cellState[i] == 1) { //this means a car is at that position
            fprintf(stderr, "  @@@>");  //the cars are represented by @@@>
        }
         pthread_mutex_unlock(&print);
    }
} else {

    for (i = bridgeSize - 1; i >= 0; i--) { //if going the other direction
        pthread_mutex_lock(&print);
        if (pBridge->cellState[i] == 0) {
            fprintf(stderr, "\t");
        } else if (pBridge->cellState[i] == 1) {
            fprintf(stderr, "<@@@ ");

        }
         pthread_mutex_unlock(&print);
    }


}
printf("\n--------------------------------------------------------------------------\n");
fprintf(stderr, "\n Direction= %d", pBridge->direction);

sleep(1); //because I need to see what's going on
 }

So...a proper print would be like this: 所以...正确的打印是这样的:

        <@@@        <@@@ 
--------------------------------------------------------------------------

But sometimes it gets as messy as this: 但有时它变得像这样混乱:

    <@@@ <@@@ <@@@ <@@@     
    --------------------------------------------------------------------------
<@@@ <@@@ <@@@ <@@@     

 Direction= 1--------------------------------------------------------------------------

Could it be because of the system("clear") being executed by many threads at a time? 可能是因为系统(“清除”)一次被多个线程执行了吗?

SOLVED: 解决了:

Solved calling the pthread_mutex_lock and unlock outside the function using a mutex for each position of the array ( bridge): 解决了调用pthread_mutex_lock并使用互斥量为数组的每个位置(桥)在函数外部解锁的方法:

    pthread_mutex_lock(&pBridge->mutexBridge[i]);
    pBridge->cellState[i]=1;
    printBridge(pBridge,dir);
    pBridge->cellState[i]=0;
    pthread_mutex_unlock(&pPuente->mutexBridge[i]);

And for some reason, inside the printBridge function, wrapped the system("clear") with mutex as well: 出于某种原因,在printBridge函数内部,还用互斥体包装了系统(“ clear”):

pthread_mutex_lock(&print);
system("clear");
pthread_mutex_unlock(&print);

Not doing the above gave me crazy prints. 不执行上述操作会给我疯狂的打印。 Thanks to pat for the help 感谢拍拍的帮助

You are only locking around printing of individual cars and spaces, so, when multiple threads are printing, they will interleave the bridges on a car by car basis. 您只能锁定单个轿厢和空间的打印,因此,当打印多个线程时,它们将逐个轿厢地交错桥。 You need to acquire the mutex before you start printing the bridge and release it when you are done. 您需要先获取互斥锁,然后才能开始打印桥接并在完成后释放它。 In this way, threads will interleave whole bridges instead of individual cars. 这样,线程将交错整个桥而不是单个车。

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

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