DEMO Program: Testsemaphore_demo.rar
Source code: Testsemaphore_src.rar
---Key code is as follows---
voidCtestsemaphoredlg::onbnclickedbuttonthread1 () {//TODO: Add control notification handler code hereAfxBeginThread (Afx_threadproc) Thread1writea, This);}voidctestsemaphoredlg::onbnclickedbuttonthread2 () {//TODO: Add control notification handler code hereAfxBeginThread (Afx_threadproc) Thread2writeb, This);}voidctestsemaphoredlg::onbnclickedbuttonthread3 () {//TODO: Add control notification handler code hereAfxBeginThread (Afx_threadproc) Thread3writec, This);} UINT Ctestsemaphoredlg::thread1writea (lpvoid pparam) {Ctestsemaphoredlg* PThis = (ctestsemaphoredlg*) Pparam; PThis-Writea (); return 0;}voidCtestsemaphoredlg::writea () {CString str; G_semaphore. Lock (); for(intI=0; i<5; i++) {Sleep ( -); M_editdisp.getwindowtext (str); STR+="A"; M_editdisp.setwindowtext (str); } g_semaphore. Unlock ();} UINT ctestsemaphoredlg::thread2writeb (lpvoid pparam) {Ctestsemaphoredlg* PThis = (ctestsemaphoredlg*) Pparam; PThis-Writeb (); return 0;}voidCtestsemaphoredlg::writeb () {CString str; G_semaphore. Lock (); for(intI=0; i<5; i++) {Sleep ( -); M_editdisp.getwindowtext (str); STR+="B"; M_editdisp.setwindowtext (str); } g_semaphore. Unlock ();} UINT Ctestsemaphoredlg::thread3writec (lpvoid pparam) {Ctestsemaphoredlg* PThis = (ctestsemaphoredlg*) Pparam; PThis-Writec (); return 0;}voidCtestsemaphoredlg::writec () {CString str; G_semaphore. Lock (); for(intI=0; i<5; i++) {Sleep ( -); M_editdisp.getwindowtext (str); STR+="C"; M_editdisp.setwindowtext (str); } g_semaphore. Unlock ();}
With this tutorial you can quickly learn how to use the Semaphore in MFC!
MFC Semaphore Usage Guide