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

xili 2005-09-01 04:57:14
开2个线程, 写同一个文件, 怎么做?
写的是不同的部分,无需担心互相覆盖.
有没有例子可以看看?
...全文
210 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
计及源荷不确定性的综合能源生产单元运行调度与容量配置优化研究(Matlab代码实现)内容概要:本文围绕“计及源荷不确定性的综合能源生产单元运行调度与容量配置优化”展研究,利用Matlab代码实现相关模型的构建与仿真。研究重点在于综合能源系统中多能耦合特性以及风、光等可再生能源出力和负荷需求的不确定性,通过鲁棒优化、场景生成(如Copula方法)、两阶段优化等手段,实现对能源生产单元的运行调度与容量配置的协同优化,旨在提高系统经济性、可靠性和可再生能源消纳能力。文中提及多种优化算法(如BFO、CPO、PSO等)在调度与预测中的应用,并强调了模型在实际能源系统规划与运行中的参考价值。; 适合人群:具备一定电力系统、能源系统或优化理论基础的研究生、科研人员及工程技术人员,熟悉Matlab编程和基本优化工具(如Yalmip)。; 使用场景及目标:①用于学习和复现综合能源系统中考虑不确定性的优化调度与容量配置方法;②为含高比例可再生能源的微电网、区域能源系统规划设计提供模型参考和技术支持;③展学术研究,如撰论文、课题申报时的技术方案借鉴。; 阅读建议:建议结合文中提到的Matlab代码和网盘资料,先理解基础模型(如功率平衡、设备模型),再逐步深入不确定性建模与优化求解过程,注意区分鲁棒优化、随机优化与分布鲁棒优化的适用场景,并尝试复现关键案例以加深理解。
内容概要:本文系统分析了DesignData(设计数据)的存储结构,围绕其形态多元化、版本关联性强、读特性差异化等核心特性,提出了灵活性、版本化、高效性、一致性和可扩展性五大设计原则。文章深入剖析了三类主流存储方案:关系型数据库适用于结构化元信息存储,具备强一致性与高效查询能力;文档型数据库适配半结构化数据,支持动态字段扩展与嵌套结构;对象存储结合元数据索引则有效应对非结构化大文件的存储需求,具备高扩展性与低成本优势。同时,文章从版本管理、性能优化和数据安全三个关键维度提出设计要点,建议采用全量与增量结合的版本策略、索引与缓存优化性能、并通过权限控制、MD5校验和备份机制保障数据安全。最后提出按数据形态分层存储的核心结论,并针对不同规模团队给出实践建议。; 适合人群:从事工业设计、UI/UX设计、工程设计等领域数字化系统发的技术人员,以及负责设计数据管理系统架构设计的中高级工程师和系统架构师。; 使用场景及目标:①为设计数据管理系统选型提供依据,合理选择或组合使用关系型数据库、文档型数据库与对象存储;②构建支持版本追溯、高性能访问、安全可控的DesignData存储体系;③解决多用户协作、大文件存储、历史版本管理等实际业务挑战。; 阅读建议:此资源以实际应用场景为导向,结合具体数据库类型和表结构设计进行讲解,建议读者结合自身业务数据特征,对比分析不同存储方案的适用边界,并在系统设计中综合考虑成本、性能与可维护性之间的平衡。

15,467

社区成员

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

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