Skip to content

Instantly share code, notes, and snippets.

@bzoz
Created May 13, 2020 16:02
Show Gist options
  • Save bzoz/52b1d45ce6a27418f31d58956e72e4c0 to your computer and use it in GitHub Desktop.
Save bzoz/52b1d45ce6a27418f31d58956e72e4c0 to your computer and use it in GitHub Desktop.
#include <windows.h>
#include <stdio.h>
#include <string.h>
#define BUF_SIZE (128*1024)
DWORD __stdcall thread_proc(void* num);
HANDLE signal_event;
int main() {
LARGE_INTEGER prev, next;
BOOL r;
QueryPerformanceCounter(&prev);
QueryPerformanceFrequency(&next);
printf("Initial: %lld, freq: %lld\n", prev.QuadPart, next.QuadPart);
signal_event = CreateEvent(NULL, FALSE, FALSE, NULL);
for (int i = 0; i<64; ++i) {
CreateThread(NULL, 0, thread_proc, (void*)i, 0, NULL);
}
for (unsigned tick = 0;;++tick) {
WaitForSingleObject(signal_event, INFINITE);
r = QueryPerformanceCounter(&next);
if (next.QuadPart < prev.QuadPart) {
printf("%d %lld %lld\n", r, next.QuadPart, prev.QuadPart);
}
if (tick % 65536 == 0) {
printf("still running..\n");
}
prev = next;
}
}
DWORD __stdcall thread_proc(void* num) {
char file_name[256];
char buffer[BUF_SIZE];
memset(buffer, 'z', BUF_SIZE);
sprintf(file_name, "file%d", (int)num);
for(;;) {
HANDLE file = CreateFile(file_name,
GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
DWORD total_bytes = 0, bytes = 0;
while (total_bytes < BUF_SIZE) {
WriteFile(file, buffer + total_bytes, BUF_SIZE - total_bytes, &bytes, NULL);
total_bytes += bytes;
}
CloseHandle(file);
SetEvent(signal_event);
file = CreateFile(file_name,
GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
total_bytes = 0;
bytes = 0;
while (total_bytes < BUF_SIZE) {
ReadFile(file, buffer + total_bytes, BUF_SIZE - total_bytes, &bytes, NULL);
total_bytes += bytes;
}
CloseHandle(file);
SetEvent(signal_event);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment