开2个线程, 写同一个文件, 怎么做?

xili 2005-09-01 04:57:14
开2个线程, 写同一个文件, 怎么做?
写的是不同的部分,无需担心互相覆盖.
有没有例子可以看看?
...全文
206 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
HaoyuTan 2005-09-02
  • 打赏
  • 举报
回复
// Create the I/O completion port.
hIOCompletionPort = CreateIoCompletionPort(hSourceFile,
NULL, // no existing I/O completion port
KEY, // key received in I/O completion packet
dwNumProcessors); // max worker threads

if (hIOCompletionPort == NULL)
{
fprintf(stderr,
"%s: Failed to create IO completion port (error %d)\n",
argv[0],
dwExitStatus = GetLastError());
goto EXIT;
}

// Initialize the file pointer for the overlapped structure
readPointer.LowPart = readPointer.HighPart = 0;

dwStartTime = GetTickCount();

// Start worker threads.
for (i=0; i<2*dwNumProcessors; i++)
{
hThread[i]=CreateThread(NULL,
0, // default stack size
(LPTHREAD_START_ROUTINE) readAndAnalyzeChunk,
(LPVOID) &fileSize,
0, // run immediately
&dwThreadId);
if (hThread[i] == NULL)
{
fprintf(stderr, "%s: Failed to create thread #%d (error %d)\n",
argv[0], i, dwExitStatus=GetLastError());
goto EXIT;
}
}
// Post the kickoff event.
PostQueuedCompletionStatus(hIOCompletionPort,
0,
KICKOFFKEY,
&kickoffOverlapped);

// Wait for a worker thread to terminate.
dwStatus = WaitForMultipleObjects(2*dwNumProcessors,
hThread,
FALSE,
INFINITE);
if (dwStatus == WAIT_FAILED)
{
fprintf(stderr, "%s: Wait failed (error %d)\n", argv[0],
dwExitStatus=GetLastError());
goto EXIT;
}
// Worker thread returned; send message to threads to exit.
for (i=0; i<2*dwNumProcessors-1; i++)
{
PostQueuedCompletionStatus(hIOCompletionPort,
0,
EXITKEY,
&dieOverlapped);
}
// Wait for threads to finish their work and terminate.
dwStatus = WaitForMultipleObjects(2*dwNumProcessors,
hThread,
TRUE,
INFINITE);

if (dwStatus == WAIT_FAILED)
{
fprintf(stderr, "%s: Wait failed (error %d)\n",
argv[0], dwExitStatus=GetLastError());
goto EXIT;
}
dwEndTime = GetTickCount();
printf( "\n\n%d bytes analyzed in %.3f seconds\n", fileSize.LowPart,
(float)(dwEndTime-dwStartTime)/1000.0);
printf( "%.2f MB/sec\n",
((LONGLONG)fileSize.QuadPart/(1024.0*1024.0))/(((float)(dwEndTime-dwStartTime))/1000.0));

EXIT:
(void) _getch();
if (bInit)
DeleteCriticalSection(&critSec);
if (hThread[i])
CloseHandle(hThread[i]);
if (hIOCompletionPort)
CloseHandle(hIOCompletionPort);
if (hSourceFile)
CloseHandle(hSourceFile);

exit(dwExitStatus);
}

