I'm using libcurl in C++, and I'm calling curl_easy_perform
in a separate thread from my UI using Boost.Thread.
The main UI has a cancel button that I'd like to be perfectly responsive (i.e., when a user clicks on it, it should immediately react). I have read, write, and progress callbacks set up to read an atomic should_cancel
variable (as in this question), but there are two problems:
There's often a very small (but noticeable) delay from when cancel is pressed to when the curl operation completes.
Occasionally, there's a very long (sometimes interminable) delay. In this case, either:
a. the progress, read, and write callbacks simply aren't called for a long time, or
b. the progress callback is called, I return a nonzero value (meaning it should terminate), but the curl operation doesn't complete for a while longer (in fact, the progress function is called again in the meantime!)
So:
- Why do the long delays happen (especially without calling the progress function)?
- What should I do instead to allow the cancel button to react properly?
One possibility is to tell the UI that the cancel operation succeeded, but keep running the curl thread in the background until it cancels. The problem with this (I think) is that it forces the should_cancel
variable to be global, instead of scoped to the dialog box where the operation began.