建立新執行緒

2021-05-01 18:42:10 字數 3061 閱讀 2512

在進行多執行緒程式設計的時候,我們經常用到afxbeginthread函式來啟動一條執行緒

該函式使用起來非常的簡單方便,其定義如下

cwinthread* afxbeginthread(

afx_threadproc pfnthreadproc,//執行緒函式位址

lpvoid pparam,//執行緒引數

int npriority = thread_priority_normal,//執行緒優先順序

uint nstacksize = 0,//執行緒堆疊大小,預設為1m

dword dwcreateflags = 0,//

lpsecurity_attributes lpsecurityattrs = null

);cwinthread* afxbeginthread(

cruntimeclass* pthreadclass,

int npriority = thread_priority_normal,

uint nstacksize = 0,

dword dwcreateflags = 0,

lpsecurity_attributes lpsecurityattrs = null

);引數說明:

pfnthreadproc:執行緒函式的位址,該引數不能設定為null,執行緒函式必須定義成全域性函式或者類的靜態成員函式

例如:uint mythreadfunc(lpvoid lparam)

或者class a

之所以要定義成類的靜態成員函式,是因為類的靜態成員函式不屬於某個類物件,這樣在呼叫函式

的時候就不用傳遞乙個額外的this指標.

pthreadclass:指向從cwinthread派生的子類物件的runtime_class

pparam:要傳遞給執行緒函式的引數

npriority:要啟動的執行緒的優先順序,預設優先順序為thread_priority_normal(普通優先順序),關於執行緒

優先順序的詳細說明請參考platform sdk setthreadpriority函式說明

nstacksize:新執行緒的堆疊大小,如果設定為0,則使用預設大小,在應用程式中一般情況下執行緒的預設堆疊大小

為1mdwcreateflags:執行緒建立標誌,該引數可以指定為下列標誌

比如:m_bautodelete或者你的派生類中的成員變數,當初始化完成之後,你可以使用cwinthread類的resumethread

成員函式來恢復執行緒的執行

如果把該標誌設定為0,則表示立即啟動執行緒

lpsecurityattrs:指向安全描述符的指標,如果使用預設的安全級別只要講該引數設定為null就可以了!

上面就是afxbeginthread函式的簡單說明,我們在使用的時候一般情況下只要指定前兩個引數,其他

引數使用預設值就可以.嗯,的確,使用起來是很簡單,只要這個函式一被呼叫,就建立了乙個執行緒.

但是大家有沒有想過,afxbeginthread函式究竟是如何啟動的執行緒呢?它的內部是如何實現的呢?

下面我們就來看一下afxbeginthread函式的內部實現

//啟動worker執行緒

cwinthread* afxapi afxbeginthread(afx_threadproc pfnthreadproc, lpvoid pparam,

int npriority, uint nstacksize, dword dwcreateflags,

lpsecurity_attributes lpsecurityattrs)

verify(pthread->setthreadpriority(npriority));

if (!(dwcreateflags & create_suspended))

verify(pthread->resumethread() != (dword)-1);

return pthread;

#endif //!_mt)

}//啟動ui執行緒

cwinthread* afxapi afxbeginthread(cruntimeclass* pthreadclass,

int npriority, uint nstacksize, dword dwcreateflags,

lpsecurity_attributes lpsecurityattrs)

verify(pthread->setthreadpriority(npriority));

if (!(dwcreateflags & create_suspended))

verify(pthread->resumethread() != (dword)-1);

return pthread;

#endif //!_mt

}從上面的**中可以看出afxbeginthread所做的事情主要有以下幾點:

1.在heap中配置乙個新的cwinthread物件(worker執行緒)

**如:cwinthread* pthread = debug_new cwinthread(pfnthreadproc, pparam);

呼叫cruntimeclass結構中的createobject函式建立cwinthread物件

cwinthread* pthread = (cwinthread*)pthreadclass->createobject();

《深入淺出mfc》侯捷著

2.呼叫cwinthread::createthread()並設定屬性,使執行緒以掛起狀態產生

pthread->createthread(dwcreateflags|create_suspended, nstacksize,lpsecurityattrs);

3.設定執行緒的優先權

pthread->setthreadpriority(npriority);

4.呼叫cwinthread::resumethread

pthread->resumethread();

通過上面的說明,我想大家對該函式到底在內部都做了什麼,應該有乙個初步的了解了!

對於vc老手來說,這篇文章可能並沒有什麼可讀之處,但是對於初學者來說,還是有一定的

價值的!

總之,希望這篇文章能給各位一點點的幫助!

多執行緒 建立新執行緒

執行緒是 的執行序列或者執行路徑。執行緒與程序的概念有一些類似,它類似於乙個輕量級的程序 乙個作業系統可以執行多個程序,乙個程序內可以執行多個執行緒。每個應用程式至少執行在乙個執行緒上。當只有乙個執行緒時,稱作單執行緒應用程式,該執行緒由系統自動建立。下面看乙個簡單單執行緒例子 如上圖,本短程式從入...

執行緒中建立新執行緒,退出機制

執行緒中建立新執行緒,退出機制 程式 include include include include pthread t son t 0 void cleanup void 這一句是找出本程序的所有執行緒.printf clean n void son cleanup void void test ...

建立新執行緒的幾個主要方法

這裡面主要介紹了建立新執行緒的幾個方法,不討論實現執行緒有多少種的方法 public class testrunnableinte ce implements runnable public static void main string args 實現 runnable介面後,需要重寫它的 run...