Debugging Multithreaded Programs

Blitz3D Forums/Blitz3D Programming/Debugging Multithreaded Programs

xtremegamr(Posted 2011) [#1]
When I first started messing around with FastPointer, I used it to make multithreaded web servers. That's cool and all, but now I want to try and incorporate it into a game project.

However, I've run into a pretty serious problem with debugging; I've been finding that either the program will run perfectly, or it will just crash with no indication as to what is wrong.

So my question is, is there any feasible way to debug and track down errors in multithreaded Blitz programs?

Last edited 2011


Yasha(Posted 2011) [#2]
Well your biggest obstacle is that the debugger flat-out won't work with them (this isn't unique to multithreading: the debugger won't work with code called via function pointer anyway - although not being designed for multiple threads means that would likely be a problem too). So you're limited to printing debug messages at strategic points in your program.

Three things come to mind:

1) Does Print work? I haven't tested it, so don't know what kind of havoc might result from two threads trying to print at once (obviously only relevant if Print is usable in the context of your graphical setup).

2) Similarly, if multiple threads can be forced by DirectX to play nice when accessing the graphics object, you could just have a different horizontal alignment for the data from each thread when using a very simple Text debug display over your graphics. Again, I haven't tried, to see if this even works.

3) A more complex solution might be to roll-your-own Debug console. You could use a second application as a listener, and have each thread connect to it with a network stream or something, and write debug messages to the streams (to then be displayed by the second process). Since such streams are designed to work between multiple machines it would work as a brute-force way to reintegrate the messages into a single thread for easy display.

4) Uh... don't use multithreading in commercial Blitz3D code. Seriously: the feature isn't reliable and doesn't work properly with the language in any way. If you need multithreading for something you intend to release, you should seriously consider using some other language. (Obviously though, for personal projects, the impossible should never be an obstacle.)


xtremegamr(Posted 2011) [#3]
Does Print work?

Print DOES work -- It's just possible to get messages that are smashed together when 2 threads access the thread stream at the same time.

For example, in Thread 1:
Print "Hi Yasha!"


and in thread 2:
Print "Hi xtremegamr!"


sometimes results in:
Hi Hi Yasxtremegamr!
ha!


..using a very simple Text debug display over your graphics...

I haven't tried using Text from multiple threads before, though. It seems like a pretty good idea.

A more complex solution might be to roll-your-own Debug console...

I've tried this, too. It works pretty well...assuming that the program doesn't randomly crash.

...don't use multithreading in commercial Blitz3D code

Meh. I guess that this is true, given all of the hacks\workarounds I have to use to debug. I'll probably end up learning the DirectX or OpenGL API's and switching to C++ eventually, anyway.

Thanks for the help!

P.S. That was four things :P


Rroff(Posted 2011) [#4]
You need a very good understanding of how threads work to use fastpointer for threading safely especially the async nature of threading and access to resources, IO and variables i.e. 2 threads trying to read/write a value to the same location at the same time cause all kinds of issues.

Been very thankful for fastpointer tho lately as I decided to use the OS console for debugging purposes (kernel32 function AllocConsole()) and the kernel32 function ReadConsole() halts main thread execution until certain input rules are met, so I threaded that using fast pointer :D (my game engine accepts commands typed into the console window as well as the ingame console when in developer mode - its handy for testing stuff before the ingame UI is setup)

EDIT: For anyone interested AllocConsole() only works from a compiled exe and won't work when the code is run from the blitz IDE.

Last edited 2011