}
VoidWritebuffertofile (ulong threadid)
{
Postthreadmessage (threadid, wm_writelog, null, null );
}
The code here is simplified to illustrate the basic problems. The resource division here should be as reasonable as possible, and enter the critical section only when you really need to access the resource, instead of doing any extra work in the criticalsection. At the same time, the division of resources should be as independent as possible. Do not place irrelevant resources in the same section to avoid unnecessary waiting and affect the efficiency of thread scheduling.
==========================================
Supplement
==========================================
(1) When the program exits, we hope that the log thread can output data that remains in the buffer zone to the file, but this seems a little difficult. Because we only have one Asynchronous Method of postthreadmessage, and there is no synchronous method that forces the current thread to wait for the processing of the other party to end.
When I first send wm_writelog and then send the wm_quit message to the log thread, I find that the log thread is finished, but it does not seem to process the wm_writelog message before the end. Of course, the specific cause remains to be checked. Because wm_quit is not appended to the queue as we imagine other messages, on the contrary, it only changes a flag that affects the return value of getmessage. The processing process here is transparent to programmers, so I can only guess it by myself. The getmessage function first detects this flag, if "received" wm_quit is found, false is returned. Due to the multi-thread processing method and the particularity of wm_quit, wm_writelog message may fail to be sent because it seems that the thread message queue has been recycled by the system when it tries to enter the column.
Since there is no function such as sendthreadmessage (probably to prevent deadlocks between threads), I adopt a method that seems not reliable but is indeed useful, that is, after sending the wm_writelog message, take the initiative to let out the CPU time slice, let yourself sleep for a moment 200 milliseconds (we are optimistic that the log thread has enough time to process the message during this period), and then send the wm_quit message to end the log thread, it turns out that this method is actually useful, but it seems "blind", so it cannot be a very good and reliable method.
[Supplement 2: Supplement] hoodlum1980, 2011-9-2
First, redescribe the issues mentioned in the supplement:
When a process needs to exit, wm_quit is sent to the main thread of the process. Because the log thread periodically writes logs in the cache to the disk, this problem exists. Before the process exits, ensure that the log thread writes the remaining content in the buffer to the disk. However, it is not enough to post a wm_writelog to the log thread at the exit time! (The log thread may not have the opportunity to process the message because it has started to try to end all threads ).
Looking back at this problem, we can find that this is a typical thread synchronization problem.
Therefore, this problem can also be solved using a typical thread synchronization method. Of course, some new changes are required. For example, before postquitmessage, we also use postthreadmessage to notify the log thread (we can define another message, such as wm_writelog2) to save the buffer content to the disk file, at this time, we create a signal or event, and then wait for it with waitsingleobject. If the log thread is fully saved, we will set the signal/event.
This is much more reliable than the previous method of "blindly waiting for 200 milliseconds.