Need help with pthread

I can't run the following program which uses pthreads. When I compile it using gcc, I get errors which I copied at the bottom of this program.

#include<stdio.h>
#include<pthread.h>
pthread_mutex_t region_mutex = PTHREAD_MUTEX_INITIALIZER;
int b; /*buffer size = 1; */

main()
{
pthread_t producer_thread, consumer_thread;
void *producer(), *consumer();
void *consumer();
pthread_create(&consumer_thread, NULL, consumer, NULL);
pthread_create(&producer_thread, NULL, producer, NULL);
pthread_join(consumer_thread, NULL);

}

void add_buffer(int i)
{
b=i;
}

int get_buffer(int i){
b=i'
}
int get_buffer()
{
return b;
}

void *producer(){
int i=0;
while(1)
{
pthread_mutex_lock(&region_mutex);
put_buffer(i);
pthread_mutex_unlock(&region_mutex);
i++;
}
}

void *consumer()
{
int i, v;
for(i=0;i<100;i++)
{
pthread_mutex_lock(&region_mutex);
v=get_buffer();
pthread_mutex_unlock(&region_mutex);
printf("got %d ",v");
}
}

-----------------------------------------------------------------------------
Errors I get:
-------------

When I compile this program, I get the following errors:

In function `main':
MutexExample.c:(.text+0x1f): undefined reference to `pthread_create'
MutexExample.c:(.text+0x3a): undefined reference to `pthread_create'
MutexExample.c:(.text+0x4b): undefined reference to `pthread_join'
/tmp/cc8pA8kt.o: In function `producer':
MutexExample.c:(.text+0x93): undefined reference to `put_buffer'
collect2: ld returned 1 exit status
Add -lpthread to your link command
U mean something like:

cc -lpthread programName.c
Topic archived. No new replies allowed.