Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


staticVoid2

Member Since 06 Jan 2008
Offline Last Active Jul 13 2015 05:55 AM

Topics I've Started

boost::thread failing to join in singleton destructor

18 June 2015 - 02:26 AM

Hi,

 

I came across an issue recently and was quite curious as to why it was happening - I managed to fix the problem but the reason for it occurring still alludes me.

 

Basically I'm using a singleton class whose instance is wrapped in a std::unique_ptr like so:

 

Header:

#pragma once
#include <boost/thread.hpp>
#include <memory>

class Singleton
{
public:

	~Singleton();

	static Singleton* Instance();
	
private:

	Singleton();
	
	boost::thread m_thread;
	static std::unique_ptr<Singleton> m_sInstance;
}

.cpp

#include "Singleton.h"

std::unique_ptr<Singleton> Singleton::m_sInstance = nullptr;
std::atmoic_bool gCond;

void ThreadFunction()
{
	while(gCond)
	{ }
	cout << "Hello World" << endl;
}

Singleton::Singleton()
{
	gCond = true;
	m_thread = boost::thread(ThreadFunction);
}

Singleton* Singleton::Instance()
{
	if(m_sInstance == nullptr)
	{
		m_sInstance = std::unique_ptr<Singleton>(new Singleton());
	}
        return m_sInstance.get();
}

Singleton::~Singleton()
{
	if(m_thread.joinable())
	{
		gCond = false;
		cout << "joinable" << endl;
		m_thread.join();
                cout << "joined" << endl;
	}
}

The singleton uses lazy initialization therefore the first call into the 'Instance' method will construct the singleton and subsequently start the thread. The problem occurs in the destructor where I attempt to join the thread. In my main application method I have the following:


int main()
{
	Singleton::Instance()->DoSomething();

	return 0;
}

The output of the program is as follows:

 

joinable

joined

 

The singleton destructor gets called correctly but when it attempt to join it does not actually finish executing the ThreadFunction method - this method does get called and is spinning until the gCond variable is set to true. Whats puzzling is that the m_thread object reports to be joinable but the subsequent call to join does not actually do anything. I've also checked the join method pre-condition:

 

this->get_id()!=boost::this_thread::get_id()

 

which is true.

 

When I modify the code to explicitly call a shutdown method at the end of main where the shutdown method does the same as the destructor:

int main()
{
   Singleton::Instance()->DoSomething();

   Singleton::Instance()->Shutdown();

   return 0;
}

It works fine and the program output is as expected:

 

joinable

Hello World

joined

 

 

I'm not entirely sure why the destructor is failing to join or in-fact how destructors of objects declared in global scope operate. I assumed this was to perhaps to do with a boost sub-system which is also using a global object destructor to uninitialize all its boost::thread objects. Another assumption was that the main method had already finished and therefore the thread had lost its context when the destructor was called and join failed.

 

I added in the explicit shutdown as a fix but it would interesting to know exactly why this is happening.

 

Cheers.

 


Any good cross platform C++ scheduler libraries?

20 March 2015 - 06:18 AM

Hi

 

Does anyone know of any good cross-platform Task scheduling frameworks / libraries available in C++? I'm looking for something similar to the C# task scheduler in terms of functionality.

 

I've looked into the Parallel Patterns Library but seems to low level for what I need.

 

Thanks.


Convenient approach to composite pattern in C++

03 March 2015 - 08:50 AM

Hi,

 

I have a class (A) which contains 'Listeners' or 'Observers' which need to be informed when certain things happen in class A. This currently involves iterating over all listeners and calling a certain notification method for example:

for(Listener listener : listeners)
{
   listener->InformOfSomething(1, 2, 3, 4);
}

I was wondering if there is any convenient way in c++11 to reduce this to a one-liner without having to create additional functions or use lambdas (or even remove the brackets ;)

 

The approach I'm looking for would be something similar to jQuery where functions act on collections of objects as opposed to single items. For e.g. maybe something as simple as this:

$(listeners).InformOfSomething(1, 2, 3, 4);

(please forgive the shameless copy of jQuery syntax)

 

Where the $ syntax acts in a similar way to range-based for loops and requires begin() and end() functions on the 'listeners' param which then iterates the collection and substitutes the item in the collection with first parameter of the proceeding function (or more specifically the 'this' pointer when using a class member function).

 

Anything like this in c++11 or any better way to do it?

 

Thanks.


Released my game open source...

16 February 2015 - 10:53 AM

Hi,

 

I just thought I would post this incase anyone is interested.

 

In August of last year I released a game on Google play and after its somewhat failed success I made it free-to-play and have now decided to release the source code which can be found here:

 

http://sourceforge.net/projects/dreadsarmy/

 

The game only made something like £15 overall which was about 5 months of work (a few months also working full-time for a games company). I suppose this is partly my fault for not capitalising on the micro-transactions approach most companies are taking nowadays (i.e Clash of Clans) and also for not having a marketing division tongue.png but hey.

 

Just thought this might be useful to some people starting in Android mobile development, the code is very simple, the majority of work was the artwork which was done in Blender.

 

Cheers.


Possible to be a (good) software developer without owning a PC?

03 February 2015 - 02:22 PM

Ok, so I'm thinking of selling my PC soon in an attempt to simplify my life a bit.

 

I'm tired of stressing out over some game that I know I will never complete and I find development these days to be a very isolated experience and somewhat detrimental to my mental health. I'm in the process of uploading my projects (completed and uncompleted) to sourceforge under an open source license.

 

I just wanted to know if you guys think it is still possible to be good software developer without actually owning a home PC? Granted it is what I do for a living :(

 

Cheers.


PARTNERS