例項一參考資料
uint threadfun
(lpvoid pparam)::
afxbeginthread
(threadfun,
null);
//這就是建立乙個執行緒並執行了,呼叫上面的函式彈出乙個對話方塊.
void cmultithread7dlg::
onsum()
cwinthread*
afxbeginthread
( afx_threadproc pfnthreadproc,
//乙個函式指標
lpvoid pparam,
//void*型別的指標,可以傳任何種類指標過來.
int npriority = thread_priority_normal,
//執行緒優先順序
unt nstacksize =0,
//分配堆疊大小
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
);
pthreadclass 是指向 cwinthread 的乙個匯出類的執行時類物件的指標,該匯出類定義了被建立的使用者介面執行緒的啟動、退出等。windows基於事件觸發機制工作,每乙個win32應用程式都至少有乙個訊息佇列和乙個訊息幫浦。訊息佇列是作業系統預留在記憶體區域中的,訊息幫浦機制又稱為訊息迴圈機制,不斷搜尋訊息佇列,將取得的訊息分發給應用程式的各個部分。
基本訊息迴圈如下:
while
(getmessage
(&msg,0,
0,0)
)
m_hthread:當前執行緒的控制代碼;m_nthreadid:當前執行緒的id;
m_pmainwnd:指向應用程式主視窗的指標
該函式中的dwcreateflags、nstacksize、lpsecurityattrs引數和api函式createthread中的對應引數有相同含義,該函式執行成功,返回非0值,否則返回0。bool cwinthread::
createthread
(dword dwcreateflags=0,
uint nstacksize=0,
lpsecurity_attributes lpsecurityattrs=
null
);
一般情況下,呼叫afxbeginthread()來一次性地建立並啟動乙個執行緒,但是也可以通過兩步法來建立執行緒:首先建立cwinthread類的乙個物件,然後呼叫該物件的成員函式createthread()來啟動該執行緒。
virtual bool cwinthread::initinstance();過載該函式以控制使用者介面執行緒例項的初始化。初始化成功則返回非0值,否則返回0。使用者介面執行緒經常過載該函式,工作者執行緒一般不使用 initinstance()。
virtual int cwinthread::exitinstance();
bool cuithread::
initinstance()
mfc執行緒(一):簡單介紹void cuithreaddlg::
onbutton1()
mfc多執行緒各種執行緒用法
多執行緒MFC
csemaphore semaphorewrite 2,2 資源最多訪問執行緒2個,當前可訪問執行緒數2個 cwinthread pwritea afxbeginthread writea,m ctrla,thread priority normal,0,create suspended pwrit...
MFC多執行緒
如果使用者的應用程式需要有多個任務同時進行相應的處理,則使用多執行緒是較理想的選擇。多執行緒對於網路,列印,字處理,圖形影象,動畫和檔案管理的同乙個系統下執行來說,是非常必要的。在乙個多執行緒程式中,程序必須有乙個主線程,主線程可以在任何需要的時候建立新的執行緒。所有活動的縣城共享程序的資源。解決多...
MFC多執行緒 如何安全終止MFC多執行緒
3 在檔案中定義執行緒傳遞引數的資料結構,如下 demodlg.h typedef struct thread param thread param 4 在cdemodlg類中新增成員變數,如下 demodlg.h protected cwinthread m pthread thread para...