Member Avatar for OzY360

I got this code from a book and it doesn't seem to work. I've triple checked for common mistakes and syntax errors but can't find any. Please tell me where I am going wrong.

#include <pthread.h>
#include <stdio.h>
#define NUM_THREADS 5

int main(int argc, char *argv[]){
    int i, scope;
    pthread_t tid[NUM_THREADS];
    pthread_attr_t attr;

    /* get the default attribute */
    pthread_attr_init(&attr);

    /* first inquire on the current scope */
    if (pthread_attr_getscope(&attr, &scope) != 0)
        fprintf(stderr, "Unable to get scheduling scope\n");
    else {
        if (scope == PTHREAD_SCOPE_PROCESS)
            printf("PTHREAD_SCOPE_PROCESS");
        else if (scope == PTHREAD_SCOPE_SYSTEM)
            printf("PTHREAD_SCOPE_SYSTEM");
        else
            fprintf(stderr, "Illegal scope value.\n");
    }

    /* set the scheduling algorithm to PCS or SCS */
    pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);

    /* create the threads */
    for (i = 0; i < NUM_THREADS; i++)
        pthread_create(&tid[i],&attr,runner,NULL);

    /* now join on each thread */
    for (i=0; i < NUM_THREADS; i++)
        pthread_join(tid[i], NULL);
}

/* Each thread will begin control in this function */
void *runner(void *param){
    /* do some work ... */

    pthread_exit(0);
}
Salem commented: 36 posts, no code tags - here, HAVE A COOKIE -4

Recommended Answers

All 3 Replies

Member Avatar for OzY360

Here is the error I am getting:
/usr/bin/make -f nbproject/Makefile-Debug.mk SUBPROJECTS= .clean-conf
rm -f -r build/Debug
rm -f dist/Debug/GNU-MacOSX/pthread
CLEAN SUCCESSFUL (total time: 109ms)

/usr/bin/make -f nbproject/Makefile-Debug.mk SUBPROJECTS= .build-conf
/usr/bin/make -f nbproject/Makefile-Debug.mk dist/Debug/GNU-MacOSX/pthread
mkdir -p build/Debug/GNU-MacOSX
rm -f build/Debug/GNU-MacOSX/PThread.o.d
gcc -c -g -MMD -MP -MF build/Debug/GNU-MacOSX/PThread.o.d -o build/Debug/GNU-MacOSX/PThread.o PThread.c
PThread.c: In function 'main':
PThread.c:30: error: 'runner' undeclared (first use in this function)
PThread.c:30: error: (Each undeclared identifier is reported only once
PThread.c:30: error: for each function it appears in.)
make[2]: *** [build/Debug/GNU-MacOSX/PThread.o] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 283ms)

The compiler is complaining because you have not given it a prototype for the runner function.

Member Avatar for OzY360

What are you talking about? I thought there was no requirement to provide a prototype for a function in C. Besides this isn't my code, I typed it out from a book I have for Operating Systems. I just wanted to play around with it to see how it works. After all, thats how you learn right? Anyone got any useful comments or am I wasting my time?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.