系統編程-------線程編程----線程創建和調度

線程的創建和調度

1、線程的創建

pthread_create創建線程

#include <pthread.h>
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);

參數:

  • pthread_t *thread 指定創建線程id
  • const pthread_attr_t *attr;NULL
  • void (start_routine) (void *): 線程函數
  • void *arg:線程參數

返回值:成功,返回0,;出錯,返回錯誤值;

2、等待線程的返回值

pthread_join();等待線程返回值

#include <pthread.h>
int pthread_join(pthread_t thread, void **retval);

參數:

  • pthread_t thread :線程ID號
  • void **retval:線程返回結果

返回值:成功,返回0;失敗,返回錯誤號;

3、獲取線程自身線程ID

pthread_self();

#include <pthread.h>
pthread_t pthread_self(void);

參數:無

返回值:自身線程ID;

4、判斷線程是否為同一個線程

pthread_equal()

#include <pthread.h>
int pthread_equal(pthread_t t1, pthread_t t2);

參數:

  • 線程1的ID
  • 線程2的ID

返回值:相同,返回非0;不相同,返回0;

5、取消指定線程

pthread_cancel();

#include <pthread.h>
int pthread_cancel(pthread_t thread);

參數:線程ID號

返回值:成功,返回0;失敗,返回非0;

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

// 線程函數:用于提供線程的執行指令代碼
void *thread_func1(void *arg);

void *thread_func2(void *arg);

int main(int argc, char *argv[])
{
    pthread_t thread_id1;
    pthread_t thread_id2;

    // 創建線程并指定線程的執行函數
    pthread_create(&thread_id1, NULL, thread_func1, NULL);
    pthread_create(&thread_id2, NULL, thread_func2, NULL);

    // 等待指定線程的退出
    // 注意:主線程退出,則進程退出,該進程下的相關線程全部退出
    pthread_join(thread_id1, NULL);
    pthread_join(thread_id2, NULL);

    return 0;
}

void *thread_func1(void *arg)
{
    while(1)
        printf("A");

    pthread_exit(NULL);
}

void *thread_func2(void *arg)
{
    while(1)
        printf("B");
    
    pthread_exit(NULL);
}
//使用雙線程求解1~100的和
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <stdlib.h>

// 定義傳遞個線程的參數結構類型
struct thread_arg
{
    int thread_no;      // 線程編號(自定義的)
    int lower;          // 區間下限
    int upper;          // 區間上限
    // int sum;         // 傳出結果方法2,可以用作接收線程計算完畢后返回的整數結果
};

// 定義用于傳遞線程參數使用的全部變量,每一個線程一個
struct thread_arg argument1, argument2;

// 線程函數:用于提供線程執行的指令代碼
void *add_thread_func(void *arg);

int main(int argc, char *argv[])
{
    pthread_t thread_id1;
    pthread_t thread_id2;
    // 定義指向線程返回結果空間的地址
    int *psum1 = NULL;
    int *psum2 = NULL;
    
    // 創建線程1
    argument1.thread_no = 1;
    argument1.lower = 1;
    argument1.upper = 50;
    pthread_create(&thread_id1, NULL, add_thread_func, &argument1);
    // 創建線程2
    argument2.thread_no = 2;
    argument2.lower = 51;
    argument2.upper = 100;
    pthread_create(&thread_id2, NULL, add_thread_func, &argument2);
    
    // 等待線程的退出,并接收線程返回值
    pthread_join(thread_id1, &psum1);
    pthread_join(thread_id2, &psum2);
    
    printf("sum = %d\n", *psum1+*psum2);

    // 釋放從線程中得到的結果空間
    free(psum1);
    free(psum2);

    return 0;
}

// 線程函數
void *add_thread_func(void *arg)
{
    struct thread_arg *p_arg = (struct thread_arg *)arg;
    int i = 0;
    int sum = 0;
    int *p_res = NULL;
    
    // 累和
    for(i = p_arg->lower; i < p_arg->upper+1; i++)
        sum += i;
     
    //printf("in thread no %d : sum = %d\n", p_arg->thread_no, sum);
    // 開辟空間存放結果
    p_res = (int *)malloc(sizeof(int));
    *p_res = sum;
    // 返回結果方法2,使用線程參數使用的全局變量返回
    //p_arg->sum = sum;
    
    // 線程返回結果并退出
    pthread_exit(p_res);   
}  


最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容