Linuxの----スレッドの作成、終了を待っています

まず、スレッドの作成と終了

簡単なスレッドを作成します。1.

#include <func.h>

void* threadFnc(void *p){
    printf("I am child thread\n");
}
int main(int argc,char* argv[])
{
    pthread_t pth;//创建一个线程ID
    int ret=pthread_create(&pth,NULL,threadFnc,NULL);//创建线程
    THREARD_ERRORCHECK(ret,"pthread_create");
    usleep(1);
    printf("I am main .\n");
    return 0;
}

结果:
ここに画像を挿入説明
代码分析:
int型のpthread_create(がpthread_t *スレッド、CONSTのpthread_attr_t * attrの、
void *型(* start_routineが)(void *型)、void *型のarg);

  • 最初の引数は、スレッドIDを指し
  • 二番目の引数は、平均糸を表す、NULLであります
  • 3番目のパラメータは、サブスレッド機能を示します
  • 第四パラメータは、子スレッド関数パラメータに渡されます。

図2に示すように、スレッドの参加を渡します

#include <func.h>

void* threadFnc(void *p){
    printf("I am child thread\n");
    printf("%s\n",(char *)p);
}
int main(int argc,char* argv[])
{
    pthread_t pth;
    char *pArr=(char *)malloc(20);
    strcpy(pArr,"helloworld");
    int ret=pthread_create(&pth,NULL,threadFnc,pArr);
    THREARD_ERRORCHECK(ret,"pthread_create");
    
    pthread_join(pth,NULL);
    printf("I am main .\n");
    return 0;
}

ここに画像を挿入説明
3.パス整数パラメータ

#include <func.h>

void* threadFnc(void *p){
    printf("I am child thread\n");
    printf("%d\n",*(int *)p);
}
int main(int argc,char* argv[])
{
    pthread_t pth;
    //char *pArr=(char *)malloc(20);
    //strcpy(pArr,"helloworld");
    int val=10;
    int ret=pthread_create(&pth,NULL,threadFnc,&val);
    THREARD_ERRORCHECK(ret,"pthread_create");
    
    pthread_join(pth,NULL);
    printf("I am main .\n");
    return 0;
}

結果:

ここに画像を挿入説明
解析コード:
int型のpthread_create(がpthread_tスレッド*、constの* ATTRのpthread_attr_tは、ある
無効*(* start_routineがで)(void *型)、void *型のArg);
最後のパラメータは、アドレス整数渡されました。スレッドハンドラでは、強力なターンの値を経由して来ています。
4。2つのスレッド
(ボイド*)を使用して、本明細書パラメータを用い音符を強く(64ビットシステムでは、8つのポインタ型である、ロングタイプは8である)タイプvalを回します

#include <func.h>

void* threadFnc(void *p){
    printf("I am child thread\n");
    
    printf("%ld\n",(long)p);
}
int main(int argc,char* argv[])
{
    pthread_t pth,pth1;
    long val=1;
    int ret=pthread_create(&pth,NULL,threadFnc,(void *)val);
    THREARD_ERRORCHECK(ret,"pthread_create");
    val=2;
    ret=pthread_create(&pth1,NULL,threadFnc,(void *)val);
    THREARD_ERRORCHECK(ret,"pthread_create");
    pthread_join(pth,NULL);
    pthread_join(pth1,NULL);

    printf("I am main .\n");
    return 0;
}

結果:
ここに画像を挿入説明
コード分析:書き込み機能した場合、実施の形態3によれば、出力は、2つの調査結果2となります。
このように利点はvoid *型とlong型が8 64ビット・システムに格納されている、ということ。受信された、またはクロスボーダー現象が発生訪問印字長いタイプに同様に強いターン。
5、メインスレッドと子スレッドは、追加の数です10000000

 #include <func.h>
    2 #define N 10000000
    3 void* threadFnc(void *p){
    4     int i=0;
    5     for(i=0;i<N;i++){
    6         *(int *)p+=1;
    7     }                                                                            
    8     printf("%d\n",*(int *)p);9 }10 int main(int argc,char* argv[])
   11 {
   12     pthread_t pth;
   13     //char *pArr=(char *)malloc(20);
   14     //strcpy(pArr,"helloworld");
   15     int val=10;
   16     int ret=pthread_create(&pth,NULL,threadFnc,&val);
   17     THREARD_ERRORCHECK(ret,"pthread_create");
   18     int i;
   19     for(i=0;i<N;i++){
   20         val+=1;
   21     }
   22     pthread_join(pth,NULL);
   23     printf("I am main .\n");
   24     return 0;
   25 }

結果:
ここに画像を挿入説明
分析:この結果は正しいです。200万同時現象に起因しているに追加することはできません。これは、同時ロックとアンロックすることで回避することができます。
スレッドを終了します6.

    1 #include <func.h>
    2 void print(){
    3     printf("I am print.\n");
    4     pthread_exit(NULL);     //退出线程                                                     
    5 }
?   6 void* threadFnc(void *p){
    7     printf("I am child thread\n");
    8     print();
    9     printf("I am after print.\n");
   10     return NULL;
   11 }
