多執行緒函式系列pthread_create(), pthread_join(), pthread_self(),pthread_exit(), pthread_detach()例項詳解

readyao發表於2015-12-16
#include <pthread.h>
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg)

建立執行緒,並可以向建立的執行緒傳遞一個引數(見例項3,向執行緒傳遞總的執行緒個數,一個int變數);


#include <pthread.h>
int pthread_join(pthread_t thread, void **retval)
等待執行緒終止,並且可以獲得執行緒的返回值;


#include <pthread.h>
pthread_t pthread_self(void);
獲得執行緒自身的執行緒ID;


執行緒可以通過下面三種方式退出:

1.執行緒只是從啟動例程中返回,返回值是執行緒的退出碼;
2.執行緒可以被同一程式中的其它執行緒取消;
3.執行緒呼叫pthread_exit

#include <pthread.h>
void pthread_exit(void *retval);
終止執行緒,並可以向主執行緒返回值;

#include <pthread.h>
int pthread_detach(pthread_t thread);
在建立的執行緒中呼叫該函式,使該執行緒脫離程式(這裡的脫離並不是真正意義上的完全脫離;脫離之後,該執行緒仍然在該程式空間裡執行,只不過是當該執行緒退出的時候,它的資源會自動釋放,程式不用等待其並釋放其資源;

總結:

1:如何建立一個或多個執行緒;

2:如何在主執行緒中等待剛才建立的執行緒;

3:如何獲得執行緒的執行緒ID;

4:如何在向建立的執行緒傳遞資訊;

5:如何在建立的執行緒中退出的時候向主執行緒傳遞資訊;

6:如何使執行緒脫離主執行緒;


例項1:簡單的建立兩個執行緒,並在主執行緒中等待這兩個執行緒,使用pthread_create, pthread_join, pthread_self

/*************************************************************************
	> File Name: create_join.cpp
	> Author: 
	> Mail: 
	> Created Time: 2015年12月16日 星期三 10時41分19秒
 ************************************************************************/

#include<iostream>
#include <unistd.h>
#include <pthread.h>
#include <cstdlib>

using namespace std;

void *task1(void *a)
{
    cout << "This is threadA..." << "thread id is " << pthread_self() << endl;
    sleep(2);
    cout << "threadA exit.." << endl;
}

void *task2(void *b)
{
    cout << "This is threadB..." << "thread id is " << pthread_self() << endl;
    sleep(2);
    cout << "ThreadB exit.." << endl;
}
int main()
{
    int ret;
    pthread_t threadA, threadB;

    ret = pthread_create(&threadA, NULL, task1, NULL);
    if(ret != 0){
        cout << "pthread_create error..." << endl;
        exit(-1);
    }
    ret = pthread_create(&threadB, NULL, task2, NULL);
    if(ret != 0){
        cout << "pthread_create error..." << endl;
        exit(-1);
    }

    ret = pthread_join(threadA, NULL);
    if(ret != 0){
        cout << "pthread_create error..." << endl;
        exit(-1);
    }
    ret = pthread_join(threadB, NULL);
    if(ret != 0){
        cout << "pthread_create error..." << endl;
        exit(-1);
    }

    cout << "main thread exit..." << endl;
    exit(0);
}



例項2:建立多個執行緒(個數自己指定),並在主執行緒中等待這些執行緒,使用pthread_create, pthread_join, pthread_self

/*************************************************************************
	> File Name: create_join.cpp
	> Author: 
	> Mail: 
	> Created Time: 2015年12月16日 星期三 10時41分19秒
 ************************************************************************/

#include<iostream>
#include <unistd.h>
#include <pthread.h>
#include <cstdlib>

using namespace std;

void *task(void *a)
{
    cout << "This is thread..." << "thread id is " << pthread_self() << endl;
    sleep(2);
    cout << "thread exit.." <<  pthread_self() << endl;
}


int main(int argc, char *argv[])
{
    int ret;
    int i = 0;
    if(argc != 2){
        cout << "You must input the number of thread you want to create..." << endl;
        exit(-1);
    }
    int thread_num = atoi(argv[1]);
    pthread_t threadId[thread_num];

    for (i = 0; i < thread_num; ++i){
	ret = pthread_create(&threadId[i], NULL, task, NULL);
	if(ret != 0){
	        cout << "pthread_create error..." << endl;
	        exit(-1);
	}			
    }
    
    for (i = 0; i < thread_num; ++i){
		ret = pthread_join(threadId[i], NULL);
	    if(ret != 0){
	        cout << "pthread_create error..." << endl;
	        exit(-1);
	    }			
	}

    cout << "main thread exit..." << endl;
    exit(0);
}



例項3:建立多個執行緒(個數自己指定)同時向各個執行緒中傳遞一個整數值,並在主執行緒中等待這些執行緒,使用pthread_create, pthread_join, pthread_self

/*************************************************************************
	> File Name: create_join.cpp
	> Author: 
	> Mail: 
	> Created Time: 2015年12月16日 星期三 10時41分19秒
 ************************************************************************/

#include<iostream>
#include <unistd.h>
#include <pthread.h>
#include <cstdlib>

using namespace std;

void *task(void *a)
{
    int i = 0;
    int * loop;
    loop = static_cast<int*> (a);
    for (i = 0; i < *loop; ++i){
        cout <<  i+1 << ": This is thread..." << "thread id is " << pthread_self() << endl;
        sleep(1);
    } 
    cout << "thread exit.." <<  pthread_self() << endl;
}


int main(int argc, char *argv[])
{
    int ret;
    int i = 0;
    if(argc != 2){
        cout << "You must input the number of thread you want to create..." << endl;
        exit(-1);
    }
    int thread_num = atoi(argv[1]);
    pthread_t threadId[thread_num];

    for (i = 0; i < thread_num; ++i){
	ret = pthread_create(&threadId[i], NULL, task, (void *)&thread_num);
	if(ret != 0){
	        cout << "pthread_create error..." << endl;
	        exit(-1);
	}			
    }
    
    for (i = 0; i < thread_num; ++i){
		ret = pthread_join(threadId[i], NULL);
	    if(ret != 0){
	        cout << "pthread_create error..." << endl;
	        exit(-1);
	    }			
	}
    
    cout << "main thread exit..." << endl;
    exit(0);
}



例項4:建立多個執行緒(個數自己指定)同時向各個執行緒中傳遞一個整數值,並在主執行緒中等待這些執行緒,執行緒退出的時候向主執行緒返回值(將自身的執行緒id傳給主執行緒);
使用pthread_create, pthread_join, pthread_self, pthread_exit

/*************************************************************************
	> File Name: create_join.cpp
	> Author: 
	> Mail: 
	> Created Time: 2015年12月16日 星期三 10時41分19秒
 ************************************************************************/

#include<iostream>
#include <unistd.h>
#include <pthread.h>
#include <cstdlib>

using namespace std;

void *task(void *a)
{
    int i = 0;
    int * loop;
    pthread_t pid = pthread_self();
    loop = static_cast<int*> (a);
    for (i = 0; i < *loop; ++i){
        cout <<  i+1 << ": This is thread..." << "thread id is " << pid << endl;
        sleep(1);
    } 
    cout << "thread exit.." <<  pid << endl;
    pthread_exit((void *)&pid);
}


int main(int argc, char *argv[])
{
    int ret;
    int i = 0;
    if(argc != 2){
        cout << "You must input the number of thread you want to create..." << endl;
        exit(-1);
    }
    int thread_num = atoi(argv[1]);
    pthread_t threadId[thread_num];

    for (i = 0; i < thread_num; ++i){
	ret = pthread_create(&threadId[i], NULL, task, (void *)&thread_num);
	if(ret != 0){
	        cout << "pthread_create error..." << endl;
	        exit(-1);
	}			
    }
    
    pthread_t * pid[thread_num];
    for (i = 0; i < thread_num; ++i){
		ret = pthread_join(threadId[i], (void **)&pid[i]);
	    if(ret != 0){
	        cout << "pthread_create error..." << endl;
	        exit(-1);
	    }			
	}
    
    for (i = 0; i < thread_num; ++i){
        cout << "main thread wait the thread id is " << *pid[i] << endl;
        
    }
    cout << "main thread exit..." << endl;
    exit(0);
}



例項5:建立多個執行緒(個數自己指定)同時向各個執行緒中傳遞一個整數值,並在主執行緒中等待這些執行緒,執行緒退出的時候向主執行緒返回值(將自身的執行緒id傳給主執行緒),最要的是讓執行緒脫離主執行緒;使用pthread_create, pthread_join, pthread_self, pthread_exit, pthread_detach

第一:出錯的例子

/*************************************************************************
	> File Name: create_join.cpp
	> Author: 
	> Mail: 
	> Created Time: 2015年12月16日 星期三 10時41分19秒
 ************************************************************************/

#include<iostream>
#include <unistd.h>
#include <pthread.h>
#include <cstdlib>

using namespace std;

void *task(void *a)
{
    int i = 0;
    int * loop;
    pthread_t pid = pthread_self();
    pthread_detach(pid);
    loop = static_cast<int*> (a);
    for (i = 0; i < *loop; ++i){
        cout <<  i+1 << ": This is thread..." << "thread id is " << pid << endl;
        sleep(1);
    } 
    cout << "thread exit.." <<  pid << endl;
    pthread_exit((void *)&pid);
}


int main(int argc, char *argv[])
{
    int ret;
    int i = 0;
    if(argc != 2){
        cout << "You must input the number of thread you want to create..." << endl;
        exit(-1);
    }
    int thread_num = atoi(argv[1]);
    pthread_t threadId[thread_num];

    for (i = 0; i < thread_num; ++i){
	    ret = pthread_create(&threadId[i], NULL, task, (void *)&thread_num);
	    if(ret != 0){
	        cout << "pthread_create error..." << endl;
	        exit(-1);
	    }			
    }
    
    pthread_t * pid[thread_num];
    for (i = 0; i < thread_num; ++i){
	    ret = pthread_join(threadId[i], (void **)&pid[i]);
	    if(ret != 0){
	        cout << "pthread_join error..." << i+1 << endl;
	        exit(-1);
	    }			
	}
    
    for (i = 0; i < thread_num; ++i){
        cout << "main thread wait the thread id is " << *pid[i] << endl;
        
    }
    cout << "main thread exit..." << endl;
    exit(0);
}


第二:正確的

/*************************************************************************
	> File Name: create_join.cpp
	> Author: 
	> Mail: 
	> Created Time: 2015年12月16日 星期三 10時41分19秒
 ************************************************************************/

#include<iostream>
#include <unistd.h>
#include <pthread.h>
#include <cstdlib>

using namespace std;

void *task(void *a)
{
    int i = 0;
    int * loop;
    pthread_t pid = pthread_self();
    pthread_detach(pid);
    loop = static_cast<int*> (a);
    for (i = 0; i < *loop; ++i){
        cout <<  i+1 << ": This is thread..." << "thread id is " << pid << endl;
        sleep(1);
    } 
    cout << "thread exit.." <<  pid << endl;
}


int main(int argc, char *argv[])
{
    int ret;
    int i = 0;
    if(argc != 2){
        cout << "You must input the number of thread you want to create..." << endl;
        exit(-1);
    }
    int thread_num = atoi(argv[1]);
    pthread_t threadId[thread_num];

	for (i = 0; i < thread_num; ++i){
		ret = pthread_create(&threadId[i], NULL, task, (void *)&thread_num);
	    if(ret != 0){
	        cout << "pthread_create error..." << endl;
	        exit(-1);
	    }			
	}
    
    sleep(5);//必須等待,如果不等待則該程式退出,那麼建立的執行緒也會退出
    cout << "main thread exit..." << endl;
    exit(0);
}


再說一個linux下寫程式的一個核武器--------------man手冊;

相關文章