最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++?pthread入門指南

 更新時(shí)間:2024年05月30日 14:20:22   作者:SGchi  
pthread是C++98接口且只支持Linux,使用時(shí)需要包含頭文件#include?<pthread.h>,編譯時(shí)需要鏈接pthread庫,其中p是POSIX的縮寫,而POSIX是Portable?Operating?System?Interface的縮寫,這篇文章主要介紹了C++?pthread簡介,需要的朋友可以參考下

一、pthread簡介

pthread是C++98接口且只支持Linux,使用時(shí)需要包含頭文件#include <pthread.h>,編譯時(shí)需要鏈接pthread庫,其中p是POSIX的縮寫,而POSIX是Portable Operating System Interface的縮寫,是IEEE為要在各種UNIX操作系統(tǒng)上運(yùn)行軟件,而定義API的一系列互相關(guān)聯(lián)的標(biāo)準(zhǔn)的總稱。(Windows環(huán)境下無pthread,Linux GCC4.6以下編譯需加-pthread編譯選項(xiàng))

1、線程創(chuàng)建

int pthread_create (pthread_t *thread,
					pthread_attr_t *attr,
					void *(*start_routine)(void *),void *arg);

若創(chuàng)建成功,返回0;若出錯(cuò),則返回錯(cuò)誤編號。

  • thread 是線程標(biāo)識符,但這個(gè)參數(shù)不是由用戶指定的,而是由 pthread_create 函數(shù)在創(chuàng)建時(shí)將新的線程的標(biāo)識符放到這個(gè)變量中。
  • attr 指定線程的屬性,包含了線程的調(diào)度策略,堆棧的相關(guān)信息,join or detach 的狀態(tài)等,可以用 NULL 表示默認(rèn)屬性。
  • start_routine 指定線程開始運(yùn)行的函數(shù)。
  • arg 是 start_routine 所需要的參數(shù),是一個(gè)無類型指針。

pthread_attr_t 操作函數(shù):

pthread_attr_t attr; // 聲明attr
pthread_attr_init(&attr);  // 創(chuàng)建attr
pthread_attr_destroy(&attr); // 銷毀attr
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); // 設(shè)置為joinable

2、線程終止

void pthread_exit (void *retval);
int pthread_cancel (pthread_t thread);

當(dāng)發(fā)生以下情形之一時(shí),線程就會結(jié)束:

  • 線程運(yùn)行的函數(shù)return了,也就是線程的任務(wù)已經(jīng)完成;
  • 線程調(diào)用了 pthread_exit 函數(shù);
  • 其他線程調(diào)用 pthread_cancel 結(jié)束這個(gè)線程;
  • 進(jìn)程調(diào)用 exec() 或 exit(),結(jié)束了;
  • main() 函數(shù)先結(jié)束了,而且 main() 自己沒有調(diào)用 pthread_exit 來等所有線程完成任務(wù)。

當(dāng)然,一個(gè)線程結(jié)束,并不意味著它的所有信息都已經(jīng)消失,后面會看到僵尸線程的問題。

一個(gè)線程可以通過調(diào)用 pthread_cancel 函數(shù)來請求取消同一進(jìn)程中的線程,這個(gè)線程由thread 參數(shù)指定。如果操作成功則返回0,失敗則返回對應(yīng)的錯(cuò)誤編號。

3、線程同步

int pthread_join(pthread_t threadid, void **value_ptr);
int pthread_detach (threadid);

  阻塞是線程之間同步的一種方法。pthread_join 函數(shù)會讓調(diào)用它的線程等待 threadid 線程運(yùn)行結(jié)束之后再運(yùn)行,value_ptr 存放了其他線程的返回值。
  一個(gè)可以被join的線程,僅僅可以被別的一個(gè)線程 join,如果同時(shí)有多個(gè)線程嘗試 join 同一個(gè)線程時(shí),最終結(jié)果是未知的。另外,線程不能 join 自己。

