Keeping an event driven program responsive when dealing with potentially long operations can be difficult. There are two ways of doing this: multi-threading and asynchronous operations. Each is best suited to different tasks with some overlap between them. Network IO has already been handled mostly using async-io with threading being used for the connection setup due to api limitations. File IO is a different story. Pan's usage of files doesn't really lend itself to async-io therefore threading is used. Well, at present only attachment saving is handled with other threads. The other slow part is the loading & saving of the group header files. Currently this code runs in the main thread which means that it stalls network io, especially when dealing with large groups which can easily take minutes.
A couple of articles by Herb Sutter have given me an idea for solving this problem, Active Objects and Prefer Futures and Callbacks. The basic idea is to have an active object that handles loading and saving groups. The hard part will be making the parts that need to load groups async. The saving of groups might be easy if that is trgigered by a ref count. The object would be something like this:
A couple of articles by Herb Sutter have given me an idea for solving this problem, Active Objects and Prefer Futures and Callbacks. The basic idea is to have an active object that handles loading and saving groups. The hard part will be making the parts that need to load groups async. The saving of groups might be easy if that is trgigered by a ref count. The object would be something like this:
class GroupHandler {
public:
typedef std::tr1::function<void(group*)> Callback;
void load_group(quark group, Callback cb)
{queue.add(1,cb);}
void save_group(quark group)
{queue.add(2);}
~GroupHandler(){ join(thread); }
private:
virtual void do_load(quark group, Callback);
virtual void do_save(quark group);
bool done;
Queue queue;
Thread *thread;
}