View Full Version : Mutlithreading with QThread and networking failing

22nd November 2011, 08:51

I am building an application which requires a certain number of threads to works. The main idea is to retrieve data from network sites at the same time with several working threads.
Here is the main idea of the class definition

class workingThread:public QThread
ThreadParams params;
void run();
void SetParams(int param1,int param2,int etc)
void jobdone(int threadIndex)

The idea is to have many instances of the same thread and for each thread it is a assigned a different task, in this case each task corresponds
getting data from a web-site

workingThread threads[10];

The slot receiving the signal JobDone reassigns the task to thread in this case it assigns a new site to retrieve data from, so if the user requires
getting data from 100 different sites it will be distributed along the threads. At this point everything works fine.
The problem comes when all threads reach the bottleneck function at the same time(curl_easy_perform) in which they retrieve data. Some of these threads are terminated while others skip code. In both cases the thread never send the signal jobdone nor the signal "finished". I do not know whether there is a problem with network connections and threads specially with the use of cURL functions. This happens equally in both Windows and Linux.

22nd November 2011, 09:27
Does each thread has its own curl handle returned from curl_easy_init? I ask because the CURL manual (http://curl.haxx.se/libcurl/c/curl_easy_perform.html) says this about curl_easy_perform:
You must never call this function simultaneously from two places using the same handle. Let the function return first before invoking it another time. If you want parallel transfers, you must use several curl handles.

22nd November 2011, 09:44
yes, each call to curl has its own handle. The thread calls a function get_contents which has the CURL handle as local variable which is destroyed when the get_content functions ends.