LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-07-2004, 09:15 AM   #1
swinchen
Member
 
Registered: Apr 2004
Location: Maine (USA)
Distribution: Slackware 9.1
Posts: 45

Rep: Reputation: 15
Creating a counting semaphore from pthreads


Hi All,

I am trying to create a couting semaphore from the pthread library. Here is my solution. My professor seems to think there is a problem with, but I am less than convinced. Can anyone see any problem with this solution.

He seems to think the problem is when the count goes negative.

Thanks,
Sam.

sem.h
Code:
#ifndef COUNTING_SEM
#define COUNTING_SEM

#include <pthread.h>

typedef struct
{
  pthread_mutex_t mutex;
  pthread_mutex_t cond_m;
  pthread_cond_t cond;
  int count;
} sem_t;


void sem_init(sem_t *sem, int count);
void sem_delete(sem_t *sem);
void sem_up(sem_t *sem);
void sem_down(sem_t *sem);

#endif
sem.c
Code:
#include "sem.h"
#include <pthread.h>

void sem_init(sem_t *sem, int count)
{
  pthread_mutex_init(&(sem->mutex), 0);
  pthread_mutex_init(&(sem->cond_m),0);
  pthread_cond_init(&(sem->cond), 0);
  sem->count = count;
  return;
}

void sem_delete(sem_t *sem)
{
  pthread_mutex_destroy(&(sem->mutex));
  pthread_mutex_destroy(&(sem->cond_m));
  pthread_cond_destroy(&(sem->cond));
  return;
}

void sem_down(sem_t *sem)
{
  pthread_mutex_lock(&(sem->cond_m));
  pthread_mutex_lock(&(sem->mutex));
  sem->count--;
  if(sem->count < 0)
  {
    pthread_mutex_unlock(&(sem->mutex));
    pthread_cond_wait(&(sem->cond), &(sem->cond_m));
    pthread_mutex_unlock(&(sem->cond_m));
  }
  else
  {
    pthread_mutex_unlock(&(sem->mutex));
    pthread_mutex_unlock(&(sem->cond_m));
  }

  return;
}

void sem_up(sem_t *sem)
{
  pthread_mutex_lock(&(sem->cond_m));
  pthread_mutex_lock(&(sem->mutex));
  sem->count++;
  pthread_mutex_unlock(&(sem->mutex));
  pthread_mutex_unlock(&(sem->cond_m));
  pthread_cond_signal(&(sem->cond));
  return;
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
difference mutex vs. semaphore? Thinking Programming 1 11-16-2005 05:26 AM
Semaphore tzzdvd Programming 1 05-24-2005 09:23 PM
Links for semaphore and multithreading indian Programming 3 09-05-2004 12:40 AM
semaphore beginner_84 Programming 5 08-25-2004 06:54 PM
about semaphore dummyagain Programming 1 11-11-2003 11:09 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:18 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration