• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            posts - 0,  comments - 5,  trackbacks - 0
            參考至http://sourceware.org/pthreads-win32/manual/index.html,其中包括pthread的win32,winc3實(shí)現(xiàn)(基本跨平臺(tái))
            pthread介紹:https://computing.llnl.gov/tutorials/pthreads/

            基本接口介紹:
            1.   pthread_create 

              #include <pthread.h>

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

               創(chuàng)建一個(gè)由調(diào)用線程控制的新的線程并發(fā)運(yùn)行。新的線程使用start_routine作為實(shí)現(xiàn)體,并以arg作為第一個(gè)參數(shù)。
               新的線程可以通過調(diào)用pthread_exit顯式結(jié)束,或者通過start_routine return來隱式結(jié)束。其中后者等價(jià)于調(diào)用pthread_exit并以start_routine 的返回值作為退出碼。
                  新線程的初始信號(hào)狀態(tài)繼承自他的創(chuàng)建線程,并且沒有掛起的信號(hào)。pthread-win32暫時(shí)未實(shí)現(xiàn)信號(hào)量。
                  attr參數(shù)指明新線程的屬性,如果attr=NULL,則使用默認(rèn)屬性:新線程是joinable(not detached),和默認(rèn)的調(diào)度策略(非實(shí)時(shí))
                 返回值:如果成功,新線程的指針會(huì)被存儲(chǔ)到thread的參數(shù)中,并返回0。如果錯(cuò)誤則一個(gè)非0的錯(cuò)誤碼返回。
                  如果返回EAGAIN,沒有足夠的系統(tǒng)資源創(chuàng)建一個(gè)線程,或者已經(jīng)存在大于PTHREAD_THREADS_MAX個(gè)活躍線程。

            2. pthread_exit
               
            #include <pthread.h>
               void pthread_exit(void *retval);
              
            pthread_exit結(jié)束調(diào)用線程的執(zhí)行.所有通過pthread_cleanup_push設(shè)置的清除句柄將會(huì)被反序執(zhí)行(后進(jìn)先出)。
             所以key值非空的線程特定數(shù)據(jù)Finalization functions被調(diào)用(參見pthread_key_create)。
              最后調(diào)用線程被終止。
              retval是這個(gè)線程結(jié)束的返回值,可以通過在別的線程中調(diào)用pthread_join來獲取這個(gè)值。
               沒有返回值。

            3. pthread_join 
               #include <pthread.h>
               int pthread_join(pthread_t th, void **thread_return);   

               掛載一個(gè)在執(zhí)行的線程直到該線程通過調(diào)用pthread_exit或者cancelled結(jié)束。
               如果thread_return不為空,則線程th的返回值會(huì)保存到thread_return所指的區(qū)域。
               th的返回值是它給pthread_exit的參數(shù),或者是pthread_canceled 如果是被cancelled的。
               被依附的線程th必須是joinable狀態(tài)。一定不能是detached通過使用pthread_detach或者pthread_create中使用pthread_create_detached屬性。
               當(dāng)一個(gè)joinable線程結(jié)束時(shí),他的資源(線程描述符和堆棧)不會(huì)被釋放直到另一個(gè)線程對(duì)它執(zhí)行pthread_join操作。
               如果成功,返回值存儲(chǔ)在thread_return中,并返回0,否則返回錯(cuò)誤碼:
               ESRCH:找不到指定線程
               EINVAL:線程th是detached或者已經(jīng)存在另一個(gè)線程在等待線程th結(jié)束
               EDEADLK:th的參數(shù)引用它自己(即線程不能join自身)

            4.pthread_cancel   

              #include <pthread.h>
              int pthread_cancel(pthread_t thread);
              int pthread_setcancelstate(int state, int *oldstate);
              int pthread_setcanceltype(int type, int *oldtype);
              void pthread_testcancel(void);

               Cancellation是一種一個(gè)線程可以結(jié)束另一個(gè)線程執(zhí)行的機(jī)制。更確切的說,一個(gè)線程可以發(fā)生Cancellation請(qǐng)求給另一個(gè)線程。
               根據(jù)線程的設(shè)置,收到請(qǐng)求的線程可以忽視這個(gè)請(qǐng)求,立即執(zhí)行這個(gè)請(qǐng)求或者延遲到一個(gè)cancellation點(diǎn)執(zhí)行。
               當(dāng)一個(gè)線程執(zhí)行Cancellation請(qǐng)求,相當(dāng)于在那個(gè)點(diǎn)執(zhí)行pthread_exit操作退出:所有cleanup句柄被反向調(diào)用,所有析構(gòu)函數(shù)被調(diào)用結(jié)束線程并返回pthread_canceled.


            5.pthread_cond   
               #include <pthread.h>
               pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
               int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr);
               int pthread_cond_signal(pthread_cond_t *cond);
               int pthread_cond_broadcast(pthread_cond_t *cond);
               int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
               int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime);
               int pthread_cond_destroy(pthread_cond_t *cond);

               
            pthread_cond_init初始化條件變量,通過cond_attr,如果cond_attr是null則使用默認(rèn)屬性。也可以通過常量PTHREAD_COND_INITIALIZER靜態(tài)初始化。
               pthread_cond_signal激活一個(gè)正在等待條件變量cond的線程。如果沒有線程在等待則什么也不會(huì)發(fā)生,如果有多個(gè)線程在等待,則只能激活一個(gè)線程,帶未指定是哪個(gè)線程(根據(jù)操作系統(tǒng)自己的調(diào)度策略選擇)。
               pthread_cond_broadcast激活所有在等待條件變量cond的線程。
               pthread_cond_wait自動(dòng)解鎖mutex(pthread_unlock_mutex)等待條件變量cond發(fā)送。線程的執(zhí)行被掛起不消耗cpu時(shí)間直到cond發(fā)送。在wait的入口mutex必須被鎖住。
               在重新回到線程前,pthread_cond_wait會(huì)重新獲得mutex(pthread_lock_mutex).解鎖mutex和掛起是自動(dòng)進(jìn)行的。因此,如果所有線程在發(fā)送cond前都申請(qǐng)mutex的話,
               這種wait的內(nèi)部實(shí)現(xiàn)機(jī)制能夠保證在線程鎖住mutex和線程wait之間不會(huì)有cond發(fā)送操作發(fā)送。
               pthread_cond_timedwaitpthread_cond_wait,只是多了個(gè)超時(shí)設(shè)置,如果超時(shí),則重新獲取mutex并且返回ETIMEDOUT,時(shí)間為絕對(duì)時(shí)間。
               pthread_cond_destroy銷毀一個(gè)條件變量。必須沒有線程在wait該條件變量。
               condition函數(shù)是異步信號(hào)不安全的,容易產(chǎn)生死鎖,必須自己控制調(diào)用流程避免死鎖。

            6.semaphore
             #include <semaphore.h>
             int sem_init(sem_t *sem, int pshared, unsigned int value);
             int sem_wait(sem_t * sem);
             int sem_timedwait(sem_t * sem, const struct timespec *abstime);
             int sem_trywait(sem_t * sem);
             int sem_post(sem_t * sem);
             int sem_post_multiple(sem_t * sem, int number);
             int sem_getvalue(sem_t * sem, int * sval);
             int sem_destroy(sem_t * sem);

             sem_init:
            初始化信號(hào)量,pshared表示該信號(hào)量是否只屬于當(dāng)前進(jìn)程(pshared==0),如果pshared不為0則表示可以在進(jìn)程間共享。value表示該信號(hào)量的初始值。
              sem_wait:如果信號(hào)量的值大于0,則自動(dòng)減1并立即返回。否則線程掛起直到sem_post或sem_post_multiple增加信號(hào)量的值。
              sem_timedwait:同sem_wait,只是多了個(gè)超時(shí)設(shè)置,如果超時(shí),首先將全局變量errno設(shè)為ETIMEDOUT,然后返回-1.
              sem_trywait:如果信號(hào)量的值大于0,將信號(hào)量減1并立即返回,否則將全局變量errno設(shè)為ETIMEDOUT,然后返回-1。sem_trywait不會(huì)阻塞。
              sem_post:釋放一個(gè)正在等待該信號(hào)量的線程,或者將該信號(hào)量+1.
              sem_post_multiple:釋放多個(gè)正在等待的線程。如果當(dāng)前等待的線程數(shù)n小于number,則釋放n個(gè)線程,并且該信號(hào)量的值增加(number - n).
              sem_get_value:返回信號(hào)量的值。在pthread-win32實(shí)現(xiàn)中,正值表示當(dāng)前信號(hào)量的值,負(fù)值的絕對(duì)值表示當(dāng)前正在等待該信號(hào)量的線程數(shù)。雖然在POSIX不需要這么表示,但是也是同樣含義。


              信號(hào)量(sem)與條件變量(cond)的主要差別在于,條件變量等待必須在發(fā)送之前,否則容易出現(xiàn)死鎖,而信號(hào)量等待可以在發(fā)送之后執(zhí)行。一句話概括信號(hào)量對(duì)于操作的時(shí)序沒有要求。

            幾個(gè)測(cè)試程序:
            1.
            #include <stdio.h>
            #include 
            <Windows.h>
            #include 
            "pthread.h"


            pthread_mutex_t mutex 
            = PTHREAD_MUTEX_INITIALIZER;/*初始化互斥鎖*/
            pthread_cond_t cond 
            = PTHREAD_COND_INITIALIZER;/*初始化條件變量*/

            void *thread1(void *);
            void *thread2(void *
            );

            int i=1
            ;
            int main(void
            )
            {
                pthread_win32_process_attach_np();
                pthread_win32_thread_attach_np();

                pthread_t t_a;
                pthread_t t_b;

                pthread_create(
            &t_a,NULL,thread2,(void *)NULL);/*創(chuàng)建進(jìn)程t_a*/

                pthread_create(
            &t_b,NULL,thread1,(void *)NULL); /*創(chuàng)建進(jìn)程t_b*/
                pthread_join(t_b, NULL);
            /*等待進(jìn)程t_b結(jié)束*/
                pthread_mutex_destroy(
            &mutex);
                pthread_cond_destroy(
            &
            cond);

                pthread_win32_thread_detach_np();
                pthread_win32_process_detach_np();

                system(
            "pause"
            );
                
            return 0
            ;
            }

            void *thread1(void *
            junk)
            {
                
            for(i=1;i<=9;i++

                {
                    
                    
            if(i%3==0
            )
                    {
                        pthread_mutex_lock(
            &mutex);/*鎖住互斥量*/

                        pthread_cond_signal(
            &cond);/*條件改變,發(fā)送信號(hào),通知t_b進(jìn)程*/
                        pthread_mutex_unlock(
            &mutex);/*解鎖互斥量*/
                    }
                    
            else        
                        printf(
            "thead1:%d\n"
            ,i);

                }

                
            return
             NULL;
            }

            void *thread2(void *
            junk)
            {
                
            while(i<9
            )
                {
                    
                    
            if(i%3!=0
            )
                    {
                        pthread_mutex_lock(
            &
            mutex);
                        pthread_cond_wait(
            &cond,&mutex);/*等待*/

                        pthread_mutex_unlock(
            &mutex);
                    }
                    printf(
            "thread2:%d\n"
            ,i);
                    

                }

                
            return
             NULL;
            }
            結(jié)果:
            2.
            #include <stdio.h>
            #include 
            <Windows.h>
            #include 
            "pthread.h"
            #include 
            <ctype.h>

            typedef 
            struct arg_set
            {
                
            char *
            fname;
                
            int
             count;
                
            int
             tid;
            }ARG_SET;

            bool bWait = false
            ;
            ARG_SET 
            *mailbox =
             NULL;
            pthread_mutex_t read_lock 
            =
             PTHREAD_MUTEX_INITIALIZER;
            pthread_mutex_t write_lock 
            =
             PTHREAD_MUTEX_INITIALIZER;
            pthread_cond_t read_cond 
            =
             PTHREAD_COND_INITIALIZER;
            pthread_cond_t write_cond 
            =
             PTHREAD_COND_INITIALIZER;


            void main(int ac, char *
            av[])
            {
                pthread_win32_process_attach_np();
                pthread_win32_thread_attach_np();

                pthread_t t1, t2, t3;
                ARG_SET args1, args2, args3;
                
            void *count_words(void *
            );
                
            int reports_in = 0
            ;
                
            int total_words = 0
            ;

                
            if (4 !=
             ac)
                {
                    printf(
            "usage: %s file1 file2 file3\n", av[0
            ]);
                    
            return
            ;
                }
                
                args1.fname 
            = av[1
            ];
                args1.count 
            = 0
            ;
                args1.tid 
            = 1
            ;
                pthread_create(
            &t1, NULL, count_words, (void *)&
            args1);

                args2.fname 
            = av[2
            ];
                args2.count 
            = 0
            ;
                args2.tid 
            = 2
            ;
                pthread_create(
            &t2, NULL, count_words, (void *)&
            args2);

                args3.fname 
            = av[3
            ];
                args3.count 
            = 0
            ;
                args3.tid 
            = 3
            ;
                pthread_create(
            &t3, NULL, count_words, (void *)&
            args3);

                
            while(reports_in < 3
            )
                {
                    printf(
            "MAIN: waiting for flag to go up\n"
            );
                    pthread_mutex_lock(
            &
            read_lock);
                    bWait 
            = true
            ;
                    pthread_cond_wait(
            &read_cond, &
            read_lock);
                    bWait 
            = false
            ;
                    pthread_mutex_unlock(
            &
            read_lock);
                    printf(
            "MAIN: wow! flag was raised, I have the lock\n"
            );

                    printf(
            "MAIN: %7d: %s\n", mailbox->count, mailbox->
            fname);
                    total_words 
            += mailbox->
            count;

                    Sleep(
            10
            );

                    printf(
            "MAIN: Ok, I've read the thread %d mail\n", mailbox->
            tid);
                    
                    pthread_mutex_lock(
            &
            write_lock);
                    pthread_cond_signal(
            &
            write_cond);
                    pthread_mutex_unlock(
            &
            write_lock);
                    printf(
            "MAIN: raising write flag\n"
            );

                    reports_in
            ++
            ;
                }
                printf(
            "%7d: total words\n"
            , total_words);

                pthread_mutex_destroy(
            &
            read_lock);
                pthread_mutex_destroy(
            &
            write_lock);
                pthread_cond_destroy(
            &
            read_cond);
                pthread_cond_destroy(
            &
            write_cond);

                pthread_win32_thread_detach_np();
                pthread_win32_process_detach_np();

                system(
            "pause"
            );
            }

            void *count_words(void *
            a)
            {
                ARG_SET 
            *args = (ARG_SET*
            )a;
                FILE 
            *
            fp;
                
            int c, prevc = '\0'
            ;

                
            if (NULL != (fp = fopen(args->fname, "r"
            )))
                {
                    
            while((c = getc(fp)) !=
             EOF)
                    {
                        
            if (!isalnum(c) &&
             isalnum(prevc))
                        {
                            args
            ->count++
            ;
                        }
                        prevc 
            =
             c;
                    }
                    fclose(fp);
                }
                
            else

                {
                    perror(args
            ->fname);
                }

                printf(
            "COUNT %d: waiting to get lock\n", args->
            tid);    
                
                
                pthread_mutex_lock(
            &
            write_lock);  
                
            if (NULL !=
             mailbox)
                {
                    printf(
            "COUNT %d: oops..mailbox not empty. wait for signal\n", args->
            tid);
                    pthread_cond_wait(
            &write_cond, &
            write_lock);
                }

                printf(
            "COUNT %d: OK, I can write mail\n", args->
            tid);
                mailbox 
            =
             args;  

                
                
            while (!
            bWait)
                {
                    Sleep(
            1
            );        
                }
                pthread_mutex_lock(
            &
            read_lock); 
                pthread_cond_signal(
            &read_cond);    /* raise the flag */

                pthread_mutex_unlock(
            &read_lock); 

                printf(
            "COUNT %d: raising read flag\n", args->
            tid);


                pthread_mutex_unlock(
            &write_lock);    /* release the mailbox */

                printf(
            "COUNT %d: unlocking box\n", args->tid);    

                
            return
             NULL;
            }
            結(jié)果:

            3.

            #include <stdio.h>
            #include 
            <Windows.h>
            #include 
            "pthread.h"
            #include 
            "semaphore.h"
            #include 
            <ctype.h>

            typedef 
            struct arg_set
            {
                
            char *fname;
                
            int count;
                
            int tid;
            }
            ARG_SET;

            ARG_SET 
            *mailbox = NULL;
            static sem_t sem_write;
            static sem_t sem_read;


            void main(int ac, char *av[])
            {
                pthread_win32_process_attach_np();
                pthread_win32_thread_attach_np();

                pthread_t t1, t2, t3;
                ARG_SET args1, args2, args3;
                
            void *count_words(void *);
                
            int reports_in = 0;
                
            int total_words = 0;

                
            if (4 != ac)
                
            {
                    printf(
            "usage: %s file1 file2 file3\n", av[0]);
                    
            return;
                }


                
            if (-1 == sem_init(&sem_read, 0 , 1)
                    
            || -1 == sem_init(&sem_write, 00))
                
            {
                    
            return;
                }


                args1.fname 
            = av[1];
                args1.count 
            = 0;
                args1.tid 
            = 1;
                pthread_create(
            &t1, NULL, count_words, (void *&args1);
                
                args2.fname 
            = av[2];
                args2.count 
            = 0;
                args2.tid 
            = 2;
                pthread_create(
            &t2, NULL, count_words, (void *&args2);
                
                args3.fname 
            = av[3];
                args3.count 
            = 0;
                args3.tid 
            = 3;
                pthread_create(
            &t3, NULL, count_words, (void *&args3);


                
            while(reports_in < 3)
                
            {
                    printf(
            "MAIN: waiting for sub thread write\n");
                    sem_wait(
            &sem_write);

                    printf(
            "MAIN: %7d: %s\n", mailbox->count, mailbox->fname);
                    total_words 
            += mailbox->count;
                    
            if ( mailbox == &args1) 
                        pthread_join(t1,NULL);
                    
            if ( mailbox == &args2) 
                        pthread_join(t2,NULL);
                    
            if ( mailbox == &args3) 
                        pthread_join(t3,NULL);
                    
                    mailbox 
            = NULL;
                    printf(
            "MAIN: Ok,I have read the mail\n");
                    sem_post(
            &sem_read);  
                    reports_in
            ++;
                }

               

                printf(
            "%7d: total words\n", total_words);
                sem_destroy(
            &sem_read);
                sem_destroy(
            &sem_write);

                pthread_win32_thread_detach_np();
                pthread_win32_process_detach_np();

                system(
            "pause");
            }



            void *count_words(void *a)
            {
                
            struct arg_set *args = (arg_set *)a;    /* cast arg back to correct type */
                FILE 
            *fp;
                
            int  c, prevc = '\0';
                 
                
            if ( (fp = fopen(args->fname, "r")) != NULL )
                
            {
                     
            while( ( c = getc(fp)) != EOF )
                     
            {
                           
            if ( !isalnum(c) && isalnum(prevc) )
                           
            {
                                args
            ->count++;
                           }

                           prevc 
            = c;
                     }

                     fclose(fp);
                 }
             else 
                      perror(args
            ->fname);
                 printf(
            "COUNT %d: waiting for main thread read the mail\n", args->tid);
                 sem_wait(
            &sem_read);
                 printf(
            "COUNT %d:OK,I can write mail\n", args->tid);
                 mailbox 
            = args;            /* put ptr to our args there */
                 printf(
            "COUNT %d: Finished writting\n", args->tid);
                 sem_post(
            &sem_write);
                 
            return NULL;
            }


            4.

            posted on 2012-09-07 13:41 saha 閱讀(12332) 評(píng)論(0)  編輯 收藏 引用

            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理



            <2025年7月>
            293012345
            6789101112
            13141516171819
            20212223242526
            272829303112
            3456789

            常用鏈接

            留言簿

            文章分類

            文章檔案

            收藏夾

            搜索

            •  

            最新評(píng)論

            青青草原综合久久大伊人| 久久精品国产亚洲av水果派| 国内精品久久久久伊人av| 亚洲精品国精品久久99热一| A级毛片无码久久精品免费| 久久精品国产亚洲AV麻豆网站| 国产精品久久久久久吹潮| 久久久久成人精品无码中文字幕| 国产综合久久久久久鬼色| 久久97久久97精品免视看秋霞| 中文成人久久久久影院免费观看| 久久无码高潮喷水| 久久福利青草精品资源站| 一本久久久久久久| 久久国产色AV免费观看| 91精品国产9l久久久久| 久久久久久极精品久久久| 色综合久久综合中文综合网| 99久久国产综合精品成人影院| 久久无码专区国产精品发布| 亚洲国产精品久久久久久| 久久精品人妻中文系列| 国产精品成人久久久久久久| 狼狼综合久久久久综合网| 久久青青草视频| 国产亚洲美女精品久久久| 久久精品午夜一区二区福利| 久久中文字幕无码专区| 色综合久久最新中文字幕| 中文字幕久久精品无码| 久久久精品久久久久影院| 国产真实乱对白精彩久久| 国产日产久久高清欧美一区| 性欧美丰满熟妇XXXX性久久久 | 久久免费看黄a级毛片| 精品久久久久久国产三级| 久久中文字幕一区二区| 国内精品伊人久久久久av一坑| 五月丁香综合激情六月久久 | 夜夜亚洲天天久久| 久久亚洲精品中文字幕三区|