Home > Software design >  What is the pthread_cond_wait() & pthread_cond_signal() calling sequence?
What is the pthread_cond_wait() & pthread_cond_signal() calling sequence?

Time:11-14

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

pthread_mutex_t lockid;
pthread_cond_t cvar = PTHREAD_COND_INITIALIZER;
char name[14];

void *entry()
{
    pthread_mutex_lock(&lockid);

    printf("Enter Name\n");
    scanf("%s",name);

    pthread_cond_signal(&cvar);
    pthread_mutex_unlock(&lockid);
    pthread_exit(NULL);

}

void *display()
{
    pthread_mutex_lock(&lockid);
    pthread_cond_wait(&cvar,&lockid);

    printf("Name: %s\n",name);

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

int main()
{
    pthread_t id1,id2;
    if(pthread_mutex_init(&lockid,NULL)== 0){
        pthread_create(&id1,NULL,&entry,NULL);
        pthread_create(&id2,NULL,&display,NULL);

        pthread_join(id1,NULL);
        pthread_join(id2,NULL);
    }
    return 0;
}

I am expecting that the thread id2 to print the name that I have entered on thread id1. I know it's the pthread_cond_wait()doesn't allow the thread id2 to display the name entered on thread id1 but signal() should be called before wait() right ? Is the way of calling wait() and signal() methods correct? I am new to linux programming please help me on this.

CodePudding user response:

If you have a sequential task don't use threads:

#include <stdio.h>

char name[14];

int main() {
    printf("Enter Name\n");
    scanf("%s",name);
    printf("Name: %s\n", name);
    return 0
}

As you need entry() to run before display(), record when that happens with a state variable:

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

pthread_mutex_t lockid;
pthread_cond_t cvar = PTHREAD_COND_INITIALIZER;
char name[14];
enum { START, ENTRY } state = START;

void *entry() {
    pthread_mutex_lock(&lockid);
    printf("Enter Name\n");
    scanf("%s", name);
    state = ENTRY;
    pthread_cond_signal(&cvar);
    pthread_mutex_unlock(&lockid);
    return 0;
}

void *display() {
    pthread_mutex_lock(&lockid);
    while(state != ENTRY)
        pthread_cond_wait(&cvar, &lockid);
    printf("Name: %s\n", name);
    pthread_mutex_unlock(&lockid);
    return 0;
}

int main() {
    if(pthread_mutex_init(&lockid, NULL)) return 1;
    pthread_t id1, id2;
    pthread_create(&id1, NULL, &entry, NULL);
    pthread_create(&id2, NULL, &display, NULL);
    pthread_join(id1,NULL);
    pthread_join(id2,NULL);
    return 0;
}

You can also just create the entry() thread once you hold the mutex in display():

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

pthread_mutex_t lockid;
pthread_cond_t cvar = PTHREAD_COND_INITIALIZER;
char name[14];

void *entry() {
    pthread_mutex_lock(&lockid);
    printf("Enter Name\n");
    scanf("%s",name);
    pthread_cond_signal(&cvar);
    pthread_mutex_unlock(&lockid);
    return NULL;
}

void *display() {
    pthread_mutex_lock(&lockid);

    pthread_t id1;
    pthread_create(&id1, NULL, &entry, NULL);

    pthread_cond_wait(&cvar, &lockid);
    printf("Name: %s\n", name);
    pthread_mutex_unlock(&lockid);

    pthread_join(id1, NULL);
    return NULL;
}

int main() {
    if(pthread_mutex_init(&lockid, NULL)) return 1;
    pthread_t id2;
    pthread_create(&id2, NULL, &display, NULL);
    pthread_join(id2, NULL);
    return 0;
}
  • Related