线程工作不当

时间:2016-11-22 14:23:40

标签: c multithreading struct pthreads posix

我在c

中编写此代码
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>

typedef struct
{
    int customerID;
    int ticketNumber;
    sem_t * freeCashiers;
    sem_t Cashiers[2];
}Customer;

int firstIsFree = 1;
int secondIsFree = 1;

void * buyTheTickets(void * data)
{
    Customer * currentCustomer = (Customer *)data;
    int ID = currentCustomer->customerID;

    currentCustomer->ticketNumber = rand()%15+1;

    sem_wait(currentCustomer->freeCashiers);    

    sem_wait(&(currentCustomer->Cashiers[0]));

    if(firstIsFree)
    {
        firstIsFree = 0;
        printf("First Cashier Sold %d Ticket To %d Customer\n",currentCustomer->ticketNumber,ID);

        usleep(1000000);
        firstIsFree = 1;

        sem_post(&(currentCustomer->Cashiers[0]));
        sem_post(currentCustomer->freeCashiers);

        return 0;
    }

    sem_post(&(currentCustomer->Cashiers[0]));

    sem_wait(&(currentCustomer->Cashiers[1]));

    if(secondIsFree)
    {
        secondIsFree = 0;
        printf("Second Cashier Sold %d Ticket To %d Customer\n",currentCustomer->ticketNumber,ID);

        usleep(1000000);
        secondIsFree = 1;

        sem_post(&(currentCustomer->Cashiers[1]));
        sem_post(currentCustomer->freeCashiers);

        return 0;
    }
    sem_post(&(currentCustomer->Cashiers[1]));
}

int main()
{
    int numberOfCustomers = 15;
    sem_t * numberOfCashiers = (sem_t*)malloc(sizeof(sem_t));
    sem_init(numberOfCashiers,0,2);
    sem_t first;
    sem_t second;
    sem_init(&first,0,1);
    sem_init(&second,0,1);

    for(int i=1;i<=numberOfCustomers;i++)
    {
        pthread_t * curTh = (pthread_t*)malloc(sizeof(pthread_t));
        Customer * curCu = (Customer *)malloc(sizeof(Customer));
        curCu->customerID = i;
        curCu->freeCashiers = numberOfCashiers;
        curCu->Cashiers[0] = first;
        curCu->Cashiers[1] = second;
        pthread_create(curTh,NULL,buyTheTickets,curCu);     
    }

    pthread_exit(0);
    return 0;
}

它提供输出:

First Cashier Sold 14 Ticket To 1 Customer

Second Cashier Sold 2 Ticket To 2 Customer

First Cashier Sold 13 Ticket To 3 Customer

Second Cashier Sold 11 Ticket To 4 Customer

First Cashier Sold 9 Ticket To 5 Customer

Second Cashier Sold 11 Ticket To 6 Customer

First Cashier Sold 2 Ticket To 7 Customer

Second Cashier Sold 13 Ticket To 8 Customer

First Cashier Sold 10 Ticket To 9 Customer

Second Cashier Sold 2 Ticket To 10 Customer

First Cashier Sold 3 Ticket To 11 Customer

Second Cashier Sold 8 Ticket To 12 Customer

First Cashier Sold 6 Ticket To 13 Customer

Second Cashier Sold 5 Ticket To 14 Customer

First Cashier Sold 9 Ticket To 15 Customer

问题是,如果第二个帖子在sem_wait(&(currentCustomer->Cashiers[0]));之后,这意味着sem_post(&(currentCustomer->Cashiers[0]));这已经发生了,所以firstIsFree已经是1,那为什么要打印第二个printf?

1 个答案:

答案 0 :(得分:2)

您的代码中存在以下问题,可能导致未定义的行为。请更正它并再次尝试执行:

在main函数中,您使用信号量firstsecond的局部变量,主函数似乎退出而不等待线程退出。您需要在主要功能中进行pthread_join次呼叫。