Berkin
Berkin

Reputation: 1664

Changing A Value in Mulithread Programming

I have a project about marriage operations. In this program, a thread called registrar uses marriage function. In this marriage operations, we have brides and grooms. Marriage function does decrease bride count and groom count one by one. But i have a problem while i want to decrease these count.

MAIN.c

#include<pthread.h>
#include<stdlib.h>
#include<stdio.h>
#include<semaphore.h>
#include "bride.h"
#include "groom.h"

pthread_t groomThread;
pthread_t brideThread;
sem_t registrarSemaphore;
pthread_mutex_t lock;

int *groomCount = 14;
int *brideCount = 20;
int *availableRegistrar;

void createBride(int *brideCount) {
    pthread_create(&brideThread, NULL, &increaseBrideCount, (void *) brideCount);
}

void createGroom(int *groomCount) {
    pthread_create(&groomThread, NULL, &increaseGroomCount, (void *) groomCount);
}

void deleteGroom(int *groomCount) {
    pthread_create(&groomThread, NULL, &decreaseGroomCount, (void *) groomCount);
}

void deleteBride(int *brideCount) {
    pthread_create(&brideThread, NULL, &decreaseBrideCount, (void *) brideCount);
}


void marriage() {
    sem_init(&registrarSemaphore, 0, 2);

    while (1) {
        sem_getvalue(&registrarSemaphore, &availableRegistrar);

        printf("\nAvailable Registrar Number = %d\n", availableRegistrar);

        printf("bride %d\n", brideCount);
        printf("groom %d\n", groomCount);

        if (brideCount > 0 && groomCount > 0) {
            sem_wait(&registrarSemaphore);
            sem_getvalue(&registrarSemaphore, &availableRegistrar);
            printf("Available Registrar %d \n", availableRegistrar);

            printf("Marriage Bride %d and Groom %d \n", brideCount, groomCount);
            pthread_mutex_lock(&lock);
            deleteBride(brideCount);
            pthread_mutex_unlock(&lock);
            //pthread_join(brideThread, &brideCount);
            pthread_mutex_lock(&lock);
            deleteGroom(groomCount);
            pthread_mutex_unlock(&lock);
            //pthread_join(groomThread, &groomCount);
            printf("Exiting critical region...\n\n");
            /* END CRITICAL REGION */
            sem_post(&registrarSemaphore);
        }


        int random = rand() % 100;
        if (random % 7 > 4) {
            printf("Bride Created\n");
            pthread_mutex_lock(&lock);
            createBride(brideCount);
            //pthread_join(brideThread, &brideCount);
            pthread_mutex_unlock(&lock);

        }

        if (random % 7 < 2) {
            printf("Groom Created\n");
            pthread_mutex_lock(&lock);
            createGroom(groomCount);
            //pthread_join(groomThread, &groomCount);
            pthread_mutex_unlock(&lock);
        }

        pthread_join(brideThread, &brideCount);
        pthread_join(groomThread, &groomCount);

        for (int i = 0; i < 100000000; i++);
        printf("------------------------------");
    }
}

int main(void) {
    marriage();
}

In pthread_create part, there are some functions as you can see. It defined in .h part. For example in bride.h, there are 2 functions about bride.

BRIDE.H

#ifndef BRIDE_H
#define BRIDE_H

void* increaseBrideCount(void * bride);
void* decreaseBrideCount(void * bride);

#endif

BRIDE.C

#include <pthread.h>
#include "bride.h"


void* increaseBrideCount(void *bride){
    int brideCount = (int)bride;
    brideCount++;
    pthread_exit(brideCount);
}

void* decreaseBrideCount(void* bride){
    int brideCount = (int)bride;
    brideCount--;
    pthread_exit(brideCount);
}

While im creating new bride,i cant send the new value of bride to the function. For example :

I have 20 brides and 14 grooms at first. I have 2 available registrar

Marraige does.

Bride count = 19, groom count = 13

Then, i want to create new bride.

It count goes to = 1 :( Im trying to make it 20 again.

If you can help, i would be very happy. Thank you

Upvotes: 0

Views: 63

Answers (1)

Scott Hunter
Scott Hunter

Reputation: 49893

Semaphores are used to make sure only one of the threads that might make a particular change does so at a time. The things being changed in this case are the bride and groom counts, so you need to "protect" them using semaphores. You even seem to have created semaphores for this purpose (brideSemaphore and groomSemaphore); you just need to use them.

By the way: if you use a semaphore only in a single thread, you're wasting your time (as seems to be the case w/ your registrarSemaphore) in marriage()). Either it needs to be used elsewhere as well, or not at all.

Upvotes: 2

Related Questions