Linux 多線程編程(Pthread 庫)學習筆記 二

來源:互聯網
上載者:User

 

線程資料處理 之 線程資料

 

為了保護變數,我們必須使用訊號量、互斥等方法來保證我們對變數的正確使用。

1) 線程資料
在單線程的程式裡,有兩種基本的資料:全域變數和局部變數。但在多線程程式裡,還有第三種資料類型:線程資料(TSD: Thread-Specific Data)。它和全域變數很象,線上程內部,各個函數可以象使用全域變數一樣調用它,但它對線程外部的其它線程是不可見的.

線程儲存的具體用法

建立一個類型為 pthread_key_t 類型的變數。

           調用 pthread_key_create() 來建立該變數。該函數有兩個參數,第一個參數就是上面聲明的 pthread_key_t 變數,第二個參數是一個清理函數,用來線上程釋放該線程儲存的時候被調用。該函數指標可以設成 NULL ,這樣系統將調用預設的清理函數。

 

當線程中需要儲存特殊值的時候,可以調用 pthread_setspcific() 。該函數有兩個參數,第一個為前面聲明的 pthread_key_t 變數,第二個為 void* 變數,這樣你可以儲存任何類型的值。

如果需要取出所儲存的值,調用 pthread_getspecific() 。該函數的參數為前面提到的 pthread_key_t 變數,該函數返回 void * 類型的值。

 

下面是前面提到的函數的原型:

int pthread_setspecific(pthread_key_t key, const void *value);

void *pthread_getspecific(pthread_key_t key);

int pthread_key_create(pthread_key_t *key, void (*destructor)(void*));

 

 

下面是一個如何使用線程儲存的例子:

#include <malloc.h>

#include <pthread.h>

#include <stdio.h>

 

/* The key used to associate a log file pointer with each thread. */

static pthread_key_t thread_log_key;

 

/* Write MESSAGE to the log file for the current thread. */

void write_to_thread_log (const char* message)

{

         FILE* thread_log = (FILE*) pthread_getspecific (thread_log_key);

         fprintf (thread_log, “%s\n”, message);

}

 

/* Close the log file pointer THREAD_LOG. */

void close_thread_log (void* thread_log)

{

          fclose ((FILE*) thread_log);

}

 

void* thread_function (void* args)

{

        char thread_log_filename[20];

        FILE* thread_log;

 

        /* Generate the filename for this thread’s log file. */

        sprintf (thread_log_filename, “thread%d.log”, (int) pthread_self ());

 

        /* Open the log file. */

        thread_log = fopen (thread_log_filename, “w”);

 

       /* Store the file pointer in thread-specific data under thread_log_key. */

       pthread_setspecific (thread_log_key, thread_log);

 

       write_to_thread_log (“Thread starting.”);

         /* Do work here... */

         return NULL;

}

 

int main ()

{

        int i;

        pthread_t threads[5];

 

        /* Create a key to associate thread log file pointers in

       thread-specific data. Use close_thread_log to clean up the file pointers. */

        pthread_key_create (&thread_log_key, close_thread_log);

 

        /* Create threads to do the work. */

        for (i = 0; i < 5; ++i)

        {

                  pthread_create (&(threads[i]), NULL, thread_function, NULL);

        }

 

         /* Wait for all threads to finish. */

        for (i = 0; i < 5; ++i)

        {

                   pthread_join (threads[i], NULL);

        }

 

         return 0;

}  

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.