Jump to content

  • Log In with Google      Sign In   
  • Create Account

Zael

Member Since 28 Jan 2008
Offline Last Active May 17 2012 09:36 AM

Topics I've Started

Threading behavior changes with optmizations

27 February 2012 - 11:35 PM

I am having an issue with some code I have written, and was wondering if anybody had the depth of knowledge to explain what is happening, and how to avoid it. Here is some example code to demonstrate.

#define BOOST_THREAD_USE_LIB
#include <boost/thread.hpp>
namespace std
{
typedef boost::thread thread;
};
class Thread
{
private:
std::thread* thrd;
int x;
public:
Thread()
{
  thrd = NULL;
  x = 0;
}
~Thread()
{
  if(thrd != NULL)
  {
   thrd->join();
   delete thrd;
  }
};
void start()
{
  thrd = new std::thread(&Thread::run, this);
};
void join()
{
  thrd->join();
  thrd = NULL;
};
virtual void run()
{
  while(x!=-1)
  {
   if(x == 1)
   {
	std::cout << "Got here.\n";
	x = -1;
   }
  }
};
void setX(int x)
{
  this->x = x;
}
};
int main()
{
Thread thrd;
thrd.start();
int x;
thrd.start();
std::cin >> x;
thrd.setX(1);
thrd.join();
}

