c程序中出现错误“对gettid的未定义引用" [英] getting error in c program "undefined reference to gettid"
本文介绍了c程序中出现错误“对gettid的未定义引用"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的线程子例程...在这里,我创建了 4 个线程并将结构作为参数传递给线程子例程.
This is my thread sub routine... Here, I am creating 4 threads and passing structure as a argument to thread sub routine.
我正在尝试使用 getid()
函数打印线程 ID,
I am trying to print thread id with getid()
function,
我收到错误消息,说未定义对 gettid() 的引用".
我已经添加了必要的头文件...
I have added necessary header files...
#include <pthread.h>
#include <stdio.h>
#include <sys/types.h>
#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 : %d\n",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 <unistd.h>
#include <sys/syscall.h>
#ifdef SYS_gettid
pid_t tid = syscall(SYS_gettid);
#else
#error "SYS_gettid unavailable on this system"
#endif
这篇关于c程序中出现错误“对gettid的未定义引用"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文