简体   繁体   English

C 中的 Pthreads,第二个线程不会执行

[英]Pthreads in C, Second Thread Won't Execute

I have this code that reads in two int numbers from a file.我有这段代码从文件中读取两个 int 数字。 And stores it in a buffer[] to be taken in a second function to be used.并将其存储在缓冲区[] 中,以便在第二个 function 中使用。 I'm not sure if my stopping conditions in the first function are correct.我不确定我在第一个 function 中的停止条件是否正确。 It looks fine but when running the code it stops at the second function.它看起来不错,但是在运行代码时它会在第二个 function 处停止。

static int count = 0;
int buffer[5];
int requestNum = 1;
FILE* f;

int main(int argc, char** argv)
{
    /*THREAD IDs*/
    pthread_t liftR, lift1;

    /*OPEN FILE*/
    f = fopen("sim_input.txt", "r");

    /*CREATE THREAD*/
    pthread_create(&liftR, NULL, request, NULL);
    pthread_create(&lift1, NULL, lift, NULL);

    /*RUNS TILL THREADS FINISHED*/
    pthread_join(liftR, NULL);
    pthread_join(lift1, NULL);
    /*CLEAN UP*/
    fclose(f);

    return 0;
} 
void* request(void* data1)
{
    int req1, req2, eof;

    /*NOT EOF*/
    while(eof != -1)
    {
        /*READ ONE REQUEST*/
        eof = fscanf(f, "%d %d", &req1, &req2);

        /*CHECK IF BUFFER FULLL*/
        if(count < 5)
        {
            /*ADD REQUEST TO BUFFER*/
            buffer[count] = req1;
            buffer[count + 1] = req2;

            count = count + 2;
            printf("COUNT: %d\n", count);
            /*PRINTING*/
            printf("-------------------------------------------\n");
            printf("From Buffer -> Item1: %d, Item2: %d\n", req1, req2);
            printf("Request No: %d\n", requestNum);
            printf("-------------------------------------------\n");

            requestNum++;
        }
    }

    return NULL;
}  

void* lift(void* data2)
{
    while(count > 0)
    {
        sleep(1);
        printf("================\n");
        printf("COUNT: %d\n", count);
        printf("REMOVE ITEM FROM BUFFER - DO STUFF WITH IT\n");
        printf("================\n");
        count = count - 2;
    }
    return NULL;
}  

OUTPUT: Shows count 2, 4, 6. Only 3 request shown, In file it goes up to 10 request OUTPUT:显示计数 2、4、6。仅显示 3 个请求,在文件中最多 10 个请求

There are multiple bugs in your program:您的程序中有多个错误:

  1. Reading uninitialized eof variable:读取未初始化eof变量:
    int int req1, req2, eof;  // What is the value of eof? It could be -1 (or anything else).

    /*NOT EOF*/
    while(eof != -1)
  1. You throw away the data you read:你扔掉你读到的数据:
        if(count < 5)
        {
            /*ADD REQUEST TO BUFFER*/
        }

If the first thread runs for a while before the second thread starts, then it will store the first two requests into the buffer , and throw away the rest .如果第一个线程在第二个线程开始之前运行了一段时间,那么它将前两个请求存储到buffer中,并丢弃 rest

To fix this, you need to wait for the second thread to drain the buffer when it is full.要解决此问题,您需要等待第二个线程在buffer已满时将其耗尽。

  1. You access count without any locking, which is a data race and undefined behavior.您在没有任何锁定的情况下访问count ,这是一种数据竞争和未定义的行为。 You must protect reading and writing shared (between threads) globals with a mutex.您必须使用互斥锁保护读取和写入共享(线程之间)全局变量。

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

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