我在使用curl和pthread时有线问题。我在给定的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.html Curl site reference
答案 0 :(得分:0)
尝试CURLOPT_NOSIGNAL应该解决问题