Utilize multi-threaded synchronization test (transfer)

xiaoxiao2021-03-06  72

Thoughts of test

I have launched 10 threads, and each thread writes your own information in a text file (1.txt), and each thread writes 100 times. Each information includes its thread ID, and time, and includes a message with two horizontal lines.

Test steps

Select synchronization and do not select synchronization, each test once.

Test Results

When you do not choose to synchronize, we can find that two horizontal lines of each message in the recorded text are not one, arrange confusion.

When you select synchronization, we can find two horizontal lines of each message in the recorded text, and arrange them in order. There are more program consumption.

in conclusion

When using the critical zone to synchronize, only one thread can be written to the file at the same time, which can guarantee the integrity of each information.

Since the other thread must wait, it is more time consumption in synchronization.

The main code is as follows

// Built a file class for testing

Class CMYFILE

{

PUBLIC:

CMYFILE ()

{

InitializationCriticalSection (& CS);

}

~ CMYFILE ()

{

DeletecriticalSection (& CS);

}

Void Msglog (Char * Buffer, Int Length, Int Wait)

{

Static I = 0;

CSTRING A;

A.Format ("% d #", i );

IF (M_Bsync) EntercriticalSection (& CS);

FWRITE ("/ n", 1, 1, fstream);

FWRITE (A CString ('' - ', 50), 52, 1, fstream);

FWRITE ("/ n", 1, 1, fstream);

FWRITE (Buffer, Length, 1, FStream);

SLEEP (WAIT);

FWRITE ("/ n", 1, 1, fstream);

FWRITE (A CString ('' - ', 50), 52, 1, fstream);

FFlush (fstream);

IF (M_Bsync) LeavecriticalSection (& CS);

}

Bool OpenFile (CSTRING Filename, Bool Bsync)

{

m_filename = filename;

m_bsync = BSYNC;

IF ((FStream = FOPEN (FileName, "WT")) == NULL)

Return False;

Else

Return True;

}

Void Closefile ()

{

Fclose (FStream);

}

protected:

Critical_section CS;

FILE * FSTREAM;

CString M_FileName;

BOOL M_BSYNC;

Private:

}

Test code:

Uint ThreadFunc (LPVOID P);

Void cbdlg :: onbutton2 ()

{

// Todo: Add Your Control Notification Handler Code Here

CMYFILE FILE;

IF (m_sync.getCheck () == 1)

File.openfile ("1.txt", true); // Synchronization

Else

File.openfile ("1.txt", false; // asynchronous

CWINTHREAD * THD [10];

Handle Hthd [10];

For (int i = 0; i <10; i ) {

THD [I] = AFXBEGINTHREAD (Threadfunc, & File);

// deliberately manufacturing a thread startup time

Sleep (10);

HTHD [I] = THD [I] -> M_HTHREAD;

}

// Waiting for all threads to end

WaitFormultiPleObjects (10, HTHD, TRUE, Infinite);

File.closefile ();

AfxMessageBox ("OK");

}

Uint ThreadFunc (LPVOID P)

{

CMYFILE * file = (CMYFILE *) P;

Long LthreadId = :: afxgetthread () -> m_nthreadid;

CSTRING A;

A.Format ("Thread% D", LTHREADID;

For (int i = 0; i <100; i )

{

CTIME TIME = CTIME :: getcurrenttime ();

CString str = time.format ("% Y-% M-% D% H:% M:% S");

Str = STR "--------->" a;

File-> msglog (str.getBuffer (Str.getLength ()), str.getLength (), LTHREADID / 100);

}

Return 1;

} Related source code download: http://www.vckbase.com/code/winsys/mtask/multithreadssyntest.rar

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

New Post(0)