?  12 int main(int argc,char* argv[])
   13 {
   14     pthread_t pth;
   15     int ret=pthread_create(&pth,NULL,threadFnc,NULL);
   16     THREARD_ERRORCHECK(ret,"pthread_create");
   17     pthread_join(pth,NULL);
   18     printf("I am main .\n");
   19     return 0;
   20 }

結果:
ここに画像を挿入説明
コード分析:
無効pthread_exit(void *型のRETVAL);
正常復帰のため、終了スレッド。

第二に、糸待機出口

1.子スレッドにメインスレッドの戻り値(サブスレッド独自のアプリケーション空間を)取ります

  1 #include <func.h>
    2 
    3 void* threadFnc(void *p){
    4     p=malloc(20);
    5     strcpy((char *)p,"I AM GOOG!");
    6     printf("I am child thread.%s\n",(char *)p);
    7     pthread_exit(p);
    8 }
?   9 int main(int argc,char* argv[])
   10 {
   11     pthread_t pth;
   12     int ret=pthread_create(&pth,NULL,threadFnc,NULL);
   13     THREARD_ERRORCHECK(ret,"pthread_create");
   14     char *res;
   15     ret=pthread_join(pth,(void **)&res);
   16     THREARD_ERRORCHECK(ret,"pthread_join");
   17 
   18     printf("I am main .%s\n",res);                                               
   19     return 0;
   20 }

結果:
ここに画像を挿入説明
コード分析:
無効pthread_exit(void *型のRETVAL);
パラメータ戻り値がこの型がvoid *であること、ノートを埋めることができます。
メインスレッドは、戻り値pthread_joinをを受け取ります。
int型pthread_joinを(がpthread_tスレッド、無効**のretval);

  • 最初のパラメータは、子プロセスIDを表し、
  • 受信されたリターンに記憶された第2のパラメータ値
    の受信が成功した戻り値0です。

2.子スレッドの数はintを返し

 pthread_join.c                                                          ?? buffers 
    1 #include <func.h>                                                                
    2 
    3 void* threadFnc(void *p){
    4     printf("I am child thread.val=%ld\n",(long)p);
    5     pthread_exit((void *)2);
    6 
    7 }
    8 int main(int argc,char* argv[])
    9 {
   10     pthread_t pth;
   11     long val=1;
   12     int ret=pthread_create(&pth,NULL,threadFnc,(void *)val);
   13     THREARD_ERRORCHECK(ret,"pthread_create");
   14     long res;//不能用int,否则会访问越界
   15     pthread_join(pth,(void **)&res);
   16     printf("I am main.and getval=%ld\n",res);
   17     return 0;
   18 }

結果:
ここに画像を挿入説明
3.スレッドのキャンセル

   1 #include <func.h>
    23 void* threadFnc(void *p){
    4     printf("I am child thread\n");
    5     return NULL;
    6 }7 int main(int argc,char* argv[])
    8 {
    9     pthread_t pth;
   10     int ret=pthread_create(&pth,NULL,threadFnc,NULL);
   11     THREARD_ERRORCHECK(ret,"pthread_create");
   12     ret=pthread_cancel(pth);
   13     THREARD_ERRORCHECK(ret,"pthread_cancel");
   14     long res;
   15     ret=pthread_join(pth,(void **)&res);
   16     THREARD_ERRORCHECK(ret,"pthread_join");
   17     printf("child return :%ld\n",res);                                           
   18     printf("I am main .\n");
   19     return 0;
   20 }

結果:
ここに画像を挿入説明
成功キャンセル、子スレッドの戻り値は-1。
(1)をキャンセルすることができないながら子供がである場合
、リソースのクリーンアップ、4
アプリケーションリソースサブプロセスおよびクリーンアップ
コード:

#include <func.h>
    2 void cleanup(void *p)
    3 {
    4     free(p);
    5     printf("free end.\n");
    6 }
    7 void* threadFnc(void *p){
    8     p=malloc(20);
    9     strcpy((char *)p,"clean up");
   10     pthread_cleanup_push(cleanup,p);
   11     pthread_cleanup_pop(1);                                                      
   12     return NULL;
   13 }14 int main(int argc,char* argv[])
   15 {
   16     pthread_t pth;
   17     int ret=pthread_create(&pth,NULL,threadFnc,NULL);
   18     THREARD_ERRORCHECK(ret,"pthread_create");
   19     ret=pthread_cancel(pth);
   20     THREARD_ERRORCHECK(ret,"pthread_cancel");
   21     long res;
   22     ret=pthread_join(pth,(void **)&res);
   23     THREARD_ERRORCHECK(ret,"pthread_join");
   24     printf("child return :%ld\n",res);
   25     printf("I am main .\n");
   26     return 0;
   27 }

結果:
ここに画像を挿入説明
ヒント:クリーンアップ機能は、ポップされたプッシュ効果です。

元の記事を公開 ウォンの賞賛0 ビュー6

おすすめ

転載: blog.csdn.net/weixin_43574252/article/details/104900563