c/c++语言开发共享在c程序中获取错误“对gettid的未定义引用”

这是我的线程子例程……在这里,我创建了4个线程并将结构作为参数传递给线程子例程。

我试图用getid()函数打印线程ID,

我收到错误说“未定义的gettid()参考”。

我添加了必要的头文件……

 #include  #include  #include  #define ARRAYSIZE 17 #define NUMTHREADS 4 struct ThreadData { int start, stop; int* array; }; void* squarer(void* td) { struct ThreadData* data=(struct ThreadData*) td; int start=data->start; int stop=data->stop; int* array=data->array; int i; pid_t tid1; tid1 = gettid(); //error at this statement//` printf("tid : %dn",tid1); for (i=start; i<stop; i++) { sleep(1); array[i]=i*i; printf("arr[%d] = [%d]n",i,array[i]); } return NULL; } int main(void) { int array[ARRAYSIZE]; pthread_t thread[NUMTHREADS]; struct ThreadData data[NUMTHREADS]; int i; int tasksPerThread=(ARRAYSIZE+NUMTHREADS-1)/NUMTHREADS; for (i=0; i<NUMTHREADS; i++) { data[i].start=i*tasksPerThread; data[i].stop=(i+1)*tasksPerThread; data[i].array=array; } data[NUMTHREADS-1].stop=ARRAYSIZE; for (i=0; i<NUMTHREADS; i++) { pthread_create(&thread[i], NULL, squarer, &data[i]); } for (i=0; i<NUMTHREADS; i++) { pthread_join(thread[i], NULL); } for (i=0; i<ARRAYSIZE; i++) { printf("%d ", array[i]); } printf("n"); return 0; } 

    尝试

     #include  #include  #ifdef SYS_gettid pid_t tid = syscall(SYS_gettid); #else #error "SYS_gettid unavailable on this system" #endif 

    我已经按照errro提供的建议并更正了 以下 的源 * 是无错误代码*

    需要了解更多c/c++开发分享在c程序中获取错误“对gettid的未定义引用”,也可以关注C/ C++技术分享栏目—计算机技术网(www.ctvol.com)!

     #include  #include  #include  #include  #include  #define ARRAYSIZE 17 #define NUMTHREADS 4 struct ThreadData { int start, stop; int* array; }; void* squarer(void* td) { struct ThreadData* data=(struct ThreadData*) td; int start=data->start; int stop=data->stop; int* array=data->array; int i; pid_t tid1; tid1 = syscall(SYS_gettid); // here is the correct statement // printf("tid : %dn",tid1); for (i=start; i 

      以上就是c/c++开发分享在c程序中获取错误“对gettid的未定义引用”相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/c-cdevelopment/979160.html

      (0)
      上一篇 2021年12月12日
      下一篇 2021年12月12日

      精彩推荐