//////////////////////////////////////////////////////////////////////
DWORD WINAPI readAndAnalyzeChunk(LPVOID lpParam)
{
BOOL bSuccess,
bMoreToRead;
DWORD dwNumBytes,
dwKey,
dwSuccess,
i,
repeatCnt,
dwThreadId;
LPOVERLAPPED completedOverlapped;
PCHUNK completedChunk;
CHUNK chunk;

printf("Thread (%d) started\n", dwThreadId=GetCurrentThreadId());

chunk.buffer=VirtualAlloc(NULL, BUFFER_SIZE, MEM_COMMIT, PAGE_READWRITE);

if (chunk.buffer==NULL)
{
fprintf(stderr, "VirtualAlloc failed (error %d)\n", GetLastError());
exit(1);
}

// Start asynchronous reads. Wait for read to complete, then do next read.
while(1){
bSuccess=GetQueuedCompletionStatus(hIOCompletionPort,
&dwNumBytes,
&dwKey,
&completedOverlapped,
INFINITE);
if (!bSuccess && (completedOverlapped==NULL))
{
fprintf(stderr, "GetQueuedCompletionStatus failed (error %d)\n", GetLastError());
exit(1);
}
if (dwKey==EXITKEY)
{
VirtualFree((LPVOID) chunk.buffer,
0,
MEM_RELEASE);
ExitThread(0);
}
if (!bSuccess)
{
fprintf(stderr, "GetQueuedCompletionStatus removed a bad I/O packet (error %d)\n", GetLastError());
// While you may want to fail here, this example proceeds.
}

if (dwKey != KICKOFFKEY)
{
// Analyze the data. The analysis for this example determines
// the number of pairs of consecutive bytes that are equal.
printf("Analyzing %d byte chunk\n", dwNumBytes);
completedChunk = (PCHUNK)completedOverlapped;
repeatCnt = 0;
for (i = 1; i < dwNumBytes; i++)
if ((((PBYTE)completedChunk->buffer)[i - 1] ^ ((PBYTE)completedChunk->buffer)[i]) == 0)
repeatCnt++;
printf("Repeat count was %d (thread #%d)\n", repeatCnt, dwThreadId);

// If the number of bytes returned is less than BUFFER_SIZE,
// that was the last read. Exit the thread.
if (dwNumBytes < BUFFER_SIZE)
ExitThread(0);
}
// Adjust OVERLAPPED structure for next read.
EnterCriticalSection(&critSec);

if (readPointer.QuadPart < ((ULARGE_INTEGER *)lpParam)->QuadPart)
{
bMoreToRead = TRUE;
chunk.overlapped.Offset = readPointer.LowPart;
chunk.overlapped.OffsetHigh = readPointer.HighPart;
chunk.overlapped.hEvent = NULL; // not needed

// Set the file pointer for the next reader
readPointer.QuadPart += BUFFER_SIZE;
}
else bMoreToRead = FALSE;

LeaveCriticalSection(&critSec);

// Issue the next read.

if (bMoreToRead)
{
dwSuccess = ReadFile(hSourceFile,
chunk.buffer,
BUFFER_SIZE,
&dwNumBytes,
&chunk.overlapped);
if (!dwSuccess && (GetLastError() == ERROR_HANDLE_EOF))
printf( "End of file\n" );
if (!dwSuccess && (GetLastError() != ERROR_IO_PENDING))
fprintf (stderr, "ReadFile at %lx failed (error %d)\n",
chunk.overlapped.Offset,
GetLastError());
}
} // end while
}
HaoyuTan 2005-09-02
  • 打赏
  • 举报
回复
Example Code

[C++]
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <conio.h>

// Assume for this sample that we have a maximum of 16 processors.
#define MAX_THREADS 32
#define BUFFER_SIZE (64*1024)
DWORD dwNumProcessors;

// Handle for the source file to be analyzed
HANDLE hSourceFile=NULL;

// Handle to the I/O completion port
HANDLE hIOCompletionPort=NULL;

// Read position
ULARGE_INTEGER readPointer;

// Critical section
CRITICAL_SECTION critSec;

// ThreadProc function
DWORD WINAPI readAndAnalyzeChunk(LPVOID lpParam);

// Keys that control the behavior of the worker threads.
#define KICKOFFKEY 99 // Start reading and analyze the file
#define KEY 1 // Read the next chunk and analyze it
#define EXITKEY 86 // Exit (the file has been read and analyzed)

// Structure for tracking outstanding I/O
typedef struct _CHUNK {
OVERLAPPED overlapped;
LPVOID buffer;
} CHUNK, *PCHUNK;

