Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
307 views
in Technique[技术] by (71.8m points)

c - writing in file using threads (WINAPI)

Just when I thought I finally understood threads, I got a problem.

In the main thread, I create a mutex using this:

Mutex = CreateMutex(NULL, FALSE, Mutex_Name);

Then, in one of the worker threads (the first one to reach), I have a function that returns a handle to a file by creating it like this:

file_h = CreateFile("The_File.txt",
                    GENERIC_WRITE | GENERIC_READ,
                    FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE,
                    NULL,
                    CREATE_NEW,
                    FILE_ATTRIBUTE_NORMAL,
                    NULL);

The other threads have functions that return a handle to the file by this:

file_h = CreateFile("The_File.txt",
                    GENERIC_WRITE | GENERIC_READ,
                    FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE,
                    NULL,
                    OPEN_EXISTING,
                    FILE_ATTRIBUTE_NORMAL,
                    NULL);

At the last part, both threads try to write to the file using this function:

int W_to_f(char* str_to_w, HANDLE file)
{
    HANDLE mutex = NULL;
    DWORD waitcode1;
    DWORD dwBytesWrite;
    char line[Line_Len];
    mutex = OpenMutex(MUTEX_ALL_ACCESS, TRUE, LOCK);
    if (mutex == NULL)
    {
        printf("Failed at opening LOCK in write file
");
        return -1;
    }
    waitcode1 = WaitForSingleObject(mutex, 15000);
    if (WAIT_OBJECT_0 != waitcode1)
    {
        printf("Error when waiting for LOCK in file
");
        return -1;
    }
    strcpy(line, str_to_w);
    while ((Line_Len - (strlen(line) + 1)) > 0)
    {
        strcat(line, "@");
    }
    SetFilePointer(file, 0, NULL, FILE_END); //problem?
    WriteFile(file, line, strlen(line), &dwBytesWrite, NULL);
    WriteFile(file, "
", 2, &dwBytesWrite, NULL);
    if(!ReleaseMutex(mutex))
    {
        printf("failed release mutex in write_file fun
");
        return -1;
    }
    return 0;
}

All I want them to do is to write to the file like this:

firststring@@@@@@@@@@@@
secondstring@@@@@@@@@@@

But, after checking with breakpoints, what I get is that the first one manages to write to file:

firststring@@@@@@@@@@@

But then after the second one writes to the file, it becomes:

癡慩?㈱??????????幀搀湩楤????????????

Just one line, it doesn't even write the Chinese after the firststring@@@@.

Should I somehow use SetFilePointer?

Should I CreateFile() in the function using OPEN_EXISTING for each thread?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...