• <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實現(基本跨平臺)
            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);

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

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

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

               掛載一個在執行的線程直到該線程通過調用pthread_exit或者cancelled結束。
               如果thread_return不為空,則線程th的返回值會保存到thread_return所指的區域。
               th的返回值是它給pthread_exit的參數,或者是pthread_canceled 如果是被cancelled的。
               被依附的線程th必須是joinable狀態。一定不能是detached通過使用pthread_detach或者pthread_create中使用pthread_create_detached屬性。
               當一個joinable線程結束時,他的資源(線程描述符和堆棧)不會被釋放直到另一個線程對它執行pthread_join操作。
               如果成功,返回值存儲在thread_return中,并返回0,否則返回錯誤碼:
               ESRCH:找不到指定線程
               EINVAL:線程th是detached或者已經存在另一個線程在等待線程th結束
               EDEADLK:th的參數引用它自己(即線程不能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是一種一個線程可以結束另一個線程執行的機制。更確切的說,一個線程可以發生Cancellation請求給另一個線程。
               根據線程的設置,收到請求的線程可以忽視這個請求,立即執行這個請求或者延遲到一個cancellation點執行。
               當一個線程執行Cancellation請求,相當于在那個點執行pthread_exit操作退出:所有cleanup句柄被反向調用,所有析構函數被調用結束線程并返回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則使用默認屬性。也可以通過常量PTHREAD_COND_INITIALIZER靜態初始化。
               pthread_cond_signal激活一個正在等待條件變量cond的線程。如果沒有線程在等待則什么也不會發生,如果有多個線程在等待,則只能激活一個線程,帶未指定是哪個線程(根據操作系統自己的調度策略選擇)。
               pthread_cond_broadcast激活所有在等待條件變量cond的線程。
               pthread_cond_wait自動解鎖mutex(pthread_unlock_mutex)等待條件變量cond發送。線程的執行被掛起不消耗cpu時間直到cond發送。在wait的入口mutex必須被鎖住。
               在重新回到線程前,pthread_cond_wait會重新獲得mutex(pthread_lock_mutex).解鎖mutex和掛起是自動進行的。因此,如果所有線程在發送cond前都申請mutex的話,
               這種wait的內部實現機制能夠保證在線程鎖住mutex和線程wait之間不會有cond發送操作發送。
               pthread_cond_timedwaitpthread_cond_wait,只是多了個超時設置,如果超時,則重新獲取mutex并且返回ETIMEDOUT,時間為絕對時間。
               pthread_cond_destroy銷毀一個條件變量。必須沒有線程在wait該條件變量。
               condition函數是異步信號不安全的,容易產生死鎖,必須自己控制調用流程避免死鎖。

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


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

            幾個測試程序:
            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);/*創建進程t_a*/

                pthread_create(
            &t_b,NULL,thread1,(void *)NULL); /*創建進程t_b*/
                pthread_join(t_b, NULL);
            /*等待進程t_b結束*/
                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);/*條件改變,發送信號,通知t_b進程*/
                        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;
            }
            結果:
            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;
            }
            結果:

            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 閱讀(12330) 評論(0)  編輯 收藏 引用

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

            常用鏈接

            留言簿

            文章分類

            文章檔案

            收藏夾

            搜索

            •  

            最新評論

            久久综合综合久久97色| 亚洲精品国产成人99久久| 99久久精品免费国产大片| 国内精品久久久久久麻豆| 久久免费观看视频| 欧洲人妻丰满av无码久久不卡| 日产精品久久久久久久| 久久美女人爽女人爽| 思思久久99热只有频精品66| 久久亚洲精品国产精品| 久久久久久久综合日本| 久久人人爽人人爽人人片av高请| 精品久久久久久无码国产| 色欲av伊人久久大香线蕉影院| 精品久久久久久无码免费| 亚洲AV无码1区2区久久| 久久综合视频网站| 91精品国产高清91久久久久久| 亚洲第一极品精品无码久久| 久久国产成人| 一级做a爰片久久毛片16| 亚洲AV无码久久精品色欲| 色婷婷狠狠久久综合五月| 久久99精品久久久久久动态图| 亚洲国产成人精品久久久国产成人一区二区三区综 | 久久只有这里有精品4| 久久99精品国产麻豆宅宅| 久久精品国产亚洲av日韩| 精产国品久久一二三产区区别| 国内精品久久久久久久亚洲| 国产99精品久久| 久久福利青草精品资源站免费| 日日躁夜夜躁狠狠久久AV| 蜜桃麻豆WWW久久囤产精品| 日日狠狠久久偷偷色综合免费 | 国产精品成人久久久久久久| 97久久精品国产精品青草| 996久久国产精品线观看| 99久久99久久| 亚洲天堂久久精品| 狠狠精品干练久久久无码中文字幕 |