The "Got here." text is displayed twice (which I don't quite understand) when no optimizations are set; however, when I set -O, -O1, or -O2, the program never makes it into the if and hangs forever. Anybody know why that would be?

Thanks

P.S. Using MinGWx64 and Boost 1.48.

Falling Blocks (another Tetris clone)

30 September 2010 - 05:10 PM

I have just completed my Tetris clone. First game I have stuck through to completion. I will be putting it on my website soon so that employers looking at my resume will be able to see it. I know there are one or two hacks in it right now to get things working, but I was hoping I could get some feedback from this community. There is definitely a problem where the music doesn't always start as the program starts. If anybody knows why that is I would appreciate it. I suspect is has to do with threading in SDL_mixer. Download location: http://megaupload.com/?d=GHC84XBC

Any comments would be very appreciated.

Also while I don't want it to be the focus of this topic, if anybody notes anything I am lacking for compliance with the creative commons license (music files) and the LGPL license for SDL, please let me know.

Thanks for the input.

-Zael-

P.S. Mods, I know tetris is fairly beginner so if this is more appropriate in the beginner forums feel free to move it.

Program fails with return code 128

21 February 2010 - 06:50 PM

Hey everyone. I'm having an interesting problem that I am hoping somebody can help me with. I am trying to look at a map that has only water and land. I created this function that sets up all water set to 0 and all land set to 1. I am now trying to go through each cell and call the below function to identify separate land masses. It seems to work for maps 64x64, 128x128, 256x256, but dies with a return code of 128 when the map is 512x512. This function is called on every cell.
int followLand(std::vector <std::vector <unsigned int> > & tmpmap, unsigned int x, unsigned int y, unsigned short group)
{
	std::cout << "Following land\n";
	fflush(stdout);
	if(tmpmap[x][y] == 1)
	{
		tmpmap[x][y] = group;
		std::cout << "X: " << x << "\n";
		std::cout << "Y: " << y << "\n";
		std::cout << (x-1+tmpmap.size())%tmpmap.size() << "\n";
		std:: cout << (y-1+tmpmap.size())%tmpmap.size() << "\n";
		fflush(stdout);
/*Dies between here but before going inside the function of the next line.*/
		followLand(tmpmap, (x-1+tmpmap.size())%tmpmap.size(), (y-1+tmpmap.size())%tmpmap.size(), group);
		std::cout << "Ah1\n";
		fflush(stdout);
		followLand(tmpmap, (x-1+tmpmap.size())%tmpmap.size(), y, group);
		std::cout << "Ah2\n";
		fflush(stdout);
		followLand(tmpmap, (x-1+tmpmap.size())%tmpmap.size(), (y+1+tmpmap.size())%tmpmap.size(), group);
		std::cout << "Ah3\n";
		fflush(stdout);
		followLand(tmpmap, x, (y-1+tmpmap.size())%tmpmap.size(), group);
		std::cout << "Ah4\n";
		fflush(stdout);
		followLand(tmpmap, x, (y+1+tmpmap.size())%tmpmap.size(), group);
		std::cout << "Ah5\n";
		fflush(stdout);
		followLand(tmpmap, (x+1+tmpmap.size())%tmpmap.size(), (y-1+tmpmap.size())%tmpmap.size(), group);
		std::cout << "Ah6\n";
		fflush(stdout);
		followLand(tmpmap, (x+1+tmpmap.size())%tmpmap.size(), y, group);
		std::cout << "Ah7\n";
		fflush(stdout);
		followLand(tmpmap, (x+1+tmpmap.size())%tmpmap.size(), (y+1+tmpmap.size())%tmpmap.size(), group);
		std::cout << "Ah8\n";
		fflush(stdout);
	}
	std::cout << "Done Following land\n";
	fflush(stdout);
	return 0;
}

There is a lot of cout in the function for debugging purposes right now. The interesting thing is that the last line out for the cout is the line indicated instead of a line inside the function. I can't figure out what's going on. It seems to often die in the middle of the map; it's definitely not a segment fault. Interesting thing is when compiled in linux the program runs fine, but when compiled in windows with minGW it fails. Any ideas? Any recommendations for a better way to do this would be nice too. Thanks, -Zael-

Help with vector inside a class

08 April 2009 - 06:39 AM

I am trying to implement a matrix class which contains a 2D vector and has methods for matrix operations in the class. At the moment I am having issue with either the initialization or the output function, and I am unsure as to which. The problem that is occurring is that at run-time I am getting output of differing values when as near as I can tell they should all be the same. The weirdest part is that when I run the code in a function outside of the class this problem goes away. Below is my code and output.

Matrix.h
#ifndef _Matrix
#define _Matrix
#include <iostream>
#include <vector>

template <class T>
class Matrix
{
	private:
	std::vector <std::vector <T> > mat;

	public:
	Matrix(unsigned int, unsigned int, T);
	unsigned int rows();
	unsigned int cols();
	T operator () (unsigned int row, unsigned int col);
};

template <class T>
Matrix<T>::Matrix(unsigned int cols, unsigned int rows, T values)
{
	T x=values;
	mat.clear();
	for(unsigned int i=0; i<cols; i++)
	{
		mat.push_back(std::vector <T> ());
		for(unsigned int j=0; j<rows; j++)
		{
			mat[i].push_back(x);
			mat[i][j]=values;
		}
	}
}

template <class T>
unsigned int Matrix<T>::cols()
{
	return mat.size();
}

template <class T>
unsigned int Matrix<T>::rows()
{
	if(mat.size()==0) return 0;
	return mat[0].size();
}

template <class T>
T Matrix<T>::operator () (unsigned int row, unsigned int col)
{
	if (row > rows() || col > cols()) return mat[0][0];
	return mat[col][row];
}

template <class T>
std::ostream & operator << (std::ostream & output, Matrix <T> & mat)
{
	output<<"[\n";
	for(unsigned int i=0; i<mat.cols(); i++)
	{
		for(unsigned int j=0; j<mat.rows(); j++)
		{
			output<<mat(i, j)<<" ";
		}
		output<<"\n";
	}
	output<<"]\n";
	return output;
}

#endif




main.cpp
template <class T>
vector <vector <T> > matrix(unsigned int cols, unsigned int rows, T values)
{
	vector <vector <T> > mat;
	for(unsigned int i=0; i<cols; i++)
	{
		mat.push_back(std::vector <T> ());
		for(unsigned int j=0; j<rows; j++)
		{
			mat[i].push_back(values);
		}
	}
	return mat;
}

template <class T>
std::ostream & operator << (std::ostream & output, vector <vector <T> > & mat)
{
	output<<"[\n";
	for(unsigned int i=0; i<mat.size(); i++)
	{
		for(unsigned int j=0; j<mat[i].size(); j++)
		{
			output<<mat[i][j]<<" ";
		}
		output<<"\n";
	}
	output<<"]\n";
	return output;
}

int main()
{
	unsigned int N=5;
	Matrix <int> theta(N, 2, 50);
	vector <vector <int> > mat=matrix(N, 2, 50);
	cout << theta;
	cout << mat;
	return 0;
}



Output:
[
50 50
50 50
0 0
50 50
50 50
]
[
50 50
50 50
50 50
50 50
50 50
]

Can anybody tell me why this is happening and how to fix it? Thanks.

SDL_GetKeyState Question

19 August 2008 - 05:34 AM

I was recently working on an engine, and used Uint8 *keys=SDL_GetKeyState(NULL) in my main loop. At some point I call a function(keys). For some reason when inside the function, keys only contains the correct data 1/8 of the time, but in the main loop keys is always working. Inside the function keys seems empty the other 7/8 of the time. Can anybody tell me why this is? Let me know if it is code specific, and I will post my code. I am presently running the code on Windows by the way. I haven't tried it on any other platforms. Thanks.

PARTNERS