Log generating program in a console program (multiple threads)

xiaoxiao2021-03-05  28

The body is under VC, and the MFC Console program default behavior is a single-threaded working environment. We can use the following two ways to change the single-thread in its working environment to multi-threaded. 1. In the Code Generation option under Engineering -> Settings -> C / C , set Use Run-Time Library to Multithreaded (Multithread). 2, you can add "/ MT" in the current Project Options. (Represents C runtime library using multi-threaded version)

#include #include #include using namespace std; void readtime () {OFStream F; cstract str; int inu = 0; while (1) {ctime CT = CTime :: GetCurrentTime (); str.format ("% D:% D:% D:% D", (int) ct.getyear (), (int) ct.getMonth (), (int) ct.getday (), (Int) ct.getminute (), (int) ct.getSecond ());

F.Open ("w.txt", iOS :: app); f << (lpctstr) str-< endl; str.format ("% d / n", INU); Printf ("% s", str) ; Sleep (1000); INU ; f.close ();}}

INT main () {handle hthread; // Returned thread handle, I want to end the thread early, can pass the handle DWORD thREADID; // Record the thread ID number int inu; hthread = CreateThread (Null, 0, (lpthread_start_routine) Readtime, NULL , 0, & threadid; for (int i = 0; i <1000; i ) for (int J = 0; i <10000; j ) {SLEEP (1000);} return 0;}

You need to load the header file (AFX.H) of the class used so that the MFC class-independent of the GUI can be used. In this example, CString is used.

转载请注明原文地址:https://www.9cbs.com/read-33395.html

New Post(0)