The basic concept is that the business thread outputs the log (mainly by increasing the log queue node), and then the secondary thread is responsible for the log queue consumption.
The benefit of this is that thread blocking is not caused by the log output.
The code is simple, as follows:
//code by Lichmama from Cnblogs.com#include <stdio.h>#include<stdlib.h>#include<string.h>#include<windows.h>#include<process.h>struct_log_ {intLog_type; intlog_length; Charlog_text[1024x768];}; typedefstruct_log_queue_ {struct_log_ log; struct_log_queue_ *Next;} Log_queue,*Plog_queue; HANDLE Hmutex=NULL; Plog_queue Head=NULL; Plog_queue Tail=NULL; Plog_queue Temp=NULL; Plog_queue ttmp=null;unsignedint__stdcall Log_thread (void*args) { for(;;) {WaitForSingleObject (Hmutex, INFINITE); if(head) {ttmp=Head; printf ("%03d,%d,%s\n", Ttmp->log.log_type, Ttmp->log.log_length, ttmp->log.log_text); Head= head->Next; if(!head) tail =NULL; Free (ttmp); Ttmp=NULL; } ReleaseMutex (Hmutex); } return 0;}intMain () {HANDLE Hmutex=CreateMutex (NULL, TRUE, NULL); HANDLE Hthread= _beginthreadex (NULL,0, Log_thread, NULL,0, NULL); Charch; while((Ch=getch ())! ='Q') {Temp= (plog_queue) malloc (sizeof(Log_queue)); Temp->log.log_type = ch-'0'; Temp->log.log_length = One; strcpy (Temp->log.log_text,"Hello,world"); Temp->next =NULL; if(!head) Tail = temp, head =temp; ElseTail->next = temp, tail =temp; } closehandle (Hmutex); TerminateThread (Hthread,0); CloseHandle (Hthread); return 0;}