Gamestudio Links
Zorro Links
Newest Posts
Help with plotting multiple ZigZag
by degenerate_762. 04/30/24 23:23
M1 Oversampling
by 11honza11. 04/30/24 08:16
Trading Journey
by howardR. 04/28/24 09:55
Zorro Trader GPT
by TipmyPip. 04/27/24 13:50
Data from CSV not parsed correctly
by jcl. 04/26/24 11:18
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
3 registered members (dr_panther, Ayumi, 7th_zorro), 877 guests, and 3 spiders.
Key: Admin, Global Mod, Mod
Newest Members
firatv, wandaluciaia, Mega_Rod, EternallyCurious, howardR
19050 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 5 of 6 1 2 3 4 5 6
Re: What's (currently) your favourite snippet of code? [Re: Kartoffel] #444849
08/19/14 20:31
08/19/14 20:31
Joined: Aug 2003
Posts: 7,439
Red Dwarf
Michael_Schwarz Offline
Senior Expert
Michael_Schwarz  Offline
Senior Expert

Joined: Aug 2003
Posts: 7,439
Red Dwarf
Damn we forgot the Ackchievements in Ackmania!


"Sometimes JCL reminds me of Notch, but more competent" ~ Kiyaku
Re: What's (currently) your favourite snippet of code? [Re: Michael_Schwarz] #444850
08/19/14 20:33
08/19/14 20:33
Joined: Sep 2003
Posts: 9,859
F
FBL Offline
Senior Expert
FBL  Offline
Senior Expert
F

Joined: Sep 2003
Posts: 9,859
We forgot on intention grin
We wanted to have more time for the actual game (honestly!).

Re: What's (currently) your favourite snippet of code? [Re: FBL] #446193
10/07/14 22:54
10/07/14 22:54
Joined: Apr 2007
Posts: 3,751
Canada
WretchedSid Offline
Expert
WretchedSid  Offline
Expert

Joined: Apr 2007
Posts: 3,751
Canada
This lock free ringbuffer implementation. Supports one reader and one writer.

Code:
template<class T, size_t Size>
class lock_free_ring_buffer
{
public:
	lock_free_ring_buffer() :
		_head(0),
		_tail(0)
	{}
	
	bool push(const T &val)
	{
		size_t tail = _tail.load(std::memory_order_relaxed);
		size_t next = (index + 1) % capacity;
		
		if(next != _head.load(std::memory_order_acquire))
		{
			_buffer[tail] = val;
			_tail.store(next, std::memory_order_release);
			
			return true;
		}
		
		return false;
	}
	
	bool pop(T &val)
	{
		size_t head = _head.load(std::memory_order_relaxed);
		
		if(head != _tail.load(std::memory_order_acquire))
		{
			val = std::move(_buffer[head]);
			_head.store((index + 1) % capacity, std::memory_order_release);
			
			return true;
		}
		
		return false;
	}
	
	bool was_empty() const
	{
		return (_head.load() == _tail.load());
	}
	
private:
	enum { capacity = Size + 1 };
	
	std::atomic<size_t> _head;
	std::atomic<size_t> _tail;
	
	std::array<T, capacity> _buffer;
};



Shitlord by trade and passion. Graphics programmer at Laminar Research.
I write blog posts at feresignum.com
Re: What's (currently) your favourite snippet of code? [Re: WretchedSid] #446198
10/08/14 01:27
10/08/14 01:27
Joined: Nov 2007
Posts: 2,568
Germany, BW, Stuttgart
MasterQ32 Offline
Expert
MasterQ32  Offline
Expert

Joined: Nov 2007
Posts: 2,568
Germany, BW, Stuttgart
C# and some kind of unicode code:


Visit my site: www.masterq32.de
Re: What's (currently) your favourite snippet of code? [Re: MasterQ32] #446203
10/08/14 04:26
10/08/14 04:26
Joined: Dec 2008
Posts: 1,660
North America
Redeemer Offline
Serious User
Redeemer  Offline
Serious User

Joined: Dec 2008
Posts: 1,660
North America
Something I often take for granted as an american is how basically all programming languages worth mentioning were designed in English by default.

There was no moment for me when I learned what certain keywords really meant because for me it was always intuitive.


Eats commas for breakfast.

Play Barony: Cursed Edition!
Re: What's (currently) your favourite snippet of code? [Re: Redeemer] #446229
10/08/14 15:45
10/08/14 15:45
Joined: Sep 2003
Posts: 9,859
F
FBL Offline
Senior Expert
FBL  Offline
Senior Expert
F

