سؤال

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

#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include <pthread.h>
#include <semaphore.h>

#define MAX_STDNT 10

pthread_mutex_t ta;
//sem_t ta; //the ta is the semaphore

//void *request_and_help(void *arg)
//{
//  pthread_t cur_thread = pthread_self();
//
//  if (sem_wait(&ta) == -1) {
//      printf("TA is busy.... student(%d) programming....\n");
//  } else {
//      printf("student(%d) requesting help....\n", cur_thread);
//      sleep(1);
//      printf("TA is helping student(%d)....\n", cur_thread);
//      sleep(1);
//      printf("student(%d) DONE!!!\n", cur_thread);
//      sem_post(&ta);  //signal other waiting students to come in
//  }
//}

void *request_and_help(void *arg)
{
    pthread_t cur_thread = pthread_self();

    if (pthread_mutex_lock(&ta) != 0) {
        printf("TA is busy.... student(%d) programming....\n");
    } else {
        printf("student(%d) requesting help....\n", cur_thread);
        sleep(1);
        printf("TA is helping student(%d)....\n", cur_thread);
        sleep(1);
        printf("student(%d) DONE!!!\n", cur_thread);
        pthread_mutex_unlock(&ta);
    }

}

int main(int argc, char *argv[])
{
    int i;
    pthread_t stdnt[MAX_STDNT];

    //sem_init(&ta, 0, 1);
    pthread_mutex_init(&ta, NULL);

    for (i = 0; i < MAX_STDNT; i++)
        pthread_create(&stdnt[i], NULL, request_and_help, NULL);
    for (i = 0; i < MAX_STDNT; i++)
        pthread_join(stdnt[i], NULL);
}

my intention of this code is to understand how the POSIX APIS of thread application programming works.

Here's how it works, the ta works as a semaphore(or mutex) and the students are going to gain help one at a time. If the TA is busy (which means a lock is gained by a thread, it prints "busy" and the waiting thread is described as "programming". Well the fact is, if I compile this program and execute it, there is no waiting message at all.

obiously at least one thread must gain the semaphore resource (or mutex) and it will sleep 2 times for 1 seconds, meaning other threads(which obviously may have entered while) have to wait for the sempahore resource(or mutex) which I checked by printing "TA is busy student is programming".

But I don't see any of that message when I execute the program. I have two versions semaphore and mutex each for this code and one that I'm posting is using mutex currently. Can anybody help me?

P.S my environment is cygwin on windows and using a multithreaded processor netbook

هل كانت مفيدة؟

المحلول

If pthread_mutex_lock() is called on a mutex already locked, it blocks util the mutex in question had been unlocked.

To achieve the behaviour you want, you might like tto use pthread_mutex_trylock() like this:

void * request_and_help(void * arg)
{
  pthread_t cur_thread = pthread_self();

  int result = pthread_mutex_trylock(&ta);
  if (0 != result)
  {
    if (EBUSY == result) 
    {
      printf("TA is busy.... student(%d) programming....\n", cur_thread);
    }
    else
    {
      errno = result;
      perror("pthread_mutex_trylock() failed");
    }
  } 
  else 
  {
    [...]
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top