2013-08-08 5 views
2

알아낼 수없는 오류가 있습니다. 참조가 정의되지 않은 이유를 이해할 수 없습니다. pthread_attr_t는 먼저 a1로 초기화되고, 매개 변수에 따라 필자는 이것이 맞아야한다고 생각했습니다. 여기 내 코드와 오류가 있습니다. 이 혼란을 해결하는 데 도움을 주셔서 감사합니다. 당신이 제공 할 수있는 도움을pthread에서 정의되지 않은 참조

#include <iostream> 
#include "buffer.h" 
#include <pthread.h> 
#include <semaphore.h> 

using namespace std; 

sem_t sem_mutex; 

/* create the semaphore */ 
//sem_init(&sem_mutex, 0, 1); 
/* acquire the semaphore */ 
//sem_wait(&sem_mutex); 

/*** critical section ***/ 

/* release the semaphore */ 
//sem_post(&mutex); 

int insert(buffer_item item); 
int remove(buffer_item *item); 
//void *thread_entry(void *param); 
//create_mutex_lock(&mutex, NULL); 
//pthread_mutex_lock(&mutex); 
//release_mutex_lock(&mutex); 


int insert(buffer_item item) 
{ 
    bool added=false; 
    /* insert an item into buffer */ 
    item=item+1; 

    cout<<"The producer added "<<item<<endl; 


    if (added==true)// return 0 if successful, otherwise 
     return 0; //return -1 indicating an error condition */ 
    else 
     return -1; 
} 

int remove(buffer_item *item) 
{ 
    bool removed=false; 
    item=item-1;/* remove an object from buffer and placing it in item*/ 

    cout<<"the consumer removed "<< item<<endl; 

    if(removed==true) 
     return 0;// return 0 if successful 
    else 
     return -1;//otherwise return -1 indicating an error condition 
} 
void *thread_entry(void *param) 
{ /* the entry point of a new thread */ 
    pthread_t new_entry; 

} 

/* create the mutex lock */ 
//create_mutex_lock(&mutex, NULL); 

/* acquire the mutex lock */ 
//pthread_mutex_lock(&mutex); 

/*** critical section ***/ 

/* release the mutex lock */ 
//release_mutex_lock(&mutex); 


int main() 
{ 
    /* 1. Answer the three command lines argument */ 
    pthread_t t1; 
    pthread_attr_t a1; 
    buffer_item buffer=0;/* 2. Initialize buffer, mutex, semaphores, and other global vars */ 
    buffer_item mutex=0; 
    buffer_item semaphore=0; 

    insert(buffer);/* 3. Create producer thread(s) */ 


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

    /* create a new thread */ 
    pthread_create(&t1, &a1, thread_entry, NULL); 
//remove(*buffer);/* 4. Create consumer thread(s) */ 
     /* 5. Sleep */ 
     /* 6. Destroy mutex and semaphores */ 
    return 0; /* 7. Exit */ 
} 

오류

[Linker error] undefined reference to `_imp__pthread_attr_init' 
    [Linker error] undefined reference to `_imp__pthread_create' 
    ld returned 1 exit status 

감사합니다!

+3

당신이'-pthread' – aaronman

+2

은 또한 당신이'-lpthread' 또는 사용하고있는 컴파일러로 컴파일 않았다 당신은 여전히 ​​pthread와 함께 연결해야 명심? –

+0

[Pthreads 및 정의되지 않은 참조] 가능한 중복 (http://stackoverflow.com/questions/6608379/pthreads-and-undefined-reference) – alk

답변

1

아마도 오류는 -lpthread 또는 -pthread으로 컴파일하지 않았기 때문입니다.

그러나 C++ (11)는 원시 스레드

C++ (11)는 동시성 기능의 모두를 가지고 있기 때문에 그것은 중요하지 않습니다 실제의 pthreads보다 사용하기 훨씬 좋네요 있습니다 here

+0

글쎄, 당신은 여전히 ​​pthread – billz

+0

@billz하지만 C++ 11 개의 스레드는 사람들과 훨씬 더 좋게 작동하며 ** ** ** 더 많은 기능을 가지고 있습니다. 그 (것)들을 사용하지 않을 이유가 있습니까? – aaronman

+0

사용 방법을 말하면 다음과 같습니다. :) – billz