Joined: Sep 2003
Posts: 9,859
Not actually a snippet, but the explanation of an atrocity...

http://stackoverflow.com/questions/54137...-function-calls

Re: What's (currently) your favourite snippet of code? [Re: FBL] #446230
10/08/14 15:50
10/08/14 15:50
Joined: Dec 2008
Posts: 1,660
North America
Redeemer Offline
Serious User
Redeemer  Offline
Serious User

Joined: Dec 2008
Posts: 1,660
North America
I hate languages that do this, maintaining multiple valid approaches to key syntax is pointless and retarded and only serves to break down the usefulness of a language to describe implementations in a consistent and predictable manner.


Eats commas for breakfast.

Play Barony: Cursed Edition!
Re: What's (currently) your favourite snippet of code? [Re: Redeemer] #446236
10/08/14 16:57
10/08/14 16:57
Joined: Sep 2003
Posts: 9,859
F
FBL Offline
Senior Expert
FBL  Offline
Senior Expert
F

Joined: Sep 2003
Posts: 9,859
yes.

Re: What's (currently) your favourite snippet of code? [Re: FBL] #451002
04/27/15 00:02
04/27/15 00:02
Joined: Apr 2007
Posts: 3,751
Canada
WretchedSid Offline
Expert
WretchedSid  Offline
Expert

Joined: Apr 2007
Posts: 3,751
Canada
The following reference counting implementation:

Code:
Object *Object::Retain()
{
	_refCount.fetch_add(1, std::memory_order_relaxed);
	return this;
}

void Object::Release()
{
	if(_refCount.fetch_sub(1, std::memory_order_release) == 1)
	{
		std::atomic_thread_fence(std::memory_order_acquire); // Synchronize all accesses to this object before deleting it

		CleanUp();
		delete this;
	}
}



For the longest time I did reference counting with strong memory fences (std::memory_order_acq_rel), until I learned that relaxed ordering works with read-modify-write operations just fine.
The release operation is a bit more complicated because if this is the last reference the thread holds to the object, it needs to have a full release operation. However, only the final thread that relinquishes the reference needs an acquire operation to make sure all changes to the object are observed before entering the destructor.

Clever as fuck. The basic idea comes from Herb Sutter's Atomic<> Weapons talk, although he used std::memory_order_acq_rel for dropping a reference.


Of course this doesn't do anything for strongly ordered architectures like x86, but it produces much better code on weakly ordered CPUs like ARM.


Shitlord by trade and passion. Graphics programmer at Laminar Research.
I write blog posts at feresignum.com
Re: What's (currently) your favourite snippet of code? [Re: WretchedSid] #451019
04/27/15 07:26
04/27/15 07:26
Joined: Nov 2008
Posts: 946
T
the_clown Offline OP
User
the_clown  Offline OP
User
T

Joined: Nov 2008
Posts: 946
Funny enough, the last thing I've implemented in C++ was a reference counted object class to see how that stuff is put together - without any synchronization though, thread safe code is something I still have to explore.

I have one question though (very offtopic but hey these forums are more or less anarchistic by now anyways), Sid: You have this nice Object class in Rayne, which gives you all these nifty features like reflection, reference counting, etc, but the fact that they're reference counted enforces allocation of objects on the heap - so how do you make that work nicely with cache-friendly processing of objects? I suppose you're using a custom allocator (or multiple) to ensure objects are allocated close to each other, but I still can't imagine that this is as optimal as it could be with tightly packed object buffers.
Or, to rephrase my actual question in a way so you could answer it with a yes or a no, did the comfortable features of having this base object class outweigh the possible performance gains of having a less abstracted, but more data-oriented object model for you? Or do you perform some kind of programming magic in the background so the nice API of your object model just hides an optimized implementation?

I hope my question is somewhat clear, I'm doing a lot of research regarding different object models currently and I'm basically just trying to squeeze every sponge I can get into my hands, if you will, for first hand experiences. tongue (Especially because most opinions you find on public blogs are those of very very C-affine data-orientation fanatics, which may be a fascinating and important topic, but it's not really helpful to simply be told to scrap all OOP and make everything a system stacked onto another system then feed them data, without any explanations on how to architect such a structure in a codebase that's still somewhat flexible).

Page 5 of 6 1 2 3 4 5 6

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1