Wrox Programmer Forums
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old December 1st, 2007, 02:59 AM
Authorized User
 
Join Date: Oct 2004
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to C@uark
Default pthread help

I have the following program compiling on linux w/ gcc,
my three thread functions have been defined just omitted.

when compiling i get these errors:

/tmp/ccMjhBVI.o: In function `main':
assign5.c:(.text+0x70): undefined reference to `pthread_create'
assign5.c:(.text+0xa3): undefined reference to `pthread_create'
assign5.c:(.text+0xd6): undefined reference to `pthread_create'
/tmp/ccMjhBVI.o: In function `sqrt_thread':
assign5.c:(.text+0x4e5): undefined reference to `sqrt'

as you can see I have included pthread.h for pthread_create
and math.h for sqrt

does anyone have any ideals


#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <pthread.h>

#define NUM_STAGES 4
#define EMPTY -1
#define USR_DBL 0
#define DBL_SQRT 1
#define SQRT_POW 2
#define POW_USR 3

typedef struct stage_object
{
    pthread_mutex_t mutex;
    pthread_cond_t cond;
    int buffer;
} stage;

stage stages[NUM_STAGES] = { {PTHREAD_MUTEX_INITIALIZER,PTHREAD_COND_INITIALIZE R,EMPTY},
    {PTHREAD_MUTEX_INITIALIZER,PTHREAD_COND_INITIALIZE R,EMPTY},
    {PTHREAD_MUTEX_INITIALIZER,PTHREAD_COND_INITIALIZE R,EMPTY},
    {PTHREAD_MUTEX_INITIALIZER,PTHREAD_COND_INITIALIZE R,EMPTY} };

void* dbl_thread(void*);
void* sqrt_thread(void*);
void* pow2_thread(void*);

int main( int argc, char** argv )
{

    int status;
    int userInput;
    int pipeLine_outPut = 0;
    pthread_t dbl_thread_id, sqrt_thread_id, pow2_thread_id;

    printf("This multi-threaded program calculates pow(sqrt(x*2)),2) for a given input\n");
    printf("Enter non-neg integer to calculate, = to see contents of pipeline or non-neg integer to quit\n");

    for(scanf("%d",&userInput); userInput > 0; scanf("%d",&userInput))
    {
        if(userInput == '=')
            printf("Pipe line output: %d\n",pipeLine_outPut);
        //dbl thread
        status = pthread_create(&dbl_thread_id, NULL, dbl_thread, NULL);
        if(status != 0)
            printf("status = %d, dbl_thread not created\n",status);
        //sqrt thread
        status = pthread_create(&sqrt_thread_id, NULL, sqrt_thread, NULL);
        if(status != 0)
            printf("status = %d, sqrt_thread not created\n",status);
        //pow2 thread
        status = pthread_create(&pow2_thread_id, NULL, pow2_thread, NULL);
        if(status != 0)
            printf("status = %d, pow2_thread not created\n",status);
        //main thread consumer problem
        status = pthread_mutex_lock(&stages[POW_USR].mutex);
        if(status != 0)
            printf("status = %d, main thread mutex lock failed in consumer problem\n",status);
        while(stages[POW_USR].buffer == EMPTY)
        {
            status = pthread_cond_wait(&stages[POW_USR].cond, &stages[POW_USR].mutex); // wait for POW_USR to become full
            if(status != 0)
                printf("status = %d, main thread cond wait failed in consumer problem\n",status);
        }
        //POW_USR not EMPTY and/or has been signalled operate on POW_USR crit region
        pipeLine_outPut = stages[POW_USR].buffer; //POW_USR data consumed
        stages[POW_USR].buffer = EMPTY; //empty consumed buffer
        status = pthread_mutex_unlock(&stages[POW_USR].mutex); //POW_USR data consumed unlock mutex
        if(status != 0)
            printf("status = %d, main thread mutex unlock failed in consumer problem\n",status);
        status = pthread_cond_signal(&stages[POW_USR].cond); // POW_USR data consumed signal cond
        if(status != 0)
            printf("status = %d, main thread condition siganal failed in consumer problem\n",status);

        //main thread producer problem
        status = pthread_mutex_lock(&stages[USR_DBL].mutex);
        if(status != 0)
            printf("status = %d, main thread mutex lock failed in producer problem\n",status);
        while(stages[USR_DBL].buffer != EMPTY)
        {
            status = pthread_cond_wait(&stages[USR_DBL].cond, &stages[USR_DBL].mutex); // wait for USR_DBL to become EMPTY
            if(status != 0)
                printf("status = %d, main thread cond wait failed in producer problem\n",status);
        }
        // USR_DBL EMPTY and/or has been signalled operate on USR_DBL crit region
        stages[USR_DBL].buffer = userInput; // USR_DBL data produced
        status = pthread_mutex_unlock(&stages[USR_DBL].mutex); //USR_DBL data produced unlock mutex
        if(status != 0)
            printf("status = %d, main thread mutex unlock failed in producer problem\n",status);
        status = pthread_cond_signal(&stages[USR_DBL].cond); // USR_DBL data produced signal cond
        if(status != 0)
            printf("status = %d, main thread condition siganal failed in producer problem\n",status);
    }
    return;
}
__________________
Mike
Reply With Quote
  #2 (permalink)  
Old January 24th, 2008, 12:25 AM
Registered User
 
Join Date: Jan 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to benjamin.wang
Default

You should use the option of -lpthread when compiling.

My skype id is benjamin.wang.bj
Reply With Quote









Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.