-1

I have a school project that requires me to wrote a program printing this: <ONE><TWO><THREE><ONE><TWO><THREE><ONE><TWO><THREE>….............. using 3 threads and mutex. I have tried to do it with some help of the class, but it just keeps printing only <ONE>. Can you help me solve my problem and understand what have i wrong?

#include <pthread.h>
#include <stdio.h>

static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

void *func(void *arg)
{
    pthread_mutex_lock(&mutex);
    while (1) {
        printf ("<ONE>");
    }
    pthread_mutex_unlock(&mutex);
    pthread_exit(NULL);
}

void *func2(void *arg)
{
    pthread_mutex_lock(&mutex);
    while (1) {
        printf ("<TWO>");
    }
    pthread_mutex_unlock(&mutex);
    pthread_exit(NULL);
}

void *func3(void *arg)
{
    pthread_mutex_lock(&mutex);
    while (1) {
        printf ("<THREE>");
    }
    pthread_mutex_unlock(&mutex);
    pthread_exit(NULL);
}

main()

{
    pthread_t mythread1,mythread2,mythread3;

    pthread_create( &mythread1, NULL, func, (void *) 1);
    pthread_create( &mythread2, NULL, func2, (void *) 2);
    pthread_create( &mythread3, NULL, func3, (void *) 3);

    pthread_join ( mythread1, NULL);
    pthread_join ( mythread2, NULL);
    pthread_join ( mythread3, NULL);

    exit(0);
}
klutt
  • 30,332
  • 17
  • 55
  • 95
  • 2
    When do you expect the loop `while(1) { printf(""); }` to stop? – klutt Jun 15 '19 at 16:35
  • Are you only allowed to use one mutex? – klutt Jun 15 '19 at 16:38
  • @klutt the problem is that it doesn't stop. I want it to stop, then prin , then and all that again. i have done something wrong and the loop never stops. There is no limitations about how many mutex i have to use. Just because im not familiar with them i used one – miltos taramanidis Jun 15 '19 at 16:41
  • Yes, I'm just pointing out the obvious fact that since you never unlock the mutex it will get stuck in an infinite loop. You will have to lock and unlock it inside the loop. – klutt Jun 15 '19 at 16:43
  • @klutt okay done that. Now it just prints at random times or . not in the order i want to – miltos taramanidis Jun 15 '19 at 16:47
  • the posted code does not cleanly compile! When compiling, always enable the warnings, then fix those warnings. ( for `gcc`, at a minimum use: `-Wall -Wextra -Wconversion -pedantic -std=gnu11` ) Note: other compilers use different options to produce the same results – user3629249 Jun 15 '19 at 18:51

1 Answers1

1

As I made clear in the comments, this will get stuck in an infinite loop because you are doing the locking and unlocking outside the loop. First step is to move them inside.

void *func(void *arg)
{
    while (1) {
        pthread_mutex_lock(&mutex);
        printf ("<ONE>");
        pthread_mutex_unlock(&mutex);
   }
   pthread_exit(NULL);
}

Next, we need to add synchronization. An easy way to do that is to declare a global variable:

int next = 1; 

Then we modify the function like this:

void *func(void *arg)
{
    while (1) {
        while(1) {
            pthread_mutex_lock(&mutex);
            if(next == 1) break;
            pthread_mutex_unlock(&mutex);
        }

        printf ("<ONE>");

        next = 2;

        pthread_mutex_unlock(&mutex);
   }
   pthread_exit(NULL);
}

In func2 and func3 you need to modify if(next == 1) and next = 2 to appropriate values. func2 should have 2 and 3 while func3 should have 3 and 1.

This method is called busy waiting and is often not the best choice since it's quite cpu intense. A better alternative would be to look into pthread_cond_wait(). You can read about it here: http://pubs.opengroup.org/onlinepubs/7908799/xsh/pthread_cond_wait.html

klutt
  • 30,332
  • 17
  • 55
  • 95