简体   繁体   English

多线程方案中的线程退出差异

[英]thread exit discrepency in multi-thread scenario

I have written a very simple code for practicing semaphore. 我已经编写了一个非常简单的代码来练习信号量。 Task is to sell tickets and each thread should update the shared variable of total tickets till it becomes zero. 任务是出售票证,每个线程应更新票证总数的共享变量,直到变为零为止。 Issue I am observing is - the thread which sells last ticket and makes ticket_count = 0 by decrementing it exits without printing how many total tickets it sold. 我观察到的问题是-售出最后一张票并通过减少其退出而使ticket_count = 0的线程而不打印其售出的总票数。 I added mutex around printf just for hack of it as I read about issues of printf in multi-threaded environment on SO. 我在printf周围添加了互斥锁只是为了破解它,因为我了解了SO上多线程环境中的printf问题。 Reason I find this issue different than normal printf issues pointed at SO with regard to multi-threading is - its always ( not always 8/10 times - issue is intermittent - other 2 times it prints all 4 threads ticket sold) the last thread ie the one which sells last ticket skips the printf and exits. 我发现此问题与针对多线程的普通printf问题不同的原因是-它总是(并非总是8/10次-问题是间歇性的-其他2次它打印所有售出的4个线程票)最后一个线程,即卖出最后一张票的人跳过printf并退出。 Can someone point me what am I doing wrong ? 有人可以指出我在做什么错吗?

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <semaphore.h>
#include <stdbool.h>
#include <unistd.h>

#define NUMTHREADS  4       /* number of threads i.e. ticket sellers */

static unsigned int ticket_count = 25;

pthread_mutex_t mutex2 = PTHREAD_MUTEX_INITIALIZER;

sem_t mutex;

void *ticketProcessing(void *arg)
{
    bool loop_alive = true;
    int local_counter = 0;
    while(loop_alive)
    {
        sleep(1);
        sem_wait(&mutex);
        if (ticket_count > 0)
        {
            local_counter++;
            ticket_count--;
            printf(" thread id -- %d - decided to sell 1 ticket \n ", (int)pthread_self());
        }
        else
        {
            loop_alive = false;
        }
        sem_post(&mutex);
    }

    pthread_mutex_lock(&mutex2);
    printf(" ticket sold by thread id -- %d -- is  %d \n ", (int)pthread_self(), local_counter);
    pthread_mutex_unlock(&mutex2);
  //  return 0;
}

int main(void)
{
    pthread_t sellingTicket;
    sem_init(&mutex, 0, 1);
    int i;

    for(i = 0; i < NUMTHREADS; i++)
    {
        pthread_create(&sellingTicket, NULL, ticketProcessing, NULL);
    }

    i = 0;
    while(i < NUMTHREADS)
    {
        pthread_join(sellingTicket, NULL);
        i++;
    }


    printf(" All threads exited !!! \n ");
    return 0;
}

Edit : I tried to create array and join in below fashion and it works 编辑:我试图创建数组并以下面的方式加入,它的工作原理

 pthread_t threads[4];

 for(i = 0; i < 4; i++)
 {
     pthread_create(&threads[i], NULL, ticketProcessing, NULL);
 }

for(i = 0; i < 4; i++)
{
    pthread_join(threads[i], NULL);
}

Your join loop tries to join the last thread over and over rather than trying to join each thread that was created. 您的join循环尝试一遍又一遍地加入最后一个线程,而不是尝试加入所创建的每个线程。 You need to keep track of the thread IDs so that you can join them all. 您需要跟踪线程ID,以便可以将它们全部加入。

Once you've joined a thread, the thread ID is invalid and it is a serious mistake to pass it to any pthread_* function. 加入线程后,线程ID无效,将其传递给任何pthread_ *函数是一个严重的错误。

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

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