值得注意的的是:僵尸線程 ( “zombie” thread )是一種已經(jīng)退出了的 joinable 的線程,但是等待其他線程調(diào)用
pthread_join 來 join 它,以收集它的退出信息(exit status)。如果沒有其他線程調(diào)用 pthread_join 來
join 它的話,它占用的一些系統(tǒng)資源不會被釋放,比如堆棧。如果main()函數(shù)需要長時(shí)間運(yùn)行,并且創(chuàng)建大量 joinable的線程,就有可能出現(xiàn)堆棧不足的 error 。 對于那些不需要 join 的線程,最好利用
pthread_detach,這樣它運(yùn)行結(jié)束后,資源就會及時(shí)得到釋放。注意一個(gè)線程被使用 pthread_detach
之后,它就不能再被改成 joinable 的了。 總而言之,創(chuàng)建的每一個(gè)線程都應(yīng)該使用 pthread_join 或者
pthread_detach 其中一個(gè),以防止僵尸線程的出現(xiàn)。

4、其他函數(shù)

pthread_self (); // 返回當(dāng)前線程的 thread ID
pthread_equal (thread1,thread2); // pthread_equal 比較兩個(gè)線程的 ID,如果不同則返回0,否則返回一個(gè)非零值
// 互斥鎖
pthread_mutex_t mutexsum;
pthread_mutex_init (mutex,attr);
pthread_mutex_destroy (pthread_mutex_t *mutex);
pthread_mutexattr_init (attr);
pthread_mutexattr_destroy (attr);
phtread_mutex_lock(pthread_mutex_t *mutex);
phtread_mutex_trylock(pthread_mutex_t *mutex);
phtread_mutex_unlock(pthread_mutex_t *mutex);

5、示例代碼

#include<stdlib.h>
#include <pthread.h>
#include <stdio.h>
#include <math.h>
#define NUM_THREADS 4
void *BusyWork(void *t)
{
    int i;
    long tid;
    double result = 0.0;
    tid = (long) t;
    printf("Thread %ld starting...\n", tid);
    for (i = 0; i < 1000000; i++)
    {
        result = result + sin(i) * tan(i);
    }
    printf("Thread %ld done. Result = %e\n", tid, result);
    pthread_exit((void *) t);
}
int main(int argc, char *argv[])
{
    pthread_t thread[NUM_THREADS];
    pthread_attr_t attr;
    int rc;
    long t;
    void *status;
/* Initialize and set thread detached attribute */
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
    for (t = 0; t < NUM_THREADS; t++)
    {
        printf("Main: creating thread %ld\n", t);
        rc = pthread_create(&thread[t], &attr, BusyWork, (void *) t);
        if (rc)
        {
            printf("ERROR; return code from pthread_create() is %d\n", rc);
            exit(-1);
        }
    }
/* Free attribute and wait for the other threads */
    pthread_attr_destroy(&attr);
    for (t = 0; t < NUM_THREADS; t++)
    {
        rc = pthread_join(thread[t], &status);
        if (rc)
        {
            printf("ERROR; return code from pthread_join() is %d\n", rc);
            exit(-1);
        }
        printf("Main: completed join with thread %ld having a status of %ld\n", t, (long) status);
    }
    printf("Main: program completed. Exiting.\n");
    pthread_exit(NULL);
}
Main: creating thread 0
Main: creating thread 1
Thread 0 starting...
Thread 1 starting...
Main: creating thread 2
Main: creating thread 3
Thread 2 starting...
Thread 3 starting...
Thread 1 done. Result = -3.153838e+06
Thread 0 done. Result = -3.153838e+06
Main: completed join with thread 0 having a status of 0
Main: completed join with thread 1 having a status of 1
Thread 3 done. Result = -3.153838e+06
Thread 2 done. Result = -3.153838e+06
Main: completed join with thread 2 having a status of 2
Main: completed join with thread 3 having a status of 3
Main: program completed. Exiting.

參考鏈接:C++ 多線程編程(二):pthread的基本使用

