首页 > 代码库 > 同步线程和进程间的通信
同步线程和进程间的通信
最近回去学习了一下进程和进程间的通信,有时候很多东西久不看了也就一下子忘了==
这里面有好几个互斥对象使用线程的
1 void mListText(CString str) 2 { 3 m_list_text.AddString(str); 4 m_list_text.SendMessage(WM_VSCROLL, SB_PAGEDOWN, 0); 5 } 6 7 8 9 DWORD WINAPI Thread1(LPVOID lpParameter) 10 { 11 //GetDlgItem(IDC_STATIC_THREAD) 12 //SetDlgItemText(NULL, IDC_STATIC_THREAD, L"Thread1 is running"); 13 //CString str1 = L"Thread1 is running"; 14 //C线程Dlg test; 15 //test.m_list_text.AddString(str1); 16 //SetDlgItemText(test, IDC_STATIC_THREAD, L"Thread1 is running"); 17 //AfxMessageBox(L"Thread1 is running"); 18 CString str2 = L"Thread1 is running"; 19 m_list_text.AddString(str2); 20 //Sleep(1000); 21 //mListText(str2); 22 return 0; 23 } 24 25 DWORD WINAPI Thread2(LPVOID lpParameter) 26 { 27 //GetDlgItem(IDC_STATIC_THREAD) 28 CString str2 = L"Thread2 is running"; 29 m_list_text.AddString(str2); 30 //Sleep(1000); 31 //mListText(str2); 32 //test.m_list_text.AddString(str2); 33 //SetDlgItemText( IDC_STATIC_THREAD, L"Thread2 is running"); 34 //AfxMessageBox(L"Thread2 is running"); 35 return 0; 36 } 37 38 39 //线程,不同步的 40 void C线程Dlg::OnStatThread() 41 { 42 // TODO: 在此添加控件通知处理程序代码 43 //SetDlgItemText( IDC_STATIC_THREAD,L"Thread2 is running"); 44 HANDLE T1, T2; 45 int i = 0; 46 while (i != 5) 47 { 48 T1 = CreateThread(NULL, 0, Thread1, NULL, 0, NULL); 49 T2 = CreateThread(NULL, 0, Thread2, NULL, 0, NULL); 50 Sleep(1000); 51 i++; 52 } 53 CloseHandle(T1); 54 CloseHandle(T2); 55 } 56 57 //线程3,线程4 使用InitializeCriticalSection()来操作临界对象 58 DWORD WINAPI Thread3(LPVOID lpParameter) 59 { 60 C线程Dlg test; 61 while (true) 62 { 63 EnterCriticalSection(&Section); //进入临界区 64 a1++; 65 if (a1 <= 10) 66 { 67 Sleep(1000); 68 CString str; 69 str.Format(L"Thread3 is running,count=%d", a1); 70 //AfxMessageBox(str); 71 mListText(str); 72 LeaveCriticalSection(&Section); //离开临界区 73 } 74 else 75 { 76 LeaveCriticalSection(&Section); 77 break; 78 } 79 } 80 return 0; 81 } 82 83 //线程4 84 DWORD WINAPI Thread4(LPVOID lpParameter) 85 { 86 C线程Dlg dlg; 87 while (true) 88 { 89 EnterCriticalSection(&Section); //进入临界区 90 a1++; 91 if (a1 <=10) 92 { 93 Sleep(1000); 94 CString str; 95 str.Format(L"Thread4 is running,count=%d", a1); 96 mListText(str); 97 //AfxMessageBox(str); 98 //AfxMessageBox(L"Thread4 is running"); 99 LeaveCriticalSection(&Section); //离开临界区100 }101 else102 {103 LeaveCriticalSection(&Section);104 break;105 }106 }107 return 0;108 }109 110 //同步线程111 void C线程Dlg::OnSynchronous()112 {113 // TODO: 在此添加控件通知处理程序代码114 HANDLE T3, T4;115 T3 = CreateThread(NULL, 0, Thread3, NULL, 0, NULL);116 T4 = CreateThread(NULL, 0, Thread4, NULL, 0, NULL);117 118 CloseHandle(T3);119 CloseHandle(T4);120 121 //初始化临界区122 InitializeCriticalSection(&Section);123 if (a1 == 10)124 {125 DeleteCriticalSection(&Section); //删除临界区126 }127 }128 129 //线程5,线程6使用CCriticalSection操作临界区130 DWORD WINAPI Thread5(LPVOID lpvoid)131 {132 m_Section.Lock(); //对临界区锁定133 a1++;134 CString str;135 str.Format(L"Thread5 is running,count=%d", a1);136 mListText(str);137 //AfxMessageBox(str);138 m_Section.Unlock(); //解锁,释放临界区139 return 0;140 }141 142 DWORD WINAPI Thread6(LPVOID lpvoid)143 {144 m_Section.Lock(); //对临界区锁定145 a1++;146 CString str;147 str.Format(L"Thread6 is running,count=%d", a1);148 //AfxMessageBox(str);149 mListText(str);150 m_Section.Unlock(); //解锁,释放临界区151 152 return 0;153 }154 155 156 157 void C线程Dlg::OnCCriticalSectionThread()158 {159 // TODO: 在此添加控件通知处理程序代码160 HANDLE T5, T6;161 T5 = CreateThread(NULL, 0, Thread5, NULL, 0, NULL);162 T6 = CreateThread(NULL, 0, Thread6, NULL, 0, NULL);163 CloseHandle(T5);164 CloseHandle(T6);165 Sleep(3000);166 }167 168 169 void C线程Dlg::ListText(CString str)170 {171 m_list_text.AddString(str);172 m_list_text.SendMessage(WM_VSCROLL, SB_PAGEDOWN, 0);173 }174 175 176 177 void C线程Dlg::OnMListClear()178 {179 // TODO: 在此添加控件通知处理程序代码180 181 m_list_text.DeleteString(0);182 183 184 185 //m_list_text.DeleteTempMap();186 }
由于之前测试的时候m_list_text.AddString()不是一个个显示的,所以在控制台下测试了一下,如下
1 #include<windows.h> 2 #include<iostream> 3 HANDLE hevent; //设置事件对象 4 HANDLE hmutex; //第二种方法,设置互斥事件对象 5 //CEvent event; 也可以用CEvent事件来操作,步骤和HANDLE差不多 6 7 int a=0; 8 9 //用事件操作临界区步骤: 10 //创建事件对象-->初始化时设置为无信号-->设置为有信号 11 //在线程中:等待事件-->设置为无信号事件-->进行操作-->设置为有信号事件 12 DWORD WINAPI Thread7(LPVOID lpParemt) 13 { 14 while (true) 15 { 16 17 WaitForSingleObject(hevent, INFINITE); //无限等待请求事件 18 ResetEvent(hevent); //设置为无信号事件 19 Sleep(100); 20 if (a < 20) 21 { 22 a++; 23 std::cout << "Thread7 is running and count= " << a << std::endl; 24 SetEvent(hevent); 25 } 26 else 27 { 28 SetEvent(hevent); 29 break; 30 } 31 } 32 return 0; 33 } 34 DWORD WINAPI Thread8(LPVOID lpParemt) 35 { 36 while (true) 37 { 38 39 WaitForSingleObject(hevent, INFINITE); //无限等待请求事件 40 ResetEvent(hevent); //设置为无信号事件 41 Sleep(100); 42 if (a < 20) 43 { 44 a++; 45 std::cout << "Thread8 is running and count= " << a << std::endl; 46 SetEvent(hevent); 47 } 48 else 49 { 50 SetEvent(hevent); 51 break; 52 } 53 } 54 return 0; 55 } 56 57 //线程9,10 使用互斥事件使线程同步 58 DWORD WINAPI Thread9(LPVOID lpParemt) 59 { 60 while (true) 61 { 62 WaitForSingleObject(hmutex, INFINITE); //请求互斥对象 63 if (a < 20) 64 { 65 a++; 66 Sleep(100); 67 std::cout << "mutex thread9 is running and the count=" << a << std::endl; 68 ReleaseMutex(hmutex); 69 } 70 else 71 { 72 break; 73 } 74 } 75 return 0; 76 } 77 78 DWORD WINAPI Thread10(LPVOID lpParemt) 79 { 80 while (true) 81 { 82 WaitForSingleObject(hmutex, INFINITE); //请求互斥对象 83 if (a < 20) 84 { 85 a++; 86 Sleep(100); 87 std::cout << "mutex thread10 is running and the count=" << a << std::endl; 88 ReleaseMutex(hmutex); 89 } 90 else 91 { 92 break; 93 } 94 } 95 return 0; 96 } 97 98 99 int main()100 {101 HANDLE H1, H2;102 hevent = CreateEvent(NULL, false, false, NULL); //无信号的自动重置事件,第二个参数设置事件自动还是手动,第三是有无信号参数103 SetEvent(hevent); //设置为有信号事件104 H1 = CreateThread(NULL, 0, Thread7, NULL, 0, NULL);105 H2 = CreateThread(NULL, 0, Thread8, NULL, 0, NULL);106 CloseHandle(H1);107 CloseHandle(H2);108 Sleep(3000);109 110 //使用互斥事件111 a = 0;112 HANDLE H3, H4;113 hmutex = CreateMutex(NULL, FALSE, NULL); //设置对象为未能取得事件所有权114 H3 = CreateThread(NULL, 0, Thread9, NULL, 0, NULL);115 H4 = CreateThread(NULL, 0, Thread10, NULL, 0, NULL);116 CloseHandle(H3);117 CloseHandle(H4);118 Sleep(3000);119 120 //进程间的通信121 122 123 return 0;124 }
运行结果如下:
进程间的通信(邮慒通信)
服务端
1 #include<iostream> 2 #include<windows.h> 3 int main() 4 { 5 HANDLE mail; 6 mail = CreateMailslot(L"\\\\.\\mailslot\\mysolt", 0, MAILSLOT_WAIT_FOREVER, NULL); 7 if (mail == INVALID_HANDLE_VALUE) 8 { 9 std::cout << "创建邮慒失败" << std::endl;10 CloseHandle(mail);11 }12 else13 {14 std::cout << "创建邮慒成功" << std::endl;15 char text[100];16 std::string str;17 DWORD readtext;18 while (true)19 {20 21 if (ReadFile(mail, text, 100, &readtext, 0))22 {23 std::cout << text << std::endl;24 }25 else26 {27 std::cout << "读取数据失败" << std::endl;28 }29 }30 }31 CloseHandle(mail);32 Sleep(2000);33 return 0;34 }
客户端
1 #include<iostream> 2 #include<windows.h> 3 int main() 4 { 5 HANDLE mail; 6 char text[] = "just a test"; 7 DWORD wirtetext; 8 mail = CreateFile(L"\\\\.\\mailslot\\mysolt", GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); 9 if (mail == INVALID_HANDLE_VALUE)10 {11 std::cout << "邮慒打开失败" << std::endl;12 }13 else14 {15 while (true)16 {17 std::cout << "write data" << std::endl;18 std::cin >> text;19 //std::cin.getline(text);20 if (WriteFile(mail, text, sizeof(text), &wirtetext, NULL))21 {22 std::cout << "邮慒写入数据成功" << std::endl;23 }24 else25 {26 std::cout << "邮慒写入数据失败" << std::endl;27 }28 }29 }30 Sleep(1000);31 return 0;32 }
同步线程和进程间的通信
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。