Threading in Wesnoth

Discussion of all aspects of the game engine, including development of new and existing features.

Moderator: Forum Moderators

Post Reply
Dave
Founding Developer
Posts: 7071
Joined: August 17th, 2003, 5:07 am
Location: Seattle
Contact:

Threading in Wesnoth

Post by Dave »

I've just added some experimental networking code which makes use of something we've avoided so far in Wesnoth development......threading.

Sending and receiving data to and from the network can be a time-consuming, but not CPU intensive operation. Thus in a single thread, if you send or receive alot of network data, the game will pause whie the transfer takes place.

The code I've added makes sending of most network data occur in seperate threads, so the game remains responsive. For instance, when the game sends data to other players in a multiplayer game, instead of pausing for a moment, the work will be delegated to another thread, and the game will continue in the main thread.

Since this is in the general network code, the server, and perhaps most importantly, the campaign server, will utilise these changes too. The code actually allows a configurable number of worker threads to be started. The threads 'sleep', waiting for work to do. When a network send is initialized, one of the worker threads is woken and does the task. If all worker threads are already occupied, the data is stored in a queue, and when a thread is available, it will send the data.

One of the nice things about this code, is all the threading is encapsulated within the network module, and other areas of the code don't have to worry about it.

If this code works well in testing, I will look into extending the worker thread functionality to also receive network data in seperate threads.

This code will hopefully make the server much more scalable.

If anyone would like to review the code I've written for correctness, they're welcome to :) it's in network_worker.cpp/.hpp

David
“At Gambling, the deadly sin is to mistake bad play for bad luck.” -- Ian Fleming
Post Reply