LinuxのCのマルチスレッドの学習

 
 
/ * ************************************************ ************************ 
    >ファイル名:Test1.cに
    >著者:
    >メール:
    >作成日時:2019年06月28日星期五21时12分51秒
 ********************************************** ************************* * / 

書式#include <stdio.hに> 
する#include <stdlib.h>に含ま
する#include <pthread.hの> 無効 * myfunc関数(無効 * 引数){
     int型私は、
    文字 *名=(CHAR * )引数。
    (i = 1 ; iは< 50 ; iは++ ){ 
        のprintf(

" %sの:%dは\ nを" 、名前、I); 
    } 

    戻りNULL。
} 


int型のmain(){ 
    がpthread_t TH1。
    がpthread_tのTH2; 

    pthread_create(&TH1、NULL、myfuncが、" TH1 " )。
    pthread_create(&TH2、NULL、myfuncが、" TH2 " )。
    pthread_joinを(TH1、NULL); 
    pthread_joinを(TH2、NULL); 

    リターン 0 ; 
}
 
 

 

/ * ************************************************ ************************ 
    >ファイル名:eg4.c 
    >著者:
    >メール:
    >作成日時:2019年06月29日星期六10时56分11秒
 ********************************************** ************************* * / 

書式#include <stdio.hに> 
する#include <stdlib.h>に含ま
する#include <pthread.hの> 

pthread_mutex_t ロック;
int型 S = 0 ;
ボイド * myfunc関数(ボイド * 引数){
     int型 I = 0 (i = 0 ; iは< 1000000を; 私は++ ){ 
        pthread_mutex_lockの(ロック)。
        S ++ ; 
        pthread_mutex_unlockの(ロック)。
    } 
    戻りNULL。
} 
int型のmain(){ 
    がpthread_t TH1。
    がpthread_tのTH2; 
    pthread_mutex_initの(ロック、NULL); 

    pthread_create( TH1、NULL、myfunc関数、NULL); 
    pthread_create( TH2、NULL、myfunc関数、NULL); 

    pthread_joinを(TH1、NULL); 
    pthread_joinを(TH2、NULL); 
    printf(" S =%d個の\ nを"、S); 

}

マルチスレッドロック

おすすめ

転載: www.cnblogs.com/xjyxp/p/11106085.html