mirror of
https://github.com/curl/curl.git
synced 2026-04-13 00:31:41 +08:00
Build these examples when the necessary dependencies are present:
- cacertinmem, usercertinmem (OpenSSL/fork)
- multi-uv (libuv)
- multithread, threaded-ssl (pthread)
- sessioninfo (GnuTLS)
Indicate the necessary dependency via a `Required:` comment placed in
the source file. A single dependency per source is supported as of now.
The name of the dependency should match the variable used within
the cmake scripts, which in turn matches the macro used in the config
header. E.g. for GnuTLS it's `USE_GNUTLS`.
Also:
- GHA/macos: build examples in two job to test GnuTLS and pthread ones.
- GHA/linux: enable libuv to test it with examples.
Follow-up to 6bb7714032 #18914
Closes #18909
100 lines
2.7 KiB
C
100 lines
2.7 KiB
C
/***************************************************************************
|
|
* _ _ ____ _
|
|
* Project ___| | | | _ \| |
|
|
* / __| | | | |_) | |
|
|
* | (__| |_| | _ <| |___
|
|
* \___|\___/|_| \_\_____|
|
|
*
|
|
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
|
|
*
|
|
* This software is licensed as described in the file COPYING, which
|
|
* you should have received as part of this distribution. The terms
|
|
* are also available at https://curl.se/docs/copyright.html.
|
|
*
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
*
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
* KIND, either express or implied.
|
|
*
|
|
* SPDX-License-Identifier: curl
|
|
*
|
|
***************************************************************************/
|
|
/* <DESC>
|
|
* A multi-threaded program using pthreads to fetch several files at once
|
|
* </DESC>
|
|
*/
|
|
|
|
/* Requires: HAVE_PTHREAD_H */
|
|
|
|
#include <stdio.h>
|
|
#include <pthread.h>
|
|
#include <curl/curl.h>
|
|
|
|
#define NUMT 4
|
|
|
|
/*
|
|
List of URLs to fetch.
|
|
|
|
If you intend to use an SSL-based protocol here you might need to setup TLS
|
|
library mutex callbacks as described here:
|
|
|
|
https://curl.se/libcurl/c/threadsafe.html
|
|
|
|
*/
|
|
static const char * const urls[NUMT]= {
|
|
"https://curl.se/",
|
|
"ftp://example.com/",
|
|
"https://example.net/",
|
|
"www.example"
|
|
};
|
|
|
|
static void *pull_one_url(void *pindex)
|
|
{
|
|
int i = *(int *)pindex;
|
|
CURL *curl;
|
|
|
|
curl = curl_easy_init();
|
|
curl_easy_setopt(curl, CURLOPT_URL, urls[i]);
|
|
curl_easy_perform(curl); /* ignores error */
|
|
curl_easy_cleanup(curl);
|
|
|
|
return NULL;
|
|
}
|
|
|
|
|
|
/*
|
|
int pthread_create(pthread_t *new_thread_ID,
|
|
const pthread_attr_t *attr,
|
|
void * (*start_func)(void *), void *arg);
|
|
*/
|
|
|
|
int main(void)
|
|
{
|
|
pthread_t tid[NUMT];
|
|
int i;
|
|
|
|
/* Must initialize libcurl before any threads are started */
|
|
curl_global_init(CURL_GLOBAL_ALL);
|
|
|
|
for(i = 0; i < NUMT; i++) {
|
|
int error = pthread_create(&tid[i],
|
|
NULL, /* default attributes please */
|
|
pull_one_url,
|
|
(void *)&i);
|
|
if(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]);
|
|
}
|
|
|
|
/* now wait for all threads to terminate */
|
|
for(i = 0; i < NUMT; i++) {
|
|
pthread_join(tid[i], NULL);
|
|
fprintf(stderr, "Thread %d terminated\n", i);
|
|
}
|
|
curl_global_cleanup();
|
|
return 0;
|
|
}
|