免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1599 | 回复: 4
打印 上一主题 下一主题

怎么创建不了多线程呢? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-11-03 11:18 |只看该作者 |倒序浏览
如下一个程序,编译时报错。
/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 */

论坛徽章:
0
2 [报告]
发表于 2006-11-03 11:32 |只看该作者
Linux
-lpthread
FreeBSD
-pthread

论坛徽章:
0
3 [报告]
发表于 2006-11-03 11:42 |只看该作者

回复 2楼 mingyanguo 的帖子

什么意思呢?刚学不久,请不吝赐教

论坛徽章:
0
4 [报告]
发表于 2006-11-03 11:45 |只看该作者

回复 2楼 mingyanguo 的帖子

谢谢,是编译参数啊

论坛徽章:
0
5 [报告]
发表于 2006-11-03 14:17 |只看该作者

回复 2楼 mingyanguo 的帖子

正解。
也就是说你编译的时候要那么做。
linux下 gcc -lpthread -o test.o test.c
BSD下也类似。
之所以需要这样是因为thread不是系统的默认库
-l,是确定链接时需要的其他库函数“LIBRARY”,也就是编译时系统加入pthread这个库
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP