1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.haxx.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 * Multi-threaded transfers sharing a single connection pool
26 * This example fires up NUM_THREADS threads and in each single thread, it
27 * downloads the same fixed URL a URL_ITERATIONS number of times. The received
28 * data is just thrown away. It sets up a single shared object that holds the
29 * connection cache and all easy handles in all threads share that same cache.
31 * This example uses pthreads for threads and mutexes, but should be easy to
32 * modify to use different thread/mutex system should you want to.
38 #include <curl/curl.h>
41 URL to fetch. If you select HTTPS, you need to use a TLS backend with mutex
42 locks taken care of (OpenSSL 1.1.x, NSS, etc) or add SSL mutex callbacks!
44 #define URL "http://localhost/4KB"
46 /* number of threads to fire up in parallel */
47 #define NUM_THREADS 67
49 /* how many times each URL is transferred per thread */
50 #define URL_ITERATIONS 11235
52 static pthread_mutex_t connlock;
54 static size_t write_db(void *ptr, size_t size, size_t nmemb, void *data)
56 /* not interested in the downloaded bytes, return the size */
57 (void)ptr; /* unused */
58 (void)data; /* unused */
59 return (size_t)(size * nmemb);
62 static void lock_cb(CURL *handle, curl_lock_data data,
63 curl_lock_access access, void *userptr)
65 (void)access; /* unused */
66 (void)userptr; /* unused */
67 (void)handle; /* unused */
68 (void)data; /* unused */
69 pthread_mutex_lock(&connlock);
72 static void unlock_cb(CURL *handle, curl_lock_data data,
75 (void)userptr; /* unused */
76 (void)handle; /* unused */
77 (void)data; /* unused */
78 pthread_mutex_unlock(&connlock);
81 static void init_locks(void)
83 pthread_mutex_init(&connlock, NULL);
86 static void kill_locks(void)
88 pthread_mutex_destroy(&connlock);
97 static void *run_thread(void *ptr)
99 struct initurl *u = (struct initurl *)ptr;
102 for(i = 0; i < URL_ITERATIONS; i++) {
103 CURL *curl = curl_easy_init();
104 curl_easy_setopt(curl, CURLOPT_URL, u->url);
105 curl_easy_setopt(curl, CURLOPT_VERBOSE, 0L);
106 curl_easy_setopt(curl, CURLOPT_SHARE, u->share);
107 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_db);
108 curl_easy_perform(curl); /* ignores error */
109 curl_easy_cleanup(curl);
110 fprintf(stderr, "Tread %d transfer %d\n", u->threadno, i);
118 pthread_t tid[NUM_THREADS];
122 struct initurl url[NUM_THREADS];
124 /* Must initialize libcurl before any threads are started */
125 curl_global_init(CURL_GLOBAL_ALL);
127 share = curl_share_init();
128 curl_share_setopt(share, CURLSHOPT_LOCKFUNC, lock_cb);
129 curl_share_setopt(share, CURLSHOPT_UNLOCKFUNC, unlock_cb);
130 curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_CONNECT);
134 for(i = 0; i< NUM_THREADS; i++) {
136 url[i].share = share;
138 error = pthread_create(&tid[i], NULL, run_thread, &url[i]);
140 fprintf(stderr, "Couldn't run thread number %d, errno %d\n", i, error);
142 fprintf(stderr, "Thread %d, gets %s\n", i, URL);
145 /* now wait for all threads to terminate */
146 for(i = 0; i< NUM_THREADS; i++) {
147 error = pthread_join(tid[i], NULL);
148 fprintf(stderr, "Thread %d terminated\n", i);
153 curl_share_cleanup(share);
154 curl_global_cleanup();