0
我有有线的问题,同时使用curl与并行线程完成后崩溃。我提到的官方代码是URL。代码崩溃,如果我添加CURLOPT_TIMEOUT
或CURLOPT_CONNECTTIMEOUT
。还有一件有趣的事是,当我的回应完成后,会发生这种崩溃。 :(。如果有人已经通过这个走后,我想知道,下面是我的代码,我从cocos2dX
现场运行。卷曲请求的请求
- 确保该代码运行,等待30秒才能完成,应用程序会崩溃。但是,如果超时选项排除此故障不会来了。下面是代码片段造成的问题。
static void *pull_one_url(void *url)
{
CURL *curl;
curl = curl_easy_init();
//curl_easy_setopt(curl, CURLOPT_TIMEOUT, 30); // will crash if enabled just wait for 30 second to pass away
//curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 30); // will crash if enabled just wait for 30 second to pass away
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_perform(curl); /* ignores error */
curl_easy_cleanup(curl);
return NULL;
}
void mainTest()
{
pthread_t tid[NUMT];
int i;
int error;
/* Must initialize libcurl before any threads are started */
curl_global_init(CURL_GLOBAL_ALL);
std::cout<<"go go ";
for(i=0; i< NUMT; i++)
{
error = pthread_create(&tid[i],
NULL, /* default attributes please */
pull_one_url,
(void *)urls[i]);
if(0 != error)
fprintf(stderr, "Couldn't run thread number %d, errno %d\n", i, error);
else
fprintf(stderr, "Thread %d, gets %s\n", i, urls[i]);
}
std::cout<<"come come ";
/* now wait for all threads to terminate */
for(i=0; i< NUMT; i++) {
error = pthread_join(tid[i], NULL);
fprintf(stderr, "Thread %d terminated\n", i);
}
}
http://curl.haxx.se/libcurl/c/multithread.htmlCurl site reference