Linux下使用POSIX Thread作多核多線程並行計算

來源:互聯網
上載者:User

POSIX線程庫根據處理器、作業系統等特性封裝了一台線程處理的介面。對於目前基於x86處理器架構的Linux系統來說,它往往會預設地將新建立的一個線程調度到與主線程不同的核中執行,如果這樣能更好地平衡負荷的話。因此,在使用POSIX線程時,開發人員不能斷言當前建立的線程是否運行在與主線程相同的核心下,也不能斷言一定運行在與主線程不同的核心下。當然,由於POSIX線程具有時間片輪詢調度(Round-Robin),因此即便與主線程處於一個核心,使用旋鎖的話,效能影響也不會太大。不過多核多線程其實最好還是使用類似於OS X以及iOS中的Grand Central Dispatch機制,顯式給出線程的調度隊列。

在Linux下使用POSIX線程時應當先得加上libpthread.so動態庫,因此在連接器選項中加上-lpthread。

以下代碼在聯想Z475,AMD APU A6-3420M,4GB DDR3,Ubuntu12.04系統下完成測試。 

C代碼:

/* ============================================================================ Name        : ThreadTest.c Author      : Zenny Chen Version     : Copyright   : Your copyright notice Description : Hello World in C, Ansi-style ============================================================================ */#include <stdio.h>#include <stdlib.h>#include <pthread.h>#define TOTAL_CALC_ITEMS    1000000extern unsigned long getRealtimeCycles(void);static volatile _Bool isFinished = 0;static volatile int sum4Core2 = 0;static void* threadProc(void *param){    int sum = 0;    int *pSrc = (int*)param;    for(int i = TOTAL_CALC_ITEMS / 2; i < TOTAL_CALC_ITEMS; i++)        sum += pSrc[i];    sum4Core2 = sum;    isFinished = 1;    return NULL;}static int sourceArray[TOTAL_CALC_ITEMS];static void testOpt(void){    // Initialize the array    for(int i = 0; i < TOTAL_CALC_ITEMS; i++)        sourceArray[i] = i + 1;    pthread_t threadHandle;    pthread_attr_t attr;    if(pthread_attr_init(&attr) != 0)    {        puts("Attribute failed to create!");        return;    }    pthread_attr_setschedpolicy(&attr, SCHED_OTHER);    if(pthread_create(&threadHandle, &attr, &threadProc, sourceArray) != 0)    {        puts("Thread failed to create!");        return;    }    int polacy = -1;    pthread_attr_getschedpolicy(&attr, &polacy);    printf("Current sched polacy is: %d\n", polacy);    int sum = 0;    unsigned long ticks = getRealtimeCycles();    for(int i = 0; i < TOTAL_CALC_ITEMS / 2; i++)        sum += sourceArray[i];    while(!isFinished)        __asm__("pause");    sum += sum4Core2;    ticks = getRealtimeCycles() - ticks;    printf("The number of cycles is: %lu\n", ticks);    printf("The sum is: %d\n", sum);    pthread_attr_destroy(&attr);}static void testNaive(void){    // Initialize the array    for(int i = 0; i < TOTAL_CALC_ITEMS; i++)        sourceArray[i] = i + 1;    int sum = 0;    unsigned long ticks = getRealtimeCycles();    for(int i = 0; i < TOTAL_CALC_ITEMS; i++)        sum += sourceArray[i];    ticks = getRealtimeCycles() - ticks;    printf("The number of cycles is: %lu\n", ticks);    printf("The sum is: %d\n", sum);}int main(void){    testOpt();    testNaive();    testOpt();    testNaive();    return EXIT_SUCCESS;    // 1784293664}

 

彙編代碼:

.text.align 2.globl getRealtimeCyclesgetRealtimeCycles:    rdtsc    shl     $32, %rdx    or      %rdx, %rax    ret

 


 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.