none
CFile多线程读文件 RRS feed

  • 问题

  • Cfile多线程读的时候是线程安全的吗?

    怎么使用CFile多线程读文件呢,为什么我这么写报错

     

    const CString path = "C:\\test.t";
    UINT ExecutingFunction(LPVOID pParam)
    {
    CString name = (char*)pParam;
    CFile c1;
    c1.Open(path, CFile::modeRead | CFile::bufferRead | CFile::shareDenyRead);
    bool b= true;
    while(true)
    {
    char ptrResult8[8];
    if (b)
    {
    c1.SeekToBegin();
    b = false;
    }
    else
    {
    c1.Seek(8,CFile::begin);
    b = true;
    }
    c1.Read(ptrResult8,8);
    double d1 = *(double*)ptrResult8;
    char fu[10];
    gcvt(d1,5,fu);
    CString trace = name + " : " + fu + "\r\n";   
    TRACE(trace);
    Sleep(1000);
    }
    return 0;
    }

    main()
    {
      CFile cfile;
    cfile.Open(path, CFile::modeReadWrite | CFile::modeCreate);
    double a = 20.2;
    double b = 30.3;
    cfile.Write((char*)&a,8);
    cfile.Write((char*)&b,8);
    cfile.Close();

    AfxBeginThread(ExecutingFunction,"thread1");
    AfxBeginThread(ExecutingFunction,"thread2");
    }

    我希望两个线程同时读一个硬盘上的文件,怎么写呢?

    2011年3月30日 13:32

答案

  • 给你个例子程序,Win32 console application , support MFC

    #include "stdafx.h"
    #include <afxmt.h>

    const CString path(_T("F:\\test.txt"));
    CCriticalSection cs;
    UINT ExecutingFunction(LPVOID pParam)
    {
     LPCTSTR buf = (LPCTSTR)pParam;
     cs.Lock();
     _tprintf(_T("%s write file.\n"), buf);
     try
     {
      CFile file;
      file.Open(path, CFile::modeCreate|CFile::modeWrite|CFile::modeNoTruncate);
      file.SeekToEnd();
      file.Write(buf, _tcslen(buf)*sizeof(TCHAR));
      file.Close();
     }
     catch (CException* e)
     {
      e->ReportError();
      e->Delete();
     }
     cs.Unlock();
     return 0;
    }

    int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
    {
     HANDLE hThread[2] = {0};
     
     CWinThread* pThread1 = AfxBeginThread(ExecutingFunction, (LPVOID)(_T("Thread1")), THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED, NULL);
     CWinThread* pThread2 = AfxBeginThread(ExecutingFunction, (LPVOID)(_T("Thread2")), THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED, NULL);
     
     pThread1->m_bAutoDelete = FALSE;
     pThread2->m_bAutoDelete = FALSE;

     hThread[0] = pThread1->m_hThread;
     hThread[1] = pThread2->m_hThread;

     pThread1->ResumeThread();
     pThread2->ResumeThread();
     WaitForMultipleObjects(sizeof(hThread)/sizeof(hThread[0]), hThread, TRUE, INFINITE);
     for(int i=0; i<sizeof(hThread)/sizeof(hThread[0]); i++)
     {
      CloseHandle(hThread[i]);
     }

     delete pThread1;
     pThread1 = NULL;
     delete pThread2;
     pThread2 = NULL;

     return 0;
    }


    Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.
    • 已标记为答案 zhzhzh 2011年3月31日 8:57
    2011年3月31日 1:14
    版主

全部回复

  • 给你个例子程序,Win32 console application , support MFC

    #include "stdafx.h"
    #include <afxmt.h>

    const CString path(_T("F:\\test.txt"));
    CCriticalSection cs;
    UINT ExecutingFunction(LPVOID pParam)
    {
     LPCTSTR buf = (LPCTSTR)pParam;
     cs.Lock();
     _tprintf(_T("%s write file.\n"), buf);
     try
     {
      CFile file;
      file.Open(path, CFile::modeCreate|CFile::modeWrite|CFile::modeNoTruncate);
      file.SeekToEnd();
      file.Write(buf, _tcslen(buf)*sizeof(TCHAR));
      file.Close();
     }
     catch (CException* e)
     {
      e->ReportError();
      e->Delete();
     }
     cs.Unlock();
     return 0;
    }

    int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
    {
     HANDLE hThread[2] = {0};
     
     CWinThread* pThread1 = AfxBeginThread(ExecutingFunction, (LPVOID)(_T("Thread1")), THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED, NULL);
     CWinThread* pThread2 = AfxBeginThread(ExecutingFunction, (LPVOID)(_T("Thread2")), THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED, NULL);
     
     pThread1->m_bAutoDelete = FALSE;
     pThread2->m_bAutoDelete = FALSE;

     hThread[0] = pThread1->m_hThread;
     hThread[1] = pThread2->m_hThread;

     pThread1->ResumeThread();
     pThread2->ResumeThread();
     WaitForMultipleObjects(sizeof(hThread)/sizeof(hThread[0]), hThread, TRUE, INFINITE);
     for(int i=0; i<sizeof(hThread)/sizeof(hThread[0]); i++)
     {
      CloseHandle(hThread[i]);
     }

     delete pThread1;
     pThread1 = NULL;
     delete pThread2;
     pThread2 = NULL;

     return 0;
    }


    Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.
    • 已标记为答案 zhzhzh 2011年3月31日 8:57
    2011年3月31日 1:14
    版主
  • 谢谢你  !
    2011年3月31日 8:56
  • 最主要的问题是:

     CFile::shareDenyRead

    禁止并发访问。

     

    2011年4月2日 1:38