Voting

Category

real language

Bookmarking

Del.icio.us Digg Diigo DZone Earthlink Google Kick.ie
Windows Live LookLater Ma.gnolia Reddit Rojo StumbleUpon Technorati

Language C

(multithreaded version)

Date:05/11/05
Author:Stefan Scheler
URL:http://sts.synflood.de/
Comments:4
Info:http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html
Score: (3.00 in 44 votes)
/*
 * C (multithreaded) using POSIX Thread Library (pthread)
 * by Stefan Scheler <sts[at]synflood[dot]de>
 * Ilmenau, Germany - May 2005
 *
 * compile with: gcc -pthread -o 99bottles 99bottles.c
 * disable debug output with: ./99bottles 2>/dev/null
 *
 */
 
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h> 
 
#define THREADS 10
#define PLURALS (bottles>1) ? "s" : ""

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int bottles = 99;
 
void *takeonedownandpassitaround(void *ptr) {

    struct timespec sleeptime;
    int *thread_id = (int *)ptr;
    
    while (1) {
        pthread_mutex_lock(&mutex);
        if (bottles>0) {
            fprintf(stderr, "Thread %d: ", *thread_id);
            printf("%d bottle%s of beer on the wall, %d bottle%s of beer.\n", bottles, PLURALS, \
                bottles, PLURALS);
            fprintf(stderr, "Thread %d: ", *thread_id);
            printf("Take one down and pass it around, ");
            if (bottles==1)
                printf("no more bottles of beer on the wall.\n");
            else
                printf("%d bottle%s of beer on the wall.\n", bottles-1, PLURALS);
        } else if (bottles==0) {
            fprintf(stderr, "Thread %d: ", *thread_id);
            printf("No more bottles of beer on the wall, no more bottles of beer.\n");
            fprintf(stderr, "Thread %d: ", *thread_id);
            printf("Go to the store and buy some more, 99 bottles of beer on the wall.\n");
        } else {
            pthread_mutex_unlock(&mutex);
            break;
        }
        bottles--;
        pthread_mutex_unlock(&mutex);
        sleeptime.tv_nsec = 0;
        sleeptime.tv_sec = rand() % 10;
        fprintf(stderr, "Thread %d: sleeps for %d ms\n", *thread_id, sleeptime.tv_sec);
        nanosleep(&sleeptime, NULL);
    }
}

int main(void) {

    pthread_t thread[THREADS];
    int threadid[THREADS];
    int i;
    
    srand(time(NULL));
    
    for (i=0; i<THREADS; i++) {
        threadid[i] = i;
        pthread_create(&thread[i], NULL, takeonedownandpassitaround, &threadid[i]);
    }
    
    for (i=0; i<THREADS; i++)             
        pthread_join(thread[i], NULL);
    
    exit(EXIT_SUCCESS);
    
}

Download Source | Write Comment

Alternative Versions

VersionAuthorDateCommentsRate
actually produces correct lyrics :PDustshine08/20/050
Linux kernel moduleStefan Scheler08/02/0515
Correct ANSI C containing no semicolonsSteve Checkoway01/15/090
poor StyleMatteo Casati09/01/056
standard versionBill Wein04/20/054

Comments

>>  Tony said on 08/24/05 16:29:09

Tony Do you have any other examples of code? I am trying to write a simple program that first says hello and asks you to input your name. It then says hello again and displays your name..?

>>  Nukesquad said on 11/09/05 17:01:24

Nukesquad Are you using threads just because you can? I don't see why they are nessesary here. Other than that, it looks pretty good.

>>  Stefan said on 11/10/05 17:06:11

Stefan Well, of course this isn't necessary. Nothing on this whole page is really necessary ;)

>>  AshleyPOWERS24 said on 03/04/10 07:47:25

AshleyPOWERS24 Have no a lot of money to buy some real estate? You not have to worry, because that's available to receive the <a href="http://lowest-rate-loans.com">loans</a> to solve such kind of problems. Thence take a bank loan to buy everything you require.

Download Source | Write Comment

Add Comment

Please provide a value for the fields Name, Comment and Security Code.
This is a gravatar-friendly website.
E-mail addresses will never be shown.
Enter your e-mail address to use your gravatar.

Please don't post large portions of code here! Use the form to submit new examples or updates instead!

Name:

eMail:

URL:

Security Code:
  
Comment: