【问题标题】:Curl requests in pThread crashes after completion of requestpThread 中的 curl 请求在请求完成后崩溃
【发布时间】:2012-12-04 12:00:04
【问题描述】:

我在使用 curl 和 pthread 时遇到了有线问题。我在给定的URL 引用了官方代码。如果我添加CURLOPT_TIMEOUTCURLOPT_CONNECTTIMEOUT,代码会崩溃。还有一个有趣的事情是,当我的响应完全完成时,会发生这种崩溃。 :(。我想知道是否有人经历过这个。下面是我从cocos2dX场景运行的代码。

-确保代码运行,等待 30 秒完成,应用程序会崩溃。但是,如果排除超时选项,则不会发生此崩溃。下面是导致问题的代码 sn-p。

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

【问题讨论】:

    标签: c++ curl cocos2d-x


    【解决方案1】:

    尝试 CURLOPT_NOSIGNAL 应该可以解决问题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-05
      • 2022-01-01
      • 2013-06-30
      • 2011-03-20
      • 1970-01-01
      • 2017-04-09
      • 1970-01-01
      • 2022-11-30
      相关资源
      最近更新 更多