Reputation: 21
I have this error that I am unable to figure out. I don't understand why the references are undefined. the pthread_attr_t is initialized earlier as a1, and according to the parameters, I thought it should be right. Here is my code and the error. Thanks for any help in solving this mess.
#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 */
}
error
[Linker error] undefined reference to `_imp__pthread_attr_init'
[Linker error] undefined reference to `_imp__pthread_create'
ld returned 1 exit status
Thanks for any help you could provide!
Upvotes: 2
Views: 2648
Reputation: 18761
The error you probably made is that you didn't compile with -lpthread
or -pthread
.
But it doesn't matter because C++11 has native threads
C++11 has a whole bunch of concurrency features here which are actually much nicer to use than pthreads
keep in mind that you still have to link with pthread
Upvotes: 1