CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    May 2002
    Posts
    4

    Need help in Multithreading

    Hi All,

    My application runs in Windows 2000 Server. The same application is accessed by different users in different windows session creating new application instance for every user.

    I have a problem writing data into a file, if I run two instance using two different user Sessions. See the following code (this is a test loop checking for data loss.)

    CLog c;
    char str[200]={0};
    for(int i=0; i < 18000; i++){
    HANDLE hMutex = CreateMutex(NULL, FALSE, "MyProtectedErrorHandling");
    sprintf(str, "Line:%d Message:Inside the loop, Handle:0x%x", i, hMutex);
    DWORD dwResult = WaitForSingleObject(hMutex, INFINITE);
    c.write(str); //Opens file in append mode, writes str and closes\ the file.
    ReleaseMutex(hMutex);
    CloseHandle(hMutex);
    }

    The total lines should be 36000. But I am not getting all the data I am writing in to the file. I am missing nearly 10 lines of data. Please tell me whether I am using Mutex properly for handling multiple processes or not. If wrong please suggest other useful ways to do this.

    Thanks in advance.
    Praveen

  2. #2
    igbrus is offline Elite Member Power Poster
    Join Date
    Aug 2000
    Location
    Los Angeles
    Posts
    4,658
    CLog c;
    char str[200]={0};
    HANDLE hMutex = CreateMutex(NULL, FALSE, "MyProtectedErrorHandling");
    for(int i=0; i < 18000; i++){
    sprintf(str, "Line:%d Message:Inside the loop, Handle:0x%x", i, hMutex);
    DWORD dwResult = WaitForSingleObject(hMutex, INFINITE);
    c.write(str); //Opens file in append mode, writes str and closes\ the file.
    ReleaseMutex(hMutex);
    }
    CloseHandle(hMutex);

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured