这是我的线程子例程...... 在这里,我创建了4个线程并将结构作为参数传递给线程子例程。
我正在尝试使用getid()
函数打印线程ID,
我收到错误说"对gettid()"未定义的引用。
我添加了必要的头文件......
#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;
}
答案 0 :(得分:24)
尝试
#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
答案 1 :(得分:1)
我已按照错误提供的建议并更正了源 * 下面是无错代码*
#include <pthread.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/syscall.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 = syscall(SYS_gettid); // here is the correct 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;
}
答案 2 :(得分:1)
要粘贴的宏(相对于先前的答案有所改进):
#include <unistd.h>
#include <sys/syscall.h>
#ifndef SYS_gettid
#error "SYS_gettid unavailable on this system"
#endif
#define gettid() ((pid_t)syscall(SYS_gettid))
示例:
#include <unistd.h>
#include <sys/syscall.h>
#ifndef SYS_gettid
#error "SYS_gettid unavailable on this system"
#endif
#define gettid() ((pid_t)syscall(SYS_gettid))
#include <stdio.h>
void main()
{
printf("tid is %d\n", gettid());
}
答案 3 :(得分:0)
如果您使用 glibc 或 musl,请定义 _GNU_SOURCE
以让 unistd.h 定义此符号。