Threading on WZ2100

Discuss the future of Warzone 2100 with us.
cajadas
Trained
Trained
Posts: 56
Joined: 01 Mar 2011, 17:33

Threading on WZ2100

Post by cajadas »

Hey,

I've implemented a thread that starts in the stageThreeInitialise and is shutdown on stageThreeShutdown (src/init.cpp). The purpose of this thread is to make some calculations during the game in a cicle. The only user that launchs this thread is the host. To implement my thread i've looked to the fpath code (src/fpath.cpp).

As i've started testing my thread i kept getting segfault errors. To figure out if the problem was weak access control to global structures or variables, i've started to comment my code until there was just a simple debug message. Although my message gets printed repeatedly on the console when the game starts, short after i keep getting segmentation fault errors.

I've wondered if this problem could be the result of weak access control, but i'm not accessing any variables during the debug print. The wierdest thing is that if i don't have any debug messages, the thread runs smoothly, but i can't know if the calculations are correct.

Any thoughts?
Thanks.
cajadas
Trained
Trained
Posts: 56
Joined: 01 Mar 2011, 17:33

Re: Threading on WZ2100

Post by cajadas »

I realised that my problem may be related to the fact that i've always tested my thread on debug mode. When i tested with debug off i've experienced no segmentation fault. Are there global variables accessed during the debug routine?
Cyp
Evitcani
Evitcani
Posts: 784
Joined: 17 Jan 2010, 23:35

Re: Threading on WZ2100

Post by Cyp »

Try doing a "make clean", to clear away all compiled files. If some files are compiled in debug mode, and other files are compiled in release mode, it will do strange stuff and crash. This is because the DROID (don't remember if any other) structure has different members in release and debug.
cajadas
Trained
Trained
Posts: 56
Joined: 01 Mar 2011, 17:33

Re: Threading on WZ2100

Post by cajadas »

what do you mean "compile in debug mode"? I thought compilation was the same for release and debug and only on execution i would specify the mode.

Anyway, i still think it has to do with concurrent access to some variable used on the debug function because i would except this output:

Code: Select all

...
vfc     |07:45:08: [someFunction] FIRST
vfc     |07:45:08: [otherFunction] SECOND
...
and i get:

Code: Select all

...
vfc     |07:45:08: [someFunction] FIRST
SECOND[otherFunction] ...
One function is executed in the main thread and the other on the thread i've implemented. The fpath thread have no debug calls but who implemented it probably had the same problem.
Cyp
Evitcani
Evitcani
Posts: 784
Joined: 17 Jan 2010, 23:35

Re: Threading on WZ2100

Post by Cyp »

Oh, in that case, then maybe print to a thread-safe buffer, and then print that buffer from the main thread.
stiv
Warzone 2100 Team Member
Warzone 2100 Team Member
Posts: 876
Joined: 18 Jul 2008, 04:41
Location: 45N 86W

Re: Threading on WZ2100

Post by stiv »

what do you mean "compile in debug mode"? I thought compilation was the same for release and debug
Compiling in debug mode adds some extra information and has the effect of moving things around in memory. Occasionally, this makes pointer bugs go away (Heisenbugs).