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 */

    /* 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)
        else if (scope == PTHREAD_SCOPE_SYSTEM)
            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++)

    /* 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 ... */


Edited by Reverend Jim: Fixed formatting

Votes + Comments
36 posts, no code tags - here, HAVE A COOKIE
8 Years
Discussion Span
Last Post by 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.


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?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.