到此這篇關(guān)于C++ pthread入門指南的文章就介紹到這了,更多相關(guān)C++ pthread內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C語言實(shí)現(xiàn)自動(dòng)發(fā)牌程序

    C語言實(shí)現(xiàn)自動(dòng)發(fā)牌程序

    這篇文章主要介紹了利用C語言實(shí)現(xiàn)自動(dòng)發(fā)牌程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-12-12
  • opencv實(shí)現(xiàn)矩形檢測

    opencv實(shí)現(xiàn)矩形檢測

    這篇文章主要為大家詳細(xì)介紹了opencv實(shí)現(xiàn)矩形檢測,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-07-07
  • 解決VC++編譯報(bào)錯(cuò)error C2248的方案

    解決VC++編譯報(bào)錯(cuò)error C2248的方案

    這篇文章主要介紹了解決VC++編譯報(bào)錯(cuò)error C2248的方案的相關(guān)資料,需要的朋友可以參考下
    2015-11-11
  • 減少C++代碼編譯時(shí)間的簡單方法(必看篇)

    減少C++代碼編譯時(shí)間的簡單方法(必看篇)

    下面小編就為大家?guī)硪黄獪p少C++代碼編譯時(shí)間的簡單方法(必看篇)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-01-01
  • 深入解析C++中的字符數(shù)組和處理字符串的方法

    深入解析C++中的字符數(shù)組和處理字符串的方法

    這篇文章主要介紹了深入解析C++中的字符數(shù)組和處理字符串的方法,需要的朋友可以參考下
    2015-09-09
  • 淺談C++/C關(guān)于#define的那些奇奇怪怪的用法

    淺談C++/C關(guān)于#define的那些奇奇怪怪的用法

    本文主要介紹了C++/C關(guān)于#define的那些奇奇怪怪的用法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • Qt連接MySQL數(shù)據(jù)庫的實(shí)現(xiàn)(保姆級成功版教程)

    Qt連接MySQL數(shù)據(jù)庫的實(shí)現(xiàn)(保姆級成功版教程)

    本文主要介紹了Qt連接MySQL數(shù)據(jù)庫的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • C/C++實(shí)現(xiàn)線性順序表的示例代碼

    C/C++實(shí)現(xiàn)線性順序表的示例代碼

    使用順序存儲結(jié)構(gòu)的線性存儲結(jié)構(gòu)的表為線性順序表。本文將分別利用C語言和C++實(shí)現(xiàn)線性順序表,文中示例代碼講解詳細(xì),需要的可以參考一下
    2022-05-05
  • 基于C++泛型編程職工管理系統(tǒng)

    基于C++泛型編程職工管理系統(tǒng)

    這篇文章主要介紹了基于C++泛型編程職工管理系統(tǒng),前面介紹到了C++的泛型編程,并實(shí)現(xiàn)了萬能容器,不過那使用的是數(shù)組,今天呢咱帶大家實(shí)踐一下使用泛型技術(shù),結(jié)合單鏈表實(shí)現(xiàn)一個(gè)職工管理系統(tǒng),需要的朋友可以參考一下
    2022-02-02
  • C++簡明圖解this指針的使用

    C++簡明圖解this指針的使用

    this 指針在C++類和對象中是個(gè)很方便實(shí)用的關(guān)鍵字,可以簡化對象成員屬性的調(diào)用,使代碼表達(dá)的含義更加準(zhǔn)確;在之前的學(xué)習(xí)中我們都可以判斷變量所占內(nèi)存空間大小,那么我們創(chuàng)建的類對象所占的內(nèi)存空間怎么計(jì)算呢?想知道this的妙用和類對象占用的內(nèi)存空間就來跟我學(xué)習(xí)吧
    2022-06-06

最新評論

泰兴市| 东乌| 平乡县| 水城县| 九寨沟县| 乌审旗| 象山县| 肇东市| 延寿县| 承德县| 布尔津县| 华池县| 秦安县| 开江县| 濮阳县| 松江区| 论坛| 玉溪市| 高邮市| 特克斯县| 施甸县| 绩溪县| 张家川| 陆川县| 江油市| 新乐市| 淮滨县| 中宁县| 濮阳县| 万山特区| 大丰市| 色达县| 吉木乃县| 内黄县| 曲沃县| 涟源市| 沽源县| 四平市| 崇仁县| 永兴县| 华池县|