-3

What's the difference between mutex_lock and pthread_join in these two source codes? They seem both to do the same thing, making the main function wait for the thread to finish execution.

This code:

#include "philo.h"

typedef struct s_bablo
{
    pthread_mutex_t mutex;
} t_bablo;

void *myturn(void *arg)
{
    t_bablo *bablo = (t_bablo *)arg;
    int i = 0;
    while(i < 10)
    {
        printf("My Turn ! %d\n", i);
        i++;
        sleep(1);
    }
    pthread_mutex_unlock(&bablo->mutex);
}

void *yourturn()
{
    int i = 0;
    while(i < 5)
    {
        printf("Your Turn ! %d\n", i);
        i++;
        sleep(1);
    }
}

int main ()
{
    t_bablo bablo;
    pthread_mutex_init(&bablo.mutex, NULL);
    pthread_t ph;
    pthread_mutex_lock(&bablo.mutex);
    pthread_create(&ph, NULL, myturn, &bablo);
    yourturn();
    pthread_mutex_lock(&bablo.mutex);

}

And this code :

#include "philo.h"

void *myturn(void *arg)
{
    int i = 0;
    while(i < 10)
    {
        printf("My Turn ! %d\n", i);
        i++;
        sleep(1);
    }
}

void *yourturn()
{
    int i = 0;
    while(i < 5)
    {
        printf("Your Turn ! %d\n", i);
        i++;
        sleep(1);
    }
}

int main ()
{
    pthread_t ph;
    pthread_create(&ph, NULL, myturn, NULL);
    yourturn();
    pthread_join(ph, NULL);

}
John Bollinger
  • 160,171
  • 8
  • 81
  • 157
sFinoe
  • 1
  • 1
  • 1
    Hello Please edit your code to make it readable. – Mathieu Feb 04 '22 at 16:22
  • Does this [Difference between mutex lock and pthread_join](https://stackoverflow.com/questions/6053421/difference-between-mutex-lock-and-pthread-join) answer your question? – raiyan22 Feb 04 '22 at 16:24
  • 1
    They're totally different. One waits for a lock to be cleared, the other waits for a thread to finish. – Barmar Feb 04 '22 at 16:24
  • They would only be equivalent if the thread locks the mutex for the entire time that it's running. – Barmar Feb 04 '22 at 16:24
  • @Barmar, no, cause `join` will actually free the thread resources. – ikegami Feb 04 '22 at 16:25
  • 1
    The mutex-based version has undefined behavior because it relies on a different thread to unlock the mutex than the one that locked it. You could do that with a semaphore, but not with a mutex. – John Bollinger Feb 04 '22 at 16:55
  • @Barmar, no, they would not be equivalent in that case. The program would then deadlock because the main thread locks the mutex before starting the second thread. And if the main thread did not do that then program behavior would depend on which thread managed to lock the mutex first -- if the main thread, which would be possible in principle, then the program would terminate before the other acquired the mutex. – John Bollinger Feb 04 '22 at 17:00

1 Answers1

1

not to be rude but you can easily find the difference by googling both functions name...

Though pthread_mutex_lock is for variables. It locks this variable for the current running thread. Thus no other thread can use it, they have to wait for pthread_mutex_unlock to use it.

pthread_join waits for the specified thread to finish it's execution before continuing

I encourage you to read the man pages, they are really self explanatory.

Jules
  • 346
  • 1
  • 3
  • 15