为什么 .Net 代码版本挂起,但 C++ 没有?
Why .Net code version hangs, but C++ not?
为什么相同的代码在 .Net (C#) 中会挂起,而在 C++ 中却不会?真正的问题出在 System.IO.FileStream,但我将其缩减为 CreateFile,但它仍然挂在 .Net 中(虽然几乎 100% 类似于 C++ 代码):
using System;
using System.Runtime.InteropServices;
using System.Threading;
namespace ConsoleApplication1
{
class Program
{
static IntPtr _handle;
static void Main()
{
_handle = CreateFile("test.txt", GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero,
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, IntPtr.Zero);
new Thread(MyProc) { IsBackground = true }.Start();
new Thread(MyProc) { IsBackground = true }.Start();
Console.ReadKey();
}
static void MyProc()
{
for (int i = 0; i < int.MaxValue; i++)
{
Console.WriteLine(i);
var _overlapped = new NativeOverlapped();
LockFileEx(_handle, LOCKFILE_EXCLUSIVE_LOCK, 0, int.MaxValue, int.MaxValue, ref _overlapped);
Thread.Sleep(50);
UnlockFileEx(_handle, 0, int.MaxValue, int.MaxValue, ref _overlapped);
}
}
public const int LOCKFILE_EXCLUSIVE_LOCK = 0x00000002;
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern bool LockFileEx(IntPtr hFile, int flags, int reserved, int numberOfBytesToLockLow, int numberOfBytesToLockHigh,
ref NativeOverlapped overlapped);
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern bool UnlockFileEx(IntPtr hFile, int reserved, int numberOfBytesToUnlockLow, int numberOfBytesToUnlockHigh,
ref NativeOverlapped overlapped);
public const int GENERIC_READ = unchecked((int)0x80000000L);
public const int GENERIC_WRITE = (int)0x40000000L;
public const int OPEN_ALWAYS = 4;
public const int FILE_SHARE_READ = 0x00000001;
public const int FILE_SHARE_WRITE = 0x00000002;
public const int FILE_SHARE_DELETE = 0x00000004;
public const int FILE_ATTRIBUTE_NORMAL = 0x00000080;
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern IntPtr CreateFile(string fileName, int desiredAccess, int shareMode,
IntPtr pSecurityAttributes, int creationDisposition, int flagsAndAttributes, IntPtr hTemplateFile);
}
}
这里是不挂的C++版本:
#include "stdafx.h"
#include <thread>
#include <Windows.h>
HANDLE hFile;
DWORD WINAPI MyProc(LPVOID lpParam);
int _tmain(int argc, _TCHAR* argv[])
{
hFile = CreateFile(L"test.txt", GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
HANDLE hThread1 = CreateThread(NULL, 0, MyProc, NULL, 0, NULL);
HANDLE hThread2 = CreateThread(NULL, 0, MyProc, NULL, 0, NULL);
int ch = getchar();
return 0;
}
DWORD WINAPI MyProc(LPVOID lpParam)
{
for(int i = 0; i < INT_MAX; i++)
{
printf("%d\r\n", i);
OVERLAPPED overlapped = {0};
LockFileEx(hFile, LOCKFILE_EXCLUSIVE_LOCK, 0, INT_MAX, INT_MAX, &overlapped);
Sleep(50);
UnlockFileEx(hFile, 0, INT_MAX, INT_MAX, &overlapped);
}
return 0;
}
在同一个文件句柄上用 LOCKFILE_EXCLUSIVE_LOCK 调用 LockFileEx 两次似乎是无效的。它必须是不同的文件句柄或句柄的某种进程内同步。
令人不安的是,MSDN 文档中的问题没有明确说明。并且在运行时没有明确的错误信息。更重要的是,在我看来,这个问题只在我的电脑上重现。这有点丑。
为什么相同的代码在 .Net (C#) 中会挂起,而在 C++ 中却不会?真正的问题出在 System.IO.FileStream,但我将其缩减为 CreateFile,但它仍然挂在 .Net 中(虽然几乎 100% 类似于 C++ 代码):
using System;
using System.Runtime.InteropServices;
using System.Threading;
namespace ConsoleApplication1
{
class Program
{
static IntPtr _handle;
static void Main()
{
_handle = CreateFile("test.txt", GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero,
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, IntPtr.Zero);
new Thread(MyProc) { IsBackground = true }.Start();
new Thread(MyProc) { IsBackground = true }.Start();
Console.ReadKey();
}
static void MyProc()
{
for (int i = 0; i < int.MaxValue; i++)
{
Console.WriteLine(i);
var _overlapped = new NativeOverlapped();
LockFileEx(_handle, LOCKFILE_EXCLUSIVE_LOCK, 0, int.MaxValue, int.MaxValue, ref _overlapped);
Thread.Sleep(50);
UnlockFileEx(_handle, 0, int.MaxValue, int.MaxValue, ref _overlapped);
}
}
public const int LOCKFILE_EXCLUSIVE_LOCK = 0x00000002;
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern bool LockFileEx(IntPtr hFile, int flags, int reserved, int numberOfBytesToLockLow, int numberOfBytesToLockHigh,
ref NativeOverlapped overlapped);
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern bool UnlockFileEx(IntPtr hFile, int reserved, int numberOfBytesToUnlockLow, int numberOfBytesToUnlockHigh,
ref NativeOverlapped overlapped);
public const int GENERIC_READ = unchecked((int)0x80000000L);
public const int GENERIC_WRITE = (int)0x40000000L;
public const int OPEN_ALWAYS = 4;
public const int FILE_SHARE_READ = 0x00000001;
public const int FILE_SHARE_WRITE = 0x00000002;
public const int FILE_SHARE_DELETE = 0x00000004;
public const int FILE_ATTRIBUTE_NORMAL = 0x00000080;
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern IntPtr CreateFile(string fileName, int desiredAccess, int shareMode,
IntPtr pSecurityAttributes, int creationDisposition, int flagsAndAttributes, IntPtr hTemplateFile);
}
}
这里是不挂的C++版本:
#include "stdafx.h"
#include <thread>
#include <Windows.h>
HANDLE hFile;
DWORD WINAPI MyProc(LPVOID lpParam);
int _tmain(int argc, _TCHAR* argv[])
{
hFile = CreateFile(L"test.txt", GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
HANDLE hThread1 = CreateThread(NULL, 0, MyProc, NULL, 0, NULL);
HANDLE hThread2 = CreateThread(NULL, 0, MyProc, NULL, 0, NULL);
int ch = getchar();
return 0;
}
DWORD WINAPI MyProc(LPVOID lpParam)
{
for(int i = 0; i < INT_MAX; i++)
{
printf("%d\r\n", i);
OVERLAPPED overlapped = {0};
LockFileEx(hFile, LOCKFILE_EXCLUSIVE_LOCK, 0, INT_MAX, INT_MAX, &overlapped);
Sleep(50);
UnlockFileEx(hFile, 0, INT_MAX, INT_MAX, &overlapped);
}
return 0;
}
在同一个文件句柄上用 LOCKFILE_EXCLUSIVE_LOCK 调用 LockFileEx 两次似乎是无效的。它必须是不同的文件句柄或句柄的某种进程内同步。
令人不安的是,MSDN 文档中的问题没有明确说明。并且在运行时没有明确的错误信息。更重要的是,在我看来,这个问题只在我的电脑上重现。这有点丑。