吃字母------线程同步与互斥的学习

tech2022-07-31  158

线程的同步与互斥的实战----吃字母 希望能给你一些启发: 我们在资源输入数据,下面的两个缓冲区,通过线程获取资源的数据,吃货A-D 抢 两个缓冲区的数据 并放入对应的数据栏中。 例如: 解决思想和方案: 缓冲区的数据通过一个线程去资源栏中获取,这一个线程与下面4个吃货的线程是同步的关系, 下面4个线程是互斥的。

下面是代码: // 9_2实战_吃字母.cpp :

// An highlighted block // 9_2实战_吃字母.cpp : Defines the entry point for the application. // #include "stdafx.h" #include <stdio.h> #include <windows.h> #include "resource.h" //资源文件,需要包含,否则将找不到窗口的控制序号:IDC_EDIT1 HWND hEdit1; //这里是 资源栏 //这个7个窗口句柄就是 7个输入栏 依次排列 HWND hEdit2; //缓冲区1 HWND hEdit3;//缓冲区2 HWND hEdit4;//吃货A-D HWND hEdit5; HWND hEdit6; HWND hEdit7; HANDLE hSemaphore2; //信号量 HANDLE hEvent1; //事件1 HANDLE hEvent2; //事件2 HANDLE Thread; //这个是写入缓冲区的线程 HANDLE hEat[4];//吃字母4个线程 DWORD len=0;//资源的长度 TCHAR szBuffer[100]={0}; //存放资源栏的缓冲区 TCHAR szBuff[2]={0}; //存放两个缓冲区数据 #include <stdlib.h>//随机函数 所包含的 头文件 #include <time.h>//随机函数 BOOL E1=FALSE; //用于标记 吃货线程从哪个缓冲区读取数据 如果为TRUE 就从hEdit2获取,反之hEdit3 DWORD WINAPI ThreadProc1(LPVOID lpParemeter) //往两个缓冲区写入数据的线程函数 Thread { DWORD X=0; while(szBuffer) { TCHAR temp[1]; memset(szBuff,0,2); WaitForSingleObject(hEvent1,INFINITE); len = strlen(szBuffer); szBuff[0] = szBuffer[len-1]; GetWindowText(hEdit2,temp,1); X = rand()%2;// 随机获取0,1 根据0,1来选择将数据写入到哪个缓冲区里 if(X) { SetWindowText(hEdit2,szBuff); E1=TRUE; } else { SetWindowText(hEdit3,szBuff); E1=FALSE; } szBuffer[len-1]='\0'; //写完后,将长度减1 ReleaseSemaphore(hSemaphore2,2,NULL); SetEvent(hEvent2);//唤醒 吃货线程抢 执行权 Sleep(500); } return 0; } DWORD WINAPI ThreadProc3(LPVOID lpParemeter) { TCHAR szbuffer1[3]={0};//用于获取缓冲区的数据,大于2即可 TCHAR tempBuff[20]={0};//将 之前的数据取出来,加上每次从缓冲区获取的数据追记起来,再最后写入到hEditX 当中 SetWindowText(hEdit4," "); while(len>0) { WaitForSingleObject(hEvent2,INFINITE); WaitForSingleObject(hSemaphore2,INFINITE); if(E1) { GetWindowText(hEdit2,szbuffer1,3); E1=FALSE; } else { GetWindowText(hEdit3,szbuffer1,3); E1=TRUE; } GetWindowText(hEdit4,tempBuff,20); szbuffer1[1]='-'; strcat(tempBuff,szbuffer1); SetWindowText(hEdit4,tempBuff); memset(szbuffer1,0,3); memset(tempBuff,0,20); if(len<0||len==0) { ReleaseSemaphore(hSemaphore2,0,NULL); SetWindowText(hEdit2,TEXT("0")); SetWindowText(hEdit3,TEXT("0")); Sleep(1000); } else { ReleaseSemaphore(hSemaphore2,1,NULL); SetEvent(hEvent1); Sleep(1000); } } return 0; } DWORD WINAPI ThreadProc4(LPVOID lpParemeter) { TCHAR szbuffer1[3]={0}; TCHAR tempBuff[20]={0}; SetWindowText(hEdit5," "); while(len>0) { WaitForSingleObject(hEvent2,INFINITE); WaitForSingleObject(hSemaphore2,INFINITE); if(E1) { GetWindowText(hEdit2,szbuffer1,3); E1=FALSE; } else { GetWindowText(hEdit3,szbuffer1,3); E1=TRUE; } GetWindowText(hEdit5,tempBuff,20); szbuffer1[1]='-'; strcat(tempBuff,szbuffer1); SetWindowText(hEdit5,tempBuff); memset(szbuffer1,0,3); memset(tempBuff,0,20); if(len<0||len==0) { ReleaseSemaphore(hSemaphore2,0,NULL); SetWindowText(hEdit2,TEXT("0")); SetWindowText(hEdit3,TEXT("0")); Sleep(1000); } else { ReleaseSemaphore(hSemaphore2,1,NULL); SetEvent(hEvent1); Sleep(1000); } } return 0; } DWORD WINAPI ThreadProc5(LPVOID lpParemeter) { TCHAR szbuffer1[3]={0}; TCHAR tempBuff[20]={0}; SetWindowText(hEdit6," "); BOOL fg=FALSE; while(len>0) { WaitForSingleObject(hEvent2,INFINITE); WaitForSingleObject(hSemaphore2,INFINITE); if(E1) { GetWindowText(hEdit2,szbuffer1,3); E1=FALSE; } else { GetWindowText(hEdit3,szbuffer1,3); E1=TRUE; } GetWindowText(hEdit6,tempBuff,20); szbuffer1[1]='-'; strcat(tempBuff,szbuffer1); SetWindowText(hEdit6,tempBuff); memset(szbuffer1,0,3); memset(tempBuff,0,20); if(len<0||len==0) { ReleaseSemaphore(hSemaphore2,0,NULL); SetWindowText(hEdit2,TEXT("0")); SetWindowText(hEdit3,TEXT("0")); Sleep(1000); } else { ReleaseSemaphore(hSemaphore2,1,NULL); SetEvent(hEvent1); Sleep(1000); } } return 0; } DWORD WINAPI ThreadProc6(LPVOID lpParemeter) { TCHAR szbuffer1[3]={0}; TCHAR tempBuff[20]={0}; SetWindowText(hEdit7," "); BOOL fg=FALSE; while(len>0) { WaitForSingleObject(hEvent2,INFINITE); WaitForSingleObject(hSemaphore2,INFINITE); if(E1) { GetWindowText(hEdit2,szbuffer1,3); E1=FALSE; } else { GetWindowText(hEdit3,szbuffer1,3); E1=TRUE; } GetWindowText(hEdit7,tempBuff,20); szbuffer1[1]='-'; strcat(tempBuff,szbuffer1); SetWindowText(hEdit7,tempBuff); memset(szbuffer1,0,3); memset(tempBuff,0,20); if(len<0||len==0) { ReleaseSemaphore(hSemaphore2,0,NULL); SetWindowText(hEdit2,TEXT("0")); SetWindowText(hEdit3,TEXT("0")); Sleep(1000); } else { ReleaseSemaphore(hSemaphore2,1,NULL); SetEvent(hEvent1); Sleep(1000); } } return 0; } DWORD WINAPI ThreadMain(LPVOID lpParemeter) { GetWindowText(hEdit1,szBuffer,100); hSemaphore2 = CreateSemaphore(NULL,0,2,NULL); hEvent1 = CreateEvent(NULL,FALSE,TRUE,NULL); hEvent2 = CreateEvent(NULL,FALSE,FALSE,NULL); Thread = CreateThread(NULL,0,ThreadProc1,NULL,0,NULL); hEat[0] = CreateThread(NULL,0,ThreadProc3,NULL,0,NULL); hEat[1] = CreateThread(NULL,0,ThreadProc4,NULL,0,NULL); hEat[2] = CreateThread(NULL,0,ThreadProc5,NULL,0,NULL); hEat[3] = CreateThread(NULL,0,ThreadProc6,NULL,0,NULL); WaitForMultipleObjects(5,hEat,TRUE,INFINITE); CloseHandle(Thread); CloseHandle(hEat[0]); CloseHandle(hEat[1]); CloseHandle(hEat[2]); CloseHandle(hEat[3]); CloseHandle(hEvent1); CloseHandle(hEvent2); CloseHandle(hSemaphore2); return 0; } BOOL CALLBACK MainDlgProc(HWND hDlg,UINT uMsg,WPARAM wParam,LPARAM lParam) { BOOL bRet = FALSE; switch(uMsg) { case WM_CLOSE: EndDialog(hDlg,0); break; case WM_INITDIALOG: { hEdit1 = GetDlgItem(hDlg,IDC_EDIT1); hEdit2 = GetDlgItem(hDlg,IDC_EDIT2); hEdit3 = GetDlgItem(hDlg,IDC_EDIT3); hEdit4 = GetDlgItem(hDlg,IDC_EDIT4); hEdit5 = GetDlgItem(hDlg,IDC_EDIT5); hEdit6 = GetDlgItem(hDlg,IDC_EDIT6); hEdit7 = GetDlgItem(hDlg,IDC_EDIT7); SetWindowText(hEdit1,TEXT("0")); SetWindowText(hEdit2,TEXT("0")); SetWindowText(hEdit3,TEXT("0")); SetWindowText(hEdit4,TEXT("0")); SetWindowText(hEdit5,TEXT("0")); SetWindowText(hEdit6,TEXT("0")); SetWindowText(hEdit7,TEXT("0")); break; } case WM_COMMAND: switch(LOWORD(wParam)) { case IDC_BUTTON_BEGIN: { ::CreateThread(NULL,0,ThreadMain,NULL,0,NULL); return TRUE; } } break; } return bRet; } int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { // TODO: Place code here. srand((unsigned int)time(NULL)); DialogBox(hInstance,MAKEINTRESOURCE(IDD_DIALOG_MAIN),NULL,MainDlgProc); return 0; }

例子1: 例子2:

最新回复(0)