2016-10-07 55 views
1

我正在研究一個讀取車輛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。

+0

你能在'thread_pollCan()'的開頭顯示線程ID('pthread_self()')和'tid'陣列? – purplepsycho

+0

謝謝。 '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

回答

1

似乎你想讀取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()) 
    { /*...*/ } 
} 
+0

不確定你所指的是什麼「數據」。循環中有'pthread_join()'調用。所以,只有兩個線程同時運行(除了main)並且它們各自記錄單獨的ID。 – usr

+0

通過數據,我的意思是'tid [...]':我不知道'pthread_create'是否應該在'tid'中存儲新的線程ID後運行創建的線程。 – purplepsycho

+0

謝謝您的好,那完美的工作! 我只需要在'thread_pollCan()'中做一點修正: 'pthread_mutex_lock(&mutex); pthread_mutex_unlock(&mutex); //做東西...' – Payoff