2008-12-02 197 views
6

您能告訴我如何在C++程序中使用線程,以及如何編譯它,因爲它將是多線程的?你能告訴我一些可以從根開始的好站點嗎?在C++中使用線程

感謝

+0

我跟我的教授約在這裏分享了他的圖書館後作出的編輯我的答案。 – caglarozdag 2008-12-02 18:20:33

回答

1

我用我的大學教授寫的圖書館。這是非常簡單的實施和工作非常好(現在使用它相當長一段時間)。我會請他的許可與你分享。

對不起,等待未來,而是應該看看:)

++++++編輯+++++++

好了,我跟我的教授,如果我在這裏分享他不介意。下面是h和.cpp文件由保羅·戴維斯所寫的「RT圖書館能夠做出有關線程

http://www.filefactory.com/file/7efbeb/n/rt_h

http://www.filefactory.com/file/40d9a6/n/rt_cpp

的幾點和使用這個庫:

0)本教程將解釋在Windows平臺上創建和使用線程。

1在c)中的線程++通常編碼爲相同源的一部分(不同於進程,其中每個進程具有其自己的源文件和main()函數)

2)當一個進程是啓動和運行,它可以通過進行適當的內核調用來創建其他線程。

3)多線程比多進程運行得更快,因爲它們是同一進程的一部分,從而減少了OS的開銷,並減少了對內存的需求。

4)你將在你的案例中使用的是rt庫中的CThread類。

5)(確保rt.h和rt.cpp是你'解決方案'的一部分,並確保在你的main中包含rt.h。cpp)

6)下面是你未來主線程(當然是main.cpp)的代碼的一部分,你將使用CThread類創建線程。

void main() 
{ 
    CThread t1(ChildThread1, ACTIVE, NULL) ;  
    . . . 
    t1.WaitForThread() ;    // if thread already dead, then proceed, otherwise wait 

} 

爲了在T1的參數是:名稱作爲我們的線程函數,線程狀態(它可以是啓動或暫停 - 這取決於你想要什麼),以及最後的指針可選您可能想要在創建時傳遞給線程的數據。在執行一些代碼之後,您需要調用WaitForThread()函數。

7)下面是未來主線程代碼的一部分(當然是在main.cpp中),您將在其中描述子線程的功能。

UINT _ _stdcall ChildThread1(void *args)  
{ 

    . . .   
} 

奇怪的是微軟的線程簽名。我確信通過一些研究,你可以弄清楚在其他操作系統中如何做到這一點。參數是可以在創建時傳遞給孩子的可選數據。

8)您可以在rt.cpp文件中找到成員函數的詳細描述。以下是摘要:

CThread() - 負責創建線程

暫停()構造函數 - 掛起一個子線程有效暫停它。

恢復() - 喚醒在暫停子線程

SetPriority(int值) - 一個子線程的優先級改變到 指定

郵政(整型消息)的值 - 將消息放到一個子線程

TerminateThread() - 終止或殺死一個子線程

WaitForThread() - 暫停父線程,直到一個子線程終止。 如果子線程已經終止,父進程不會暫停

9)下面是一個示例完整程序示例。一個聰明的事情你可以做的是創建一個單一線程的多個實例。

#include 「..\wherever\it\is\rt.h」 //notice the windows notation 

    int  ThreadNum[8] = {0,1,2,3,4,5,6,7} ; // an array of thread numbers 


    UINT _ _stdcall ChildThread (void *args) // A thread function 
    { 
     MyThreadNumber = *(int *)(args);  

     for (int i = 0; i < 100; i ++) 
      printf("I am the Child thread: My thread number is [%d] \n", MyThreadNumber) ; 

     return 0 ; 
    } 
int  main() 
{ 
    CThread  *Threads[8] ; 

// Create 8 instances of the above thread code and let each thread know which number it is. 


    for (int i = 0; i < 8; i ++) { 
     printf ("Parent Thread: Creating Child Thread %d in Active State\n", i) ; 
     Threads[i] = new CThread (ChildThread, ACTIVE, &ThreadNum[i]) ; 
    } 

    // wait for threads to terminate, then delete thread objects we created above 

    for(i = 0; i < 8; i ++) { 
     Threads[i]->WaitForThread() ; 
    delete Threads[i] ; // delete the object created by ‘new’ 
    } 
    return 0 ; 
} 

10)就是這樣! rt庫包含一堆類,使您能夠使用進程和線程以及其他併發編程技術。發現其餘;)

3

我使用intel線程構建塊庫中的tbb_thread類。

1

有很多線程庫與C++兼容。所以起初你必須選擇一個。我更喜歡OpenMP或POSIX線程(也稱爲pthreads)。如何編譯它取決於你選擇的庫。在C/C++線程

0

用法:

#include <iostream> 

using namespace std; 

extern "C" 
{ 
    #include <stdlib.h> 
    #include <pthread.h> 
    void *print_message_function(void *ptr); 
} 


int main() 
{ 
    pthread_t thread1, thread2; 
    char *message1 = "Thread 1"; 
    char *message2 = "Thread 2"; 
    int iret1, iret2; 

    iret1 = pthread_create(&thread1, NULL, print_message_function (void*) message1); 
    iret2 = pthread_create(&thread2, NULL, print_message_function, (void*) message2); 

    pthread_join(thread1, NULL); 
    pthread_join(thread2, NULL); 

    //printf("Thread 1 returns: %d\n",iret1); 
    //printf("Thread 2 returns: %d\n",iret2); 
    cout<<"Thread 1 returns: %d\n"<<iret1; 
    cout<<"Thread 2 returns: %d\n"<<iret2; 

    exit(0); 
} 

void *print_message_function(void *ptr) 
{ 
    char *message; 
    message = (char *) ptr; 
    //printf("%s \n", message); 
    cout<<"%s"<<message; 
}