我在使用 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);
}
}