Hi all,

Here is the problem; I’m trying to make two threads draw pixels in two different memory spaces at the same time. Nothing fancy, just a test program to learn about thread programming.
I allocate the memory, send the address as parameters to the two threads, and in each thread make a new instance of the draw class to make sure that nothing in the two threads should interfere with one another.
BUT, the two pictures are identical?? I just don’t get it. The pixels are located at random. Even when I wait until the first thread is done to start up the next; it STILL generates the same two pictures. ??
I have set den background color on the second image to be a dark grey, just to ensure that the images are generated in the two different memory areas. (which they are)

Hope someone can explain how this is possible, or even what I'm doing wrong. :)

Below is the main code, if any more informating is needed, just say so and I'll post all you need.

Thanks

#include "stdafx.h"
#include "windows.h"
#include "tgj.h"
#include <process.h>
#include "TSDraw.h"

unsigned __stdcall Draw(void* pArguments)
{
	TSDraw* pen = new TSDraw();
	pen->height = 480;
	pen->width = 640;
	pen->BitMap = (byte*)pArguments;

	printf("In TSdraw thread... BITMAP Handle = %d And the pen = %d\n",pen->BitMap,pen);
	
	for(long i=0;i<20000;i=i++)
	{
		int r = (int)((rand()/(double)RAND_MAX)*255.0);
		int g = (int)((rand()/(double)RAND_MAX)*255.0);
		int b = (int)((rand()/(double)RAND_MAX)*255.0);
		int xpos = (int)((rand()/(double)RAND_MAX)*640.0);
		int ypos = (int)((rand()/(double)RAND_MAX)*480.0);

		pen->DrawPixel(xpos,ypos,r,g,b);
	}
	delete pen;
    _endthreadex(0);
    return 0;
}

int _tmain(int argc, _TCHAR* argv[])
{
	srand ( (unsigned)GetTickCount() );

	byte* pic1 = new byte[640*480*4];
	byte* pic2 = new byte[640*480*4];

	for(int i = 0;i<640*480*4;i++)
	{
		pic1[i]=0;
		pic2[i]=50;
	}

	unsigned ID1;
	unsigned ID2;

	long btime = GetTickCount();
 
	printf("bmp handles = %d and %d\n",&pic1,&pic2);

	HANDLE hThread1 = (HANDLE)_beginthreadex(NULL, 0, &Draw, (void*)pic1, 0, &ID1);
	HANDLE hThread2 = (HANDLE)_beginthreadex(NULL, 0, &Draw, (void*)pic2, 0, &ID2);

	WaitForSingleObject(hThread1, INFINITE);
	WaitForSingleObject(hThread2, INFINITE);

	CloseHandle(hThread1);
	CloseHandle(hThread2);

	printf("Ticks = %d\n",GetTickCount()-btime);

	TGAHEADER TH;

	TH.identsize = 0;              // Size of ID field that follows header (0)
    TH.colorMapType = 0;           // 0 = None, 1 = paletted
    TH.imageType = 2;              // 0 = none, 1 = indexed, 2 = rgb, 3 = grey, +8=rle
    TH.colorMapStart = 0;  // First colour map entry
    TH.colorMapLength = 0; // Number of colors
    TH.colorMapBits = 0;   // bits per palette entry
    TH.xstart = 0;         // image x origin
    TH.ystart = 0;         // image y origin
    TH.width = 640;          // width in pixels
    TH.height = 480;         // height in pixels
    TH.bits = 32;                   // bits per pixel (8 16, 24, 32)
    TH.descriptor = 8;             // image descriptor

	TGASave("dump1.tga", pic1, TH);
	TGASave("dump2.tga", pic2, TH);
	
	delete pic1;
	delete pic2;
	return 0;
}

- hmortense

Recommended Answers

All 2 Replies

The seed for each thread is independent. You need to call srand() in the Draw() function to reseed for each thread.

commented: Simple and accurate, information. :) +1

Thanks ;) I didn't know.
I tried srand in the draw finction earlier, but only with the current time as seed. So that didn't work.

Thanks for the info ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.