pthread_equal() 替代 powerpc 交叉编译器?
pthread_equal() alternative for powerpc cross compiler?
我正在开发一个读取车辆上 CAN 数据流的程序。该程序将在带有嵌入式 linux 的 STW TC3g 上使用。
我需要使用基于 powerpc 的交叉编译器来编译我的代码。
对于多线程,我写了一个小示例程序来测试一些功能。
代码如下:
pthread_t tid[2];
void* thread_pollCan();
int main() {
while(1) {
pthread_create(&tid[0], NULL, &thread_pollCan, NULL);
pthread_create(&tid[1], NULL, &thread_pollCan, NULL);
printf("main:\tpid0:%ld\n", tid[0]);
printf("main:\tpid1:%ld\n", tid[1]);
pthread_join(tid[0],NULL);
pthread_join(tid[1],NULL);
puts("main:\tThreads terminated");
sleep(3);
puts("main:\tLoop end");
}
return 0;
};
void* thread_pollCan(void* arg) {
int i;
// Should thread tid[0] do...
if(pthread_equal(pthread_self(),tid[0])) {
printf("tid[0]: %ld\t thread_self: %ld\n",tid[0],pthread_self());
for(i=0;i<=10;i++) {
printf("In loop %d\n",i);
}
}
// Should thread tid[1] do...
if(pthread_equal(pthread_self(),tid[1])) {
printf("tid[1]: %ld\t thread_self: %ld\n",tid[1],pthread_self());
for(i=0;i<=10;i++) {
printf("In loop %d\n",i);
}
}
pthread_exit((void *)pthread_self());
}
如果我用 gcc 编译这个程序,它在我开发的 ubuntu 电脑上运行良好:
gcc -Wall -pthread Multithreading.c -o Multithreading
程序输出:
main: pid0:140207930042112
main: pid1:140207921649408
tid[0]: 140207930042112 thread_self: 140207930042112
In loop 0
In loop 1
In loop 2
In loop 3
In loop 4
In loop 5
In loop 6
In loop 7
In loop 8
In loop 9
In loop 10
tid[1]: 140207921649408 thread_self: 140207921649408
In loop 0
In loop 1
In loop 2
In loop 3
In loop 4
In loop 5
In loop 6
In loop 7
In loop 8
In loop 9
In loop 10
main: Threads terminated
main: Loop end
但是如果我用以下命令为我的车载计算机编译它:
powerpc-stw-linux-uclibc-gcc -O2 -Wall -pthread Multithreading.c -o Multithreading -lm
它的程序输出是这样的:
main: pid0:1026
main: pid1:2051
main: Threads terminated
main: Loop end
问题是,pthread_equal()
函数与写在 tid[]
数组中的线程 ID 永远不匹配。
所以我的问题是,是否有任何其他方法可以找到正确的线程ID。
您似乎正在尝试阅读 tid[]
但没有时间更新。您应该添加一些 mutex
函数调用来同步所有:
// mutex for synchronization
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
// pthread ids
pthread_t tid[2];
// thread function
void* thread_pollCan();
int main() {
while(1) {
// lock the mutex: thread will wait for it to be unlocked
pthread_mutex_lock(&mutex);
pthread_create(&tid[0], NULL, &thread_pollCan, NULL);
pthread_create(&tid[1], NULL, &thread_pollCan, NULL);
printf("main:\tpid0:%ld\n", tid[0]);
printf("main:\tpid1:%ld\n", tid[1]);
// unlock the mutex: threads can run
pthread_mutex_unlock(&mutex);
pthread_join(tid[0],NULL);
pthread_join(tid[1],NULL);
puts("main:\tThreads terminated");
sleep(3);
puts("main:\tLoop end");
}
return 0;
};
void* thread_pollCan(void* arg) {
// wait for mutex to be unlocked before working
pthread_mutex_lock(&mutex);
// immediatly release the mutex
pthread_mutex_unlock(&mutex);
// do stuff
if (thread_equal(tid[0], pthread_self())
{ /*...*/ }
}
我正在开发一个读取车辆上 CAN 数据流的程序。该程序将在带有嵌入式 linux 的 STW TC3g 上使用。
我需要使用基于 powerpc 的交叉编译器来编译我的代码。 对于多线程,我写了一个小示例程序来测试一些功能。
代码如下:
pthread_t tid[2];
void* thread_pollCan();
int main() {
while(1) {
pthread_create(&tid[0], NULL, &thread_pollCan, NULL);
pthread_create(&tid[1], NULL, &thread_pollCan, NULL);
printf("main:\tpid0:%ld\n", tid[0]);
printf("main:\tpid1:%ld\n", tid[1]);
pthread_join(tid[0],NULL);
pthread_join(tid[1],NULL);
puts("main:\tThreads terminated");
sleep(3);
puts("main:\tLoop end");
}
return 0;
};
void* thread_pollCan(void* arg) {
int i;
// Should thread tid[0] do...
if(pthread_equal(pthread_self(),tid[0])) {
printf("tid[0]: %ld\t thread_self: %ld\n",tid[0],pthread_self());
for(i=0;i<=10;i++) {
printf("In loop %d\n",i);
}
}
// Should thread tid[1] do...
if(pthread_equal(pthread_self(),tid[1])) {
printf("tid[1]: %ld\t thread_self: %ld\n",tid[1],pthread_self());
for(i=0;i<=10;i++) {
printf("In loop %d\n",i);
}
}
pthread_exit((void *)pthread_self());
}
如果我用 gcc 编译这个程序,它在我开发的 ubuntu 电脑上运行良好:
gcc -Wall -pthread Multithreading.c -o Multithreading
程序输出:
main: pid0:140207930042112
main: pid1:140207921649408
tid[0]: 140207930042112 thread_self: 140207930042112
In loop 0
In loop 1
In loop 2
In loop 3
In loop 4
In loop 5
In loop 6
In loop 7
In loop 8
In loop 9
In loop 10
tid[1]: 140207921649408 thread_self: 140207921649408
In loop 0
In loop 1
In loop 2
In loop 3
In loop 4
In loop 5
In loop 6
In loop 7
In loop 8
In loop 9
In loop 10
main: Threads terminated
main: Loop end
但是如果我用以下命令为我的车载计算机编译它:
powerpc-stw-linux-uclibc-gcc -O2 -Wall -pthread Multithreading.c -o Multithreading -lm
它的程序输出是这样的:
main: pid0:1026
main: pid1:2051
main: Threads terminated
main: Loop end
问题是,pthread_equal()
函数与写在 tid[]
数组中的线程 ID 永远不匹配。
所以我的问题是,是否有任何其他方法可以找到正确的线程ID。
您似乎正在尝试阅读 tid[]
但没有时间更新。您应该添加一些 mutex
函数调用来同步所有:
// mutex for synchronization
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
// pthread ids
pthread_t tid[2];
// thread function
void* thread_pollCan();
int main() {
while(1) {
// lock the mutex: thread will wait for it to be unlocked
pthread_mutex_lock(&mutex);
pthread_create(&tid[0], NULL, &thread_pollCan, NULL);
pthread_create(&tid[1], NULL, &thread_pollCan, NULL);
printf("main:\tpid0:%ld\n", tid[0]);
printf("main:\tpid1:%ld\n", tid[1]);
// unlock the mutex: threads can run
pthread_mutex_unlock(&mutex);
pthread_join(tid[0],NULL);
pthread_join(tid[1],NULL);
puts("main:\tThreads terminated");
sleep(3);
puts("main:\tLoop end");
}
return 0;
};
void* thread_pollCan(void* arg) {
// wait for mutex to be unlocked before working
pthread_mutex_lock(&mutex);
// immediatly release the mutex
pthread_mutex_unlock(&mutex);
// do stuff
if (thread_equal(tid[0], pthread_self())
{ /*...*/ }
}