Beta test the new forum at
Do you need help?
Simutrans Wiki Manual can help you to play and extend Simutrans. In 9 languages.

Thread local clipping_info_t

Started by iv5343, December 01, 2023, 12:35:31 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.


Hi! The display functions are multithreaded and each display thread has its own struct clipping_info_t.  They're stored in a global array, an index is passed down through several function call levels, and there's a collection of preprocessor macros to eliminate this parameter if the program is compiled without thread support, for size and performance reasons I presume.

Simutrans seems to require a C++14 compiler now.  C++11 introduced the thread_local storage class specifier, it puts variables in a thread private data segment.  They're accessed like static ones, constructors and destructors are called at appropriate times.  With a thread local clip struct there's no need for an array, the overhead of passing down an index and computing an array offset is gone, and the code is cleaner.  Simutrans uses POSIX threads, if thread_local is not supported on some systems I suppose pthreads TLS could be used.

Why is the clip struct not thread_local?


Because that was done when simutrans was not C++14. A lot of stuff is even older. Also the servers are almost always single threaded (since the main game engine does not thread), so the focus was to remove the thread overhead rather.

Anyway, I started programming before C99 even existed (about 15 years before) and I have no idea of recent trends. If this works better, yes, why not. It is just that one thing I learned over all the years, to never touch a working code unless there is a very good reason.

In this case, there could be indeed a good performance gain (and getting rid of the defines as well). So if there is a patch, I would certainly try it out.