本文主要是介绍孙鑫 VC++深入详解第15课——多线程,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.简单的多线程实例
步骤:
①全局函数ThreadProc
②创建进程CreateThread
③关闭进程CloseHandle
④让主线程休眠 Sleep()
代码:
#include <iostream>
#include <stdlib.h>
#include <Windows.h>
using namespace std;DWORD WINAPI Fun1Proc(LPVOID lpParameter);
HANDLE hMetux;
void main()
{hMetux = CreateMutex(NULL,FALSE,NULL);HANDLE hThread1;hThread1 = CreateThread(NULL,0,Fun1Proc,NULL,0,NULL);CloseHandle(hThread1);cout<<"main thread is runing"<<endl;Sleep(1000);cin.get();cin.get();}DWORD WINAPI Fun1Proc(LPVOID lpParameter)
{cout<<"thread1 is running"<<endl;return 0;
}
2.多线程实现火车售票
①创建两个线程同时出售火车票
②线程中火车票 ticket>0则出售,否则结束
#include <Windows.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
DWORD WINAPI FunOneProc(LPVOID lpParam);
DWORD WINAPI FunTwoProc(LPVOID lpParam);
int ticket = 100;
int main()
{HANDLE hThreadOne;HANDLE hThreadTwo;hThreadOne = CreateThread(NULL,0,FunOneProc,NULL,0,NULL);hThreadTwo = CreateThread(NULL,0,FunTwoProc,NULL,0,NULL);CloseHandle(hThreadOne);CloseHandle(hThreadTwo);Sleep(4000);cin.get();cin.get();return 0;
}DWORD WINAPI FunOneProc(LPVOID lpParam)
{while (TRUE){if (ticket > 0){cout<<"One Thread tickets:"<<ticket--<<endl;} else{break;}}return 0;
}DWORD WINAPI FunTwoProc(LPVOID lpParam)
{while (TRUE){if (ticket > 0){cout<<"Two Thread tickets:"<<ticket--<<endl;} else{break;} }return 0;
}
运行结果:
结果分析:
开始看到这个结果的时候,我大惊了一下。想着10099,9897这么大的数字从何而来,我的设置ticket =100。想了一番,这么大的数字应该分开看 10099 为100,99 ;9897为98,97.
为什么会这样呢。孙鑫老师的书里的运行结果也没有这么奇葩。我自己想了一下,孙鑫老师电脑估计比较老土配置不高,是单核的cpu。而我因为游戏时间比较多,买了双核的cpu,并行性高。当进程One在运行时输出One Thread tickets:,恰好到了进程Two,然后输出:Two Thread tickets。然后又到了进程One,输出100,进程Two输出99。所以最后结果就呈现上面的奇葩了。`(*∩_∩*)′
3.利用互斥对象实现同步
①声明全局互斥变量:HANDLE hMutex
②创建互斥对象:CreateMutex
③在保护代码前加:WaitForSingleObject
④保护代码后加:ReleaseMutex
注意事项:创建的互斥对象应该在创建进程前面
#include <Windows.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
DWORD WINAPI FunOneProc(LPVOID lpParam);
DWORD WINAPI FunTwoProc(LPVOID lpParam);
int ticket = 100;
HANDLE hMutex;
int main()
{HANDLE hThreadOne;HANDLE hThreadTwo;hMutex = CreateMutex(NULL,FALSE,NULL);hThreadOne = CreateThread(NULL,0,FunOneProc,NULL,0,NULL);hThreadTwo = CreateThread(NULL,0,FunTwoProc,NULL,0,NULL);CloseHandle(hThreadOne);CloseHandle(hThreadTwo);Sleep(4000);cin.get();cin.get();return 0;
}DWORD WINAPI FunOneProc(LPVOID lpParam)
{while (TRUE){WaitForSingleObject(hMutex,INFINITE);if (ticket > 0){cout<<"One Thread tickets:"<<ticket--<<endl;} else{break;}ReleaseMutex(hMutex);}return 0;
}DWORD WINAPI FunTwoProc(LPVOID lpParam)
{while (TRUE){WaitForSingleObject(hMutex,INFINITE);if (ticket > 0){cout<<"Two Thread tickets:"<<ticket--<<endl;} else{break;} ReleaseMutex(hMutex);}return 0;
}
运行结果:
4.多线程实现Chat聊天室
实现步骤:
1.加载套接字库
①在 stdafx.h 文件中 添加 #include <afxsock.h>// MFC 套接字扩展
②在App类中的InitInstance函数中添加如下代码:
if (!AfxSocketInit())
{AfxMessageBox(IDP_SOCKETS_INIT_FAILED);return FALSE;
}
2.初始化套接字绑定IP和端口号
①在OnInitDialog函数中添加自定义函数InitSocket;
②InitSocket函数的实现
BOOL CUdpSrvDlg::InitSocket()
{m_socket = socket(AF_INET,SOCK_DGRAM,0);if (INVALID_SOCKET == m_socket){AfxMessageBox(_T("套接字创建失败!"));return FALSE;}SOCKADDR_IN sockAddr;sockAddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);sockAddr.sin_family = AF_INET;sockAddr.sin_port = htons(6000);//绑定套接字int retval = bind(m_socket,(SOCKADDR*)&sockAddr,sizeof(SOCKADDR));if (SOCKET_ERROR == retval){closesocket(m_socket);AfxMessageBox(_T("绑定套接字失败"));return FALSE;}return TRUE;
}
3.用多线程实现接收端功能
①定义结构体:RECVPARAM
struct RECVPARAM
{HWND hwnd;//对话框句柄SOCKET socket;//已创建的套接字
};
②创建接收线程 hThread
InitSocket(); RECVPARAM *pRecvParam = new RECVPARAM;pRecvParam->hwnd = m_hWnd;pRecvParam->socket = m_socket;//创建接收线程HANDLE hThread = CreateThread(NULL,0,RecvProc,(LPVOID)pRecvParam,0,NULL);//关闭该接收进程句柄,释放其引用计数CloseHandle(hThread);
③实现接收线程函数:RecvPRoc
在类内定义,必须使用静态变量
static DWORD WINAPI RecvProc(LPVOID lpParam);
函数实现:
DWORD WINAPI CUdpSrvDlg::RecvProc(LPVOID lpParam)
{//获取主线程传递的套接字和窗口句柄SOCKET sock = ((RECVPARAM*)lpParam)->socket;HWND hwnd = ((RECVPARAM*)lpParam)->hwnd;delete lpParam;SOCKADDR_IN addrFrom;int len = sizeof(SOCKADDR);char recvBuf[200];char tempBuf[300];int retval;while (TRUE){//接收数据retval = recvfrom(sock,recvBuf,200,0,(SOCKADDR*)&addrFrom,&len);if (SOCKET_ERROR == retval){break;}sprintf_s(tempBuf,300,"%s say: %s",inet_ntoa(addrFrom.sin_addr),recvBuf);::PostMessage(hwnd,WM_RECVDATA,0,(LPARAM)tempBuf);}return 0;
}
4.将接收的信息显示在窗口上
①自定义消息宏
#define WM_RECVDATA WM_USER +1
②设置消息响应函数原型的声明
afx_msg LRESULT OnRecvData(WPARAM wParam,LPARAM lParam);
③添加消息映射
ON_MESSAGE(WM_RECVDATA,OnRecvData)
④添加消息函数的定义
LRESULT CUdpSrvDlg::OnRecvData(WPARAM wParam,LPARAM lParam)
{//取出接收到的数据CString str = (char*)lParam;CString strTemp;//获得已有数据GetDlgItemText(IDC_EDIT_RECV,strTemp);str += "\r\n";str += strTemp;//显示所有接收到的数据SetDlgItemText(IDC_EDIT_RECV,str);return 0;
}
5.设置发送端的信息
实现函数:
void CUdpSrvDlg::OnBnClickedBtnSend()
{// TODO: 在此添加控件通知处理程序代码//获取对方IPDWORD dwIP;((CIPAddressCtrl*)GetDlgItem(IDC_IPADDRESS1))->GetAddress(dwIP);SOCKADDR_IN addrTo;addrTo.sin_addr.S_un.S_addr = htonl(dwIP);addrTo.sin_family = AF_INET;addrTo.sin_port = htons(6000);CString strSend;//获得待发送数据GetDlgItemText(IDC_EDIT_SEND,strSend);//发送数据sendto(m_socket,strSend,strSend.GetLength()+1,0,(SOCKADDR*)&addrTo,sizeof(SOCKADDR));//清空发送编辑框中的内容SetDlgItemText(IDC_EDIT_SEND,"");
}
6.小技巧:
①EDIT控件 MultiLine设置多行显示。
②default button设置TRUE,这是响应ENTER键。
这篇关于孙鑫 VC++深入详解第15课——多线程的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!