Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

formal-ass code review

Name: Anonymous 2011-04-21 16:40

I made a traffic light simulation with pthreads, can you pls review this code


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

#define RED 0
#define AMBER_UP 1
#define AMBER_DOWN 2
#define GREEN 3

#define FOR_HOW_MANY_FUCKING_TIMES 13

pthread_mutex_t m;
pthread_mutex_t io_m;
pthread_cond_t c;

void print_color(int c)
{
    switch (c) {
    case RED:
        printf("RED");
        break;
    case AMBER_UP:
        printf("AMBER");
        break;
    case AMBER_DOWN:
        printf("AMBER");
        break;
    case GREEN:
        printf("GREEN");
        break;
    }
}

void print_stuff(int* p_val)
{
    pthread_mutex_lock(&io_m);
    printf("[%x]", (int)p_val);
    print_color(*p_val);
    printf("\n");
    pthread_mutex_unlock(&io_m);   
    pthread_mutex_lock(&m);
}

void* simulate(void* p_arg)
{
    int iter = FOR_HOW_MANY_FUCKING_TIMES;
    int* p_val = (int*)p_arg;
    while (iter--) {
        print_stuff(p_val);
        /* begin real stuff */
        switch(*p_val) {
        case RED:
            pthread_cond_wait(&c, &m);
            *p_val = AMBER_DOWN;
            break;
        case AMBER_DOWN:
            *p_val = GREEN;
            break;
        case GREEN:
            pthread_cond_signal(&c);
            *p_val = AMBER_UP;
            break;
        case AMBER_UP:
            *p_val = RED;
            break;
        }
    }
    pthread_mutex_unlock(&m);
    pthread_mutex_lock(&io_m);
    printf("[%x] exiting...\n", (int)p_val);
    pthread_mutex_unlock(&io_m);
    pthread_exit(NULL);
    return NULL;
}

int main()
{
    int alpha = RED;
    int beta = GREEN;

    pthread_t alpha_worker, beta_worker;
    pthread_mutex_init(&m, NULL);
    pthread_mutex_init(&io_m, NULL);
    pthread_cond_init(&c, NULL);
    pthread_create(&alpha_worker, NULL, simulate, (void*)&alpha);
    pthread_create(&beta_worker, NULL, simulate, (void*)&beta);
    pthread_join(alpha_worker, NULL);
    pthread_join(beta_worker, NULL);
    pthread_exit(NULL);
    return 0;
}

Name: Anonymous 2011-04-21 16:41

case int c is being passed as a string

Name: Anonymous 2011-04-21 16:43

*blinks*
...
o.O ... 0.o ...

Name: Anonymous 2011-04-21 16:45

er I mean its being passed as an INT but you're looking for a string. See?

Name: Anonymous 2011-04-21 16:49

Eh?
Give me a link to pic: "Have you read your SICP today?"-with-the-crazy-trouser-snake-guy !

Name: Anonymous 2011-07-04 5:10

THREADSURRECTION

Name: Anonymous 2011-07-04 5:45

>>5
Do you mean /prog/snake?

Name: Anonymous 2011-07-04 5:54

>>7
It's a Python.

Name: Anonymous 2011-07-04 11:50

Why do people write code like this?
Have they no sense of beauty?

Please learn to space and align your code.

Name: Anonymous 2011-07-04 14:35

>>8
I don't think you're even allowed to post here.

Don't change these.
Name: Email:
Entire Thread Thread List