Use srand () to generate random numbers with caution when using multiple threads in MFC

Source: Internet
Author: User

I recently encountered a problem when I was writing a small program. I had to spend a day or two and finally solved the problem. Now I want to write the relevant information below. I hope that other students in the same situation can take less detours.

Implemented functions: Based on the basic dialog box, after the start button is pressed, several images are randomly drawn in the dialog box. After the stop button is pressed, the image is stopped.

Because button messages need to be responded when drawing images, I used a thread to implement the image rendering function.

The thread code is as follows:

[Cpp] UINT ThreadDisplay (LPVOID param)
{
ImageInfo * pImage = (ImageInfo *) param;
If (pImage-> m_ImagePathAry.GetSize () = 0)
{
Return FALSE;
}
<Strong> <span style = "color: # ff0000;"> srand (time (NULL); // </span> </strong>
While (1)
{
 
CDC * pDC = pImage-> pWnd-> GetDC ();
CDC memDC;
CBitmap MemBitmap;
MemDC. CreateCompatibleDC (NULL)
MemBitmap. CreateCompatibleBitmap (pDC, pImage-> rc. right-pImage-> rc. left, pImage-> rc. bottom-pImage-> rc. top );
MemDC. SelectObject (MemBitmap );
MemDC. FillSolidRect (255,255,255, pImage-> rc. right-pImage-> rc. left, pImage-> rc. bottom-pImage-> rc. top, RGB ));
<Pre name = "code" class = "cpp"> <strong> <span style = "color: # ff0000;"> // srand (time (NULL )); </span> </strong>
UINT ThreadDisplay (LPVOID param)
{
ImageInfo * pImage = (ImageInfo *) param;
If (pImage-> m_ImagePathAry.GetSize () = 0)
{
Return FALSE;
}
<Strong> <span style = "color: # ff0000;"> srand (time (NULL); // </span> </strong>
While (1)
{

CDC * pDC = pImage-> pWnd-> GetDC ();
CDC memDC;
CBitmap MemBitmap;
MemDC. CreateCompatibleDC (NULL)
MemBitmap. CreateCompatibleBitmap (pDC, pImage-> rc. right-pImage-> rc. left, pImage-> rc. bottom-pImage-> rc. top );
MemDC. SelectObject (MemBitmap );
MemDC. FillSolidRect (255,255,255, pImage-> rc. right-pImage-> rc. left, pImage-> rc. bottom-pImage-> rc. top, RGB ));
<Pre name = "code" class = "cpp"> <strong> <span style = "color: # ff0000;"> // srand (time (NULL )); </span> </strong> Graphics graph (memDC. getSafeHdc (); int m_rand = rand () % pImage-> m_ImagePathAry.GetSize (); WCHAR * path = (pImage-> imagepath + "\" + pImage-> m_ImagePathAry [m_rand]). allocSysString (); Image image (path); graph. drawImage (& image, 0, 0, pImage-> rc. right-pImage-> rc. left, pImage-> rc. bottom-pImage-> rc. top); pDC-> BitBlt (pImag e-> rc. left, pImage-> rc. top, pImage-> rc. right-pImage-> rc. left, pImage-> rc. bottom-pImage-> rc. top, & memDC, 0, 0, SRCCOPY); pImage-> pWnd-> ReleaseDC (pDC); // m_rand ++; DelayTime (30 ); // 30 ms latency} return TRUE ;}

 

The thread uses a working thread. The input parameters contain the details of the image to be drawn. Please pay attention to the bold red part, srand (time (NULL). I used to put it in the whlie loop. As a result, although I wrote DelayTime (30) in the thread) (this function has many precision latency functions on the Internet and is also reproduced in my blog). However, in actual effect, the display interval of images is 1 second (visual perception, about 1 second ).

This is what I have never understood. After I put the srand code out of the while LOOP, I found that the program can be drawn at the interval I set. This is really strange. Is the srand function interfering with the DelayTime delay accuracy?

Ps:

I did not find any errors during debugging because srand uses the current time as the seek. In the thread, although it is in the while loop, the time of each execution is different, this leads to different random numbers generated each time.

 

From txg703003659

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.