我正在研究一個讀取車輛CAN數據流的程序。該程序將用於嵌入式Linux的STW TC3g。pthread_equal()替代powerpc交叉編譯器?
我需要用基於powerpc的交叉編譯器編譯我的代碼。 對於多線程,我寫了一個小例子程序來測試一些函數。
下面是代碼:
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
但
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());
}
這個程序運行在我開發的Ubuntu PC的罰款,如果我用gcc編譯如果我爲我的車載電腦編譯它,請使用以下內容:
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()
函數從不匹配線程ID的tid[]
-array寫道。
所以我的問題是,如果有任何其他方法來找到正確的線程ID。
你能在'thread_pollCan()'的開頭顯示線程ID('pthread_self()')和'tid'陣列? – purplepsycho
謝謝。 'pthread_create()'的返回值與兩個編譯器都是'0'。 所以這不是問題,但是當我顯示'tid'和'pthread_self()'它變得有趣。 使用標準的gcc編譯器,我得到正確的tid和pthread_self()返回值。 使用交叉編譯器我得到這個: 'tid [0]:0/thread_self:1026 tid [1]:0/thread_self:1026 tid [0]:1026/thread_self:2051 tid [1]:0/thread_self:2051' 似乎當前的線程ID不會保存在'tid []' – Payoff