線程

Posix線程

#include <pthread.h>
    int pthread_create(&tid, &attr, func, args);      //線程ID, 屬性(優先級,守護線程), 函數,參數
            (void *)(* func)(void *)  // void *可以傳任何參數,返回任何值
            0: 成功   錯誤時不設置errno, 返回正值Exxx, EAGAIN。。。
    int pthread_join(tid, &pState)  
           // 等待線程tid結束,並將狀態返回(二級指針)。0:成功,同上
           // 不像進程可以等待任意子進程結束
    pthread_t pthread_self()        
          // 線程tid爲無符號整數
    int pthread_detach(tid)   
         //可匯合的線程,結束時保留狀態;分離的線程,結束時資源全部釋放
         //被想脫離自己的線程調用。  
    void pthread_exit(&status)
        //線程終止


 互斥鎖:

    阻止對共享變量的同時訪問   // 545/873     pthread_mutex_t (mutual exclusion)     

    pthread_mutex_t  lock = PTHREAD_MUTEX_INITIALIZER 
    pthread_mutex_init        //用共享內存分配的鎖,用此函數初始化  
    pthread_mutex_lock(&lock);
    local_var = g_counter;    // A :  兩個線程都操作這3行,某個線程在A或者B點,切換到另一個線程去一直執行,
     printf("%d: %d", tid, local_var + 1);  // B :  再切換回來,A的local_var還是原來的值,加1後覆蓋g_counter,出現錯誤!
    g_counter = local_var + 1;
    pthread_mutex_unlock(&lock)   

條件變量:

    睡眠等待某種條件出現    //  549/873   pthread_cond_t 

   int pthread_cond_wait(*condi, *mutex)  // A線程在等待條件M
   int pthread_cond_signal(*condi)        // B線程通知一個等待條件M的線程   
   int pthread_cond_timedwait(條件,鎖,絕對時間)
   int pthread_cond_broadcast(*condi)     //  通知所有


線程與進程的區別 

進程fork的問題:
    1. 昂貴。雖然用了寫時拷貝技術,但內存映像,描述字要拷貝到子進程。
    2. 通信。父子進程通信不方便,雖然有IPC通信。
線程:
    1. 創建一般快10-100倍
    2. 進程內的線程共享資源(進程指令,打開的文件,信號處理,工作目錄,用戶ID和組ID),除了ID,寄存器,棧,信號掩碼,優先級,errno
    3. 引入了同步問題。 func_r 爲線程安全的函數,即無全局或者靜態變量(線程共享不安全)。
    4. 線程函數的參數爲 void *, 將int 轉成它,只有在整數大小 <= 指針大小 的系統上該強轉才工作。
   若 &connId, 則因爲線程間共享,所以不安全。
   若 pConnId, 每個線程新分配一個,但malloc , free , 是不可重入的函數(有靜態變量)。
區別: 總之:線程共享,進程複製
     在用子進程或者線程處理服務器監聽收到客戶端請求,即accept後的處理
              父進程需要關閉accept的連接;線程不能關(共享,關了後,生成的線程無法處理)
              生成線程中處理完後關閉客戶端的連接;子進程不必關(子進程結束後,其連接自動關了)

 

 

例子:

pthread_cond_wait(&products->notFull, &products->locker);    // 等

pthread_cond_signal(&products->notFull);                                   // 通知

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>

#define BUFFER_SIZE 8
#define END_FLAG (-1)

struct Products
{
int buffer[BUFFER_SIZE];
pthread_mutex_t locker;           //保證存取操作的原子性 互斥性
pthread_cond_t notEmpty;          //是否可讀       
pthread_cond_t notFull;           //是否可寫
int posReadFrom;
int posWriteTo;
};

int BufferIsFull(struct Products* products)
{
if ((products->posWriteTo + 1) % BUFFER_SIZE == products->posReadFrom)
{
return (1);
}
return (0);
}


int BufferIsEmpty(struct Products* products)
{
if (products->posWriteTo == products->posReadFrom)
{
return (1);
}
return (0);
}

//製造產品。
void Produce(struct Products* products, int item)
{
pthread_mutex_lock(&products->locker); 
 //無空間可寫入
while (BufferIsFull(products))
{
	pthread_cond_wait(&products->notFull, &products->locker);
} 
//寫入數據
products->buffer[products->posWriteTo] = item;
products->posWriteTo++;
if (products->posWriteTo >= BUFFER_SIZE)
	products->posWriteTo = 0;
// 通知不空,可讀了
pthread_cond_signal(&products->notEmpty);     
//解鎖
pthread_mutex_unlock(&products->locker);      
}


int Consume(struct Products* products)
{
int item;

pthread_mutex_lock(&products->locker);

while (BufferIsEmpty(products))
{
	pthread_cond_wait(&products->notEmpty, &products->locker);
} //爲空時持續等待,無數據可讀
//提取數據
item = products->buffer[products->posReadFrom];
products->posReadFrom++;
if (products->posReadFrom >= BUFFER_SIZE) //如果到末尾,從頭讀取
	products->posReadFrom = 0;
pthread_cond_signal(&products->notFull); 

pthread_mutex_unlock(&products->locker);

return item;
}


struct Products products;

void* ProducerThread(void* data)
{
int i;
for (i = 0; i < 16; ++i)
{
printf("producer: %d\n", i);
Produce(&products, i);
}
Produce(&products, END_FLAG);
return NULL;
}


void* ConsumerThread(void* data)
{
int item;

while (1)
{
item = Consume(&products);
if (END_FLAG == item)
       break;
printf("consumer: %d\n", item);
}
return (NULL);
}


int main(int argc, char* argv[])
{
pthread_t producer;
pthread_t consumer;
int result;

pthread_create(&producer, NULL, &ProducerThread, NULL);
pthread_create(&consumer, NULL, &ConsumerThread, NULL);

pthread_join(producer, (void *)&result);
pthread_join(consumer, (void *)&result);

exit(EXIT_SUCCESS);
}


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章