文章目录
- 进程通信
- 邮槽
- 剪切板
- 线程同步
- 原子操作
- 临界区
- 事件
- 信号量
- 互斥体
进程通信
邮槽
进程一:
// Process1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include <Windows.h>
int main()
{
//创建邮槽
HANDLE hMailslot = CreateMailslot(
L"\\\\.\\mailslot\\Communication", //mailslot的名称,需要符合一定的格式
0, //指定可以写入mailslot的单个邮件的最大大小
MAILSLOT_WAIT_FOREVER, //等待时间
NULL //安全属性
);
char szBuffer[MAX_PATH] = { 0 };
DWORD dwReadSize = 0;
if (!ReadFile(hMailslot, szBuffer, MAX_PATH, &dwReadSize, NULL)) {
CloseHandle(hMailslot);
return -1;
}
else {
std::cout << szBuffer << std::endl;
CloseHandle(hMailslot);
}
system("pause");
return -1;
}
进程二:
// Process2.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include <Windows.h>
int main()
{
char szBuffer[] = "Hello";
HANDLE hMailslot = CreateFile(L"\\\\.\\mailslot\\Communication", GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL, NULL);
DWORD dwWriteSize = 0;
WriteFile(hMailslot, szBuffer, strlen(szBuffer) + 1, &dwWriteSize, NULL);
CloseHandle(hMailslot);
system("pause");
return 0;
}
通信测试:
先运行进程一,再运行进程二
通信成功:
剪切板
进程一:
// Process1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <Windows.h>
int main()
{
const char* pStr = "This is a string";
//打开剪切板进行检查
if (OpenClipboard(NULL)) {
//清空剪切板并释放剪切板中的数据句柄
EmptyClipboard();
//全局内存块句柄
//从堆中分配指定字节数
HGLOBAL hGlobalClipboard = GlobalAlloc(GHND, strlen(pStr) + 1);
//锁定全局对下那个并放回该对象内存块的第一个字节指针
char* szBuffer = NULL;
szBuffer = (char*)GlobalLock(hGlobalClipboard);
strcpy(szBuffer, pStr);
//取消锁定
GlobalUnlock(hGlobalClipboard);
//以指定的剪贴板格式将数据放在剪贴板上
SetClipboardData(CF_TEXT, hGlobalClipboard);
CloseClipboard();
}
system("pause");
return 0;
}
进程二:
// Process2.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include <Windows.h>
int main()
{
if (OpenClipboard(NULL)) {
if (IsClipboardFormatAvailable(CF_TEXT)) {
HGLOBAL hGlobalClip = GetClipboardData(CF_TEXT);
char* szBuffer = NULL;
szBuffer = (char*)GlobalLock(hGlobalClip);
GlobalUnlock(hGlobalClip);
std::cout << szBuffer << std::endl;
CloseHandle(hGlobalClip);
}
}
system("pause");
return 0;
}
通信测试:
先运行进程一,在运行进程二
通信成功:
线程同步
线程同步之前发布过两篇文章讲解了,都是非常详细,这里可以直接移步【Windows线程开发】Windows线程同步技术(这一篇当时荣登微软技术领域榜top18),【线程安全问题】线程互斥与线程同步技术。
#include <iostream>
#include <windows.h>
int g_value = 0;
DWORD ThreadCallBack(LPVOID lpThreadParameter){
for(int i = 0;i<200000;i++){
g_value++;
}
return 0;
}
DWORD ThreadCallBack1(LPVOID lpThreadParameter){
for(int i = 0;i<200000;i++){
g_value++;
}
return 0;
}
int main(){
HANDLE hThread1 = CreateThead(NULL,NULL,(LPTHREAD_START_ROUTINE)ThreadCallBack,NULL,NULL);
HANDLE hThread1 = CreateThead(NULL,NULL,(LPTHREAD_START_ROUTINE)ThreadCallBack1,NULL,NULL);
WaitSingleObject(hThread1,INFNITE);
WaitSingleObject(hThread2,INFNITE);
std::cout<<g_value<<std:endl;
system("pause"):
return 0;
}
原子操作
InterlockesAdd((LONG volatile*)&g_value,1);
InterlockedExchangeAdd((volatile long*)&g_value,1);
临界区
#include <iostream>
#include <windows.h>
int g_value = 0;
CRITICAL_SECTION g_Cs;
DWORD ThreadCallBack(LPVOID lpThreadParameter){
EnterCricalSection(g_Cs);
for(int i = 0;i<200000;i++){
g_value++;
}
LeaveCriticalSection(&g_Cs);
return 0;
}
DWORD ThreadCallBack1(LPVOID lpThreadParameter){
EnterCricalSection(g_Cs);
for(int i = 0;i<200000;i++){
g_value++;
}
LeaveCriticalSection(&g_Cs);
return 0;
}
int main(){
InitializeCriticalSection(&g_Cs);
HANDLE hThread1 = CreateThead(NULL,NULL,(LPTHREAD_START_ROUTINE)ThreadCallBack,NULL,NULL);
HANDLE hThread1 = CreateThead(NULL,NULL,(LPTHREAD_START_ROUTINE)ThreadCallBack1,NULL,NULL);
WaitSingleObject(hThread1,INFNITE);
WaitSingleObject(hThread2,INFNITE);
std::cout<<g_value<<std:endl;
DeleteCriticalSection(&g_Cs);
system("pause"):
return 0;
}
事件
#include <iostream>
#include <windows.h>
int g_value = 0;
HANDLE hEvent;
DWORD ThreadCallBack(LPVOID lpThreadParameter){
WatiForSingleObject(g_Event,INFINITE);
ResetEvent(g_ESvent);
for(int i = 0;i<200000;i++){
g_value++;
}
SetEVent(g_Event);
return 0;
}
DWORD ThreadCallBack1(LPVOID lpThreadParameter){
WatiForSingleObject(g_Event,INFINITE);
ResetEvent(g_ESvent);
for(int i = 0;i<200000;i++){
g_value++;
}
SetEVent(g_Event);
return 0;
}
int main(){
g_Event = CreateEvent(NULL,TRUE,TRUE,L"Event1");
HANDLE hThread1 = CreateThead(NULL,NULL,(LPTHREAD_START_ROUTINE)ThreadCallBack,NULL,NULL);
HANDLE hThread1 = CreateThead(NULL,NULL,(LPTHREAD_START_ROUTINE)ThreadCallBack1,NULL,NULL);
WaitSingleObject(hThread1,INFNITE);
WaitSingleObject(hThread2,INFNITE);
std::cout<<g_value<<std:endl;
system("pause"):
return 0;
}
信号量
#include <iostream>
#include <windows.h>
int g_value = 0;
HANDLE g_Semaphore;
DWORD ThreadCallBack(LPVOID lpThreadParameter){
WaitForSingleObjectEx(g_Semaphore,INFINITE,TRUE);
for(int i = 0;i<200000;i++){
g_value++;
}
ReleaseSemaphore(g_Semaphore,1,NULL);
return 0;
}
DWORD ThreadCallBack1(LPVOID lpThreadParameter){
WaitForSingleObjectEx(g_Semaphore,INFINITE,TRUE);
for(int i = 0;i<200000;i++){
g_value++;
}
ReleaseSemaphore(g_Semaphore,1,NULL);
return 0;
}
int main(){
g_Semephore = CreateSemaphore(NULL,1,2,L"RSemaphore");
HANDLE hThread1 = CreateThead(NULL,NULL,(LPTHREAD_START_ROUTINE)ThreadCallBack,NULL,NULL);
HANDLE hThread1 = CreateThead(NULL,NULL,(LPTHREAD_START_ROUTINE)ThreadCallBack1,NULL,NULL);
WaitSingleObject(hThread1,INFNITE);
WaitSingleObject(hThread2,INFNITE);
std::cout<<g_value<<std:endl;
system("pause"):
return 0;
}
互斥体
#include <iostream>
#include <windows.h>
int g_value = 0;
HANDLE g_Mutex;
DWORD ThreadCallBack(LPVOID lpThreadParameter){
WaitForSingleObjectEx(g_Mutex,INFINITE,TRUE);
for(int i = 0;i<200000;i++){
g_value++;
}
ReleaseMutex(g_Mutex);
return 0;
}
DWORD ThreadCallBack1(LPVOID lpThreadParameter){
WaitForSingleObjectEx(g_Mutex,INFINITE,TRUE);
for(int i = 0;i<200000;i++){
g_value++;
}
ReleaseMutex(g_Mutex);
return 0;
}
int main(){
g_Mutex = CreateMutex(NULL,FALSE,L"Mutex");
HANDLE hThread1 = CreateThead(NULL,NULL,(LPTHREAD_START_ROUTINE)ThreadCallBack,NULL,NULL);
HANDLE hThread1 = CreateThead(NULL,NULL,(LPTHREAD_START_ROUTINE)ThreadCallBack1,NULL,NULL);
WaitSingleObject(hThread1,INFNITE);
WaitSingleObject(hThread2,INFNITE);
std::cout<<g_value<<std:endl;
system("pause"):
return 0;
}