- 论坛徽章:
- 0
|
如下一个程序,编译时报错。
/tmp/ccCDvJO9.o(.text+0xaa): In function `main':
: undefined reference to `pthread_create'
/tmp/ccCDvJO9.o(.text+0xe9): In function `main':
: undefined reference to `pthread_join'
collect2: ld returned 1 exit status
请熟悉多线程的哥们帮忙给看一下,谢谢!
下面是程序:
#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <signal.h>
#define MAXNTHREADS 100
int nloop;
struct {
pthread_mutex_t mutex;
long counter;
} shared = { PTHREAD_MUTEX_INITIALIZER };
void *incr(void *);
int
set_concurrency(int level)
{
#ifdef HAVE_THR_SETCONCURRENCY_PROTO
int thr_setconcurrency(int);
return(thr_setconcurrency(level));
#else
return(0);
#endif
}
int
main(int argc, char **argv)
{
int i, nthreads;
pthread_t tid[MAXNTHREADS];
if (argc != 3)
printf("usage: threadmu <#loops> <#threads>");
nloop = atoi(argv[1]);
//nthreads = min(atoi(argv[2]), MAXNTHREADS);
nthreads = atoi(argv[2]);
/* 4lock the mutex */
pthread_mutex_lock(&shared.mutex);
/* 4create all the threads */
set_concurrency(nthreads);
for (i = 0; i < nthreads; i++) {
pthread_create(&tid[i], NULL, incr, NULL);
}
/* 4start the timer and unlock the mutex */
//Start_time();
pthread_mutex_unlock(&shared.mutex);
/* 4wait for all the threads */
for (i = 0; i < nthreads; i++) {
pthread_join(tid[i], NULL);
}
//printf("microseconds: %.0f usec\n", Stop_time());
if (shared.counter != nloop * nthreads)
printf("error: counter = %ld\n", shared.counter);
exit(0);
}
/* end main */
/* include incr */
void *
incr(void *arg)
{
int i;
for (i = 0; i < nloop; i++) {
pthread_mutex_lock(&shared.mutex);
shared.counter++;
pthread_mutex_unlock(&shared.mutex);
}
return(NULL);
}
/* end incr */ |
|