回答
有pthreads-win32:http:// sourceware。org/pthreads-win32/ – CesarB 2008-12-02 11:00:12
我沒有用它自己,但我聽說了Boost線程庫使得它非常容易。
它們讓它變得非常簡單。 :) – Rob 2008-12-02 07:52:46
線程是不容易的,無論庫/ API – 2008-12-02 18:13:31
You may want to read my earlier posting on SO.
(事後看來,這是張貼有點片面走向並行線程,但我在Unix/Linux的那種人,而且這種做法似乎是最好的關於原創話題。)
我用我的大學教授寫的圖書館。這是非常簡單的實施和工作非常好(現在使用它相當長一段時間)。我會請他的許可與你分享。
對不起,等待未來,而是應該看看:)
++++++編輯+++++++
好了,我跟我的教授,如果我在這裏分享他不介意。下面是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庫包含一堆類,使您能夠使用進程和線程以及其他併發編程技術。發現其餘;)
我使用intel線程構建塊庫中的tbb_thread類。
有很多線程庫與C++兼容。所以起初你必須選擇一個。我更喜歡OpenMP或POSIX線程(也稱爲pthreads)。如何編譯它取決於你選擇的庫。在C/C++線程
用法:
#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;
}
- 1. 使用C++中的線程
- 2. 使用BackgroundWorker在C#中併發線程
- 3. 在C中使用列表框線程#
- 4. C#在線程中使用Process.Start
- 5. 在C#中使用計時器線程
- 6. 線程在C中使用WaitHandles等待#
- 7. 在c中使用ArrayList的線程#
- 8. 使用線程C#
- 9. 線程在C#中
- 10. C# - 通過使用線程
- 11. C#線程Ram使用
- 12. 使用C++實現線程
- 13. 其中C#多線程選項使用
- 14. c中使用線程安全隨機數的多線程
- 15. 線程在C#中使用帶代理的線程中止異常
- 16. 如何在C/C++中使用源代碼級多線程?
- 17. 在線程中使用TWordApplication
- 18. 在類中使用線程
- 19. Objective-C - 在應用程序中使用在線常量
- 20. 在線c#應用程序
- 21. 在linux中的C線程?
- 22. 在C中的線程#
- 23. Parallel.Invoke中的線程在c#
- 24. c#在setter中的線程
- 25. 在c中的線程#
- 26. 在C#中創建線程
- 27. 在C中鎖定線程#
- 28. C#在新線程中調用方法
- 29. 線程在C#中調用.WPF
- 30. 在C#中進行線程調用
我跟我的教授約在這裏分享了他的圖書館後作出的編輯我的答案。 – caglarozdag 2008-12-02 18:20:33