//////////////////////////////////////////////////////////////////////
int main(
int argc,
char *argv[],
char *envp)
{
HANDLE hThread[MAX_THREADS];
DWORD dwThreadId,
dwStatus,
dwStartTime,
dwEndTime,
dwExitStatus = ERROR_SUCCESS;
ULARGE_INTEGER fileSize,
readPointer;
SYSTEM_INFO systemInfo;
UINT i;
BOOL bInit=FALSE;
OVERLAPPED kickoffOverlapped,
dieOverlapped;

// Make sure a source file is specified on the command line.

if (argc != 2)
{
fprintf(stderr, "Usage: %s <source file>\n", argv[0]);
dwExitStatus=1;
goto EXIT;
}

// Get the number of processors on the system.

GetSystemInfo(&systemInfo);
dwNumProcessors = systemInfo.dwNumberOfProcessors;

// Open the source file.

hSourceFile = CreateFile(argv[1],
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_FLAG_NO_BUFFERING | FILE_FLAG_OVERLAPPED,
NULL);
if (hSourceFile == INVALID_HANDLE_VALUE)
{
fprintf(stderr, "%s: Failed to open %s, error %d\n",
argv[0],
argv[1],
dwExitStatus = GetLastError());
goto EXIT;
}
fileSize.LowPart = GetFileSize(hSourceFile, &fileSize.HighPart);

if ((fileSize.LowPart==0xffffffff) && (GetLastError()!= NO_ERROR))
{
fprintf(stderr, "%s: GetFileSize failed, error %d\n",
argv[0],
dwExitStatus = GetLastError());
goto EXIT;
}

// Use a criticial section to serialize access by multiple threads.
InitializeCriticalSection(&critSec);
bInit=TRUE;

HaoyuTan 2005-09-02
  • 打赏
  • 举报
回复
用异步IO,任意多线程也没有问题,具体的方式是使用IO完成端口,这里给出一个MSDN上的例子:

Example Details

The sample code demonstrates a multi-threaded application designed to read and analyze a large file. Asynchronous I/O is used to read a chunk of the file at a time by one of a pool of worker threads. The completed I/O is posted at an I/O completion port. A worker thread picks up the completed I/O, analyzes it, reports the results, and starts another asynchronous read operation. The process continues until the whole file has been read.

The number of threads is based on the number of processors in the host. Specifically, it is twice that number. See I/O Completion Ports for more details.

The contrived "analysis" is a count of the number of identical consecutive byte pairs in each chunk.

The file to be analyzed is specified on the command line. The sample is most meaningful if the specified file that is 1MB or larger in size.

The code consists of a single thread running the main function and several identical worker threads running the function named readAndAnalyzeChunk. These functions are described following.

main function

Determines the number of processors, which is related to the concurrency value for the I/O completion port.
Opens the source file to be analyzed for unbuffered asynchronous I/O.
Determines the size of the file.
Initializes a read pointer that will be accessed by worker threads within a critical section.
Creates a single I/O completion port at which completed reads post.
Creates a pool of worker threads (twice the number of processors on the system) and starts each one running readChunkAndAnalyze.
Posts a single "kickoff" I/O completion at the I/O completion port, which causes the worker threads to begin.
Waits for any one of the worker threads to exit, signifying that it has analyzed the data that was read from the last chunk of the file.
Posts a set of "exit" I/O completions at the I/O completion port. This this indicates that the threads should exit after completing any remaining analysis.
Waits for all threads to terminate.
Displays some statistics and exits.
readAndAnalyzeChunk function

Waits on a completed I/O at the I/O completion port upon creation.
One thread begins reading the file when the "kickoff" key is posted by the main program.
Analyzes the data block returned by entering a critical section, retrieving the read pointer, updating the read pointer for the next thread, leave the critical section.
Exits when the "exit" key is posted.

Practise_Think 2005-09-01
  • 打赏
  • 举报
回复
InterlockedExchangeAdd
hundlom 2005-09-01
  • 打赏
  • 举报
回复
互斥
ss3295 2005-09-01
  • 打赏
  • 举报
回复
FTP好象就是这样的
Jarrylogin 2005-09-01
  • 打赏
  • 举报
回复
CCriticalSection
dch4890164 2005-09-01
  • 打赏
  • 举报
回复
使用临界区就可以吧
xili 2005-09-01
  • 打赏
  • 举报
回复
俺的意思是, 两个线程各有自己的 FILE*fp
或 FileHandle
xili 2005-09-01
  • 打赏
  • 举报
回复
同步这方面基本没问题

fopen() 或 CreateFile()要怎么设置参数?
xili 2005-09-01
  • 打赏
  • 举报
回复
这里人气旺,俺喜欢,谢谢各位.
去看看先.

继续欢迎指教.
DentistryDoctor 2005-09-01
  • 打赏
  • 举报
回复
使用临界区进行同步。
快乐鹦鹉 2005-09-01
  • 打赏
  • 举报
回复
用互斥能接受么?
老夏Max 2005-09-01
  • 打赏
  • 举报
回复
http://www.vckbase.com/document/viewdoc/?id=804
老夏Max 2005-09-01
  • 打赏
  • 举报
回复
使用同步:
http://www.vckbase.com/document/viewdoc/?id=758
内容概要:本文详细介绍了“秒杀商城”微服务架构的设计与实战全过程,涵盖系统从需求分析、服务拆分、技术选型到核心功能发、分布式事务处理、容器化部署及监控链路追踪的完整流程。重点解决了高并发场景下的超卖问题,采用Redis预减库存、消息队列削峰、数据库乐观锁等手段保障数据一致性,并通过Nacos实现服务注册发现与配置管理,利用Seata处理跨服务分布式事务,结合RabbitMQ实现异步下单,提升系统吞吐能力。同时,项目支持Docker Compose快速部署和Kubernetes生产级编排,集成Sleuth+Zipkin链路追踪与Prometheus+Grafana监控体系,构建可观测性强的微服务系统。; 适合人群:具备Java基础和Spring Boot发经验,熟悉微服务基本概念的中高级研发人员,尤其是希望深入理解高并发系统设计、分布式事务、服务治理等核心技术的发者;适合工作2-5年、有志于转型微服务或提升架构能力的工程师; 使用场景及目标:①学习如何基于Spring Cloud Alibaba构建完整的微服务项目;②掌握秒杀场景下高并发、超卖控制、异步化、削峰填谷等关键技术方案;③实践分布式事务(Seata)、服务熔断降级、链路追踪、统一配置中心等企业级中间件的应用;④完成从本地发到容器化部署的全流程落地; 阅读建议:建议按照文档提供的七个阶段循序渐进地动手实践,重点关注秒杀流程设计、服务间通信机制、分布式事务实现和系统性能优化部分,结合代码调试与监控工具深入理解各组件协作原理,真正掌握高并发微服务系统的构建能力。
MATLAB基于3D FDTD的微带线馈矩形天线分析[用于模拟超宽带脉冲通过线馈矩形天线的传播,以计算微带结构的回波损耗参数]内容概要:本文介绍了基于3D FDTD(时域有限差分)方法在MATLAB平台上对微带线馈电的矩形天线进行分析的技术方案,旨在模拟超宽带脉冲通过该天线结构的传播过程,并重点计算微带结构的回波损耗参数。该方法通过数值仿真手段精确建模电磁波在天线中的传播特性,适用于高频电磁场仿真与天线性能评估,能够有效支持天线设计优化。文中可能涵盖FDTD算法的基本原理、网格划分、边界条件设置、激励源配置及结果后处理等关键环节。; 适合人群:具备电磁场与微波技术基础知识,熟悉MATLAB编程,从事天线设计、射频工程或相关领域研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①展超宽带天线的设计与性能仿真;②研究微带天线在脉冲激励下的瞬态响应特性;③计算和优化天线的回波损耗(S11参数),提升匹配性能;④教学与科研中用于电磁仿真方法的实践训练。; 阅读建议:建议读者结合FDTD理论基础与MATLAB编程实践,逐步实现仿真流程,重点关注时间步长、空间网格精度和边界条件对仿真结果的影响,并通过对比仿真与实测数据验证模型准确性。

15,467

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