[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[GNUnet-SVN] [gnurl] 26/125: threaded-shared-conn.c: new example
From: |
gnunet |
Subject: |
[GNUnet-SVN] [gnurl] 26/125: threaded-shared-conn.c: new example |
Date: |
Sun, 21 Jan 2018 23:41:21 +0100 |
This is an automated email from the git hooks/post-receive script.
ng0 pushed a commit to branch master
in repository gnurl.
commit 5254d8bf2772627c6c8a4cecc9254dc7c9784225
Author: Daniel Stenberg <address@hidden>
AuthorDate: Tue Dec 5 23:31:47 2017 +0100
threaded-shared-conn.c: new example
---
docs/examples/Makefile.inc | 2 +-
docs/examples/threaded-shared-conn.c | 156 +++++++++++++++++++++++++++++++++++
2 files changed, 157 insertions(+), 1 deletion(-)
diff --git a/docs/examples/Makefile.inc b/docs/examples/Makefile.inc
index 075df9ea2..b560882f4 100644
--- a/docs/examples/Makefile.inc
+++ b/docs/examples/Makefile.inc
@@ -34,7 +34,7 @@ check_PROGRAMS = 10-at-a-time anyauthput cookie_interface
debug fileupload \
imap-tls imap-multi url2file sftpget ftpsget postinmemory http2-download \
http2-upload http2-serverpush getredirect ftpuploadfrommem \
ftpuploadresume sslbackend postit2-formadd multi-formadd \
- shared-connection-cache
+ shared-connection-cache threaded-shared-conn
# These examples require external dependencies that may not be commonly
# available on POSIX systems, so don't bother attempting to compile them here.
diff --git a/docs/examples/threaded-shared-conn.c
b/docs/examples/threaded-shared-conn.c
new file mode 100644
index 000000000..7ff832e84
--- /dev/null
+++ b/docs/examples/threaded-shared-conn.c
@@ -0,0 +1,156 @@
+/***************************************************************************
+ * _ _ ____ _
+ * Project ___| | | | _ \| |
+ * / __| | | | |_) | |
+ * | (__| |_| | _ <| |___
+ * \___|\___/|_| \_\_____|
+ *
+ * Copyright (C) 1998 - 2017, Daniel Stenberg, <address@hidden>, 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.haxx.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.
+ *
+ ***************************************************************************/
+/* <DESC>
+ * Multi-threaded transfers sharing a single connection pool
+ * </DESC>
+ *
+ * This example fires up NUM_THREADS threads and in each single thread, it
+ * downloads the same fixed URL a URL_ITERATIONS number of times. The received
+ * data is just thrown away. It sets up a single shared object that holds the
+ * connection cache and all easy threads in all threads share that same cache.
+ *
+ * This example uses pthreads for threads and mutexes, but should be easy to
+ * modify to use different thread/mutex system should you want to.
+ *
+ */
+
+#include <stdio.h>
+#include <pthread.h>
+#include <curl/curl.h>
+
+/*
+ URL to fetch. If you select HTTPS, you need to use a TLS backend with mutex
+ locks taken care of (OpenSSL 1.1.x, NSS, etc) or add SSL mutex callbacks!
+*/
+#define URL "http://localhost/4KB"
+
+/* number of threads to fire up in parallel */
+#define NUM_THREADS 67
+
+/* how many times each URL is transferred per thread */
+#define URL_ITERATIONS 11235
+
+static pthread_mutex_t connlock;
+
+static size_t write_db(void *ptr, size_t size, size_t nmemb, void *data)
+{
+ /* not interested in the downloaded bytes, return the size */
+ (void)ptr; /* unused */
+ (void)data; /* unused */
+ return (size_t)(size * nmemb);
+}
+
+static void lock_cb(CURL *handle, curl_lock_data data,
+ curl_lock_access access, void *userptr)
+{
+ (void)access; /* unused */
+ (void)userptr; /* unused */
+ (void)handle; /* unused */
+ (void)data; /* unused */
+ pthread_mutex_lock(&connlock);
+}
+
+static void unlock_cb(CURL *handle, curl_lock_data data,
+ void *userptr)
+{
+ (void)userptr; /* unused */
+ (void)handle; /* unused */
+ (void)data; /* unused */
+ pthread_mutex_unlock(&connlock);
+}
+
+static void init_locks(void)
+{
+ pthread_mutex_init(&connlock, NULL);
+}
+
+static void kill_locks(void)
+{
+ pthread_mutex_destroy(&connlock);
+}
+
+struct initurl {
+ const char *url;
+ CURLSH *share;
+ int threadno;
+};
+
+static void *run_thread(void *ptr)
+{
+ struct initurl *u = (struct initurl *)ptr;
+ int i;
+
+ for(i = 0; i < URL_ITERATIONS; i++) {
+ CURL *curl = curl_easy_init();
+ curl_easy_setopt(curl, CURLOPT_URL, u->url);
+ curl_easy_setopt(curl, CURLOPT_VERBOSE, 0L);
+ curl_easy_setopt(curl, CURLOPT_SHARE, u->share);
+ curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_db);
+ curl_easy_perform(curl); /* ignores error */
+ curl_easy_cleanup(curl);
+ fprintf(stderr, "Tread %d transfer %d\n", u->threadno, i);
+ }
+
+ return NULL;
+}
+
+int main(void)
+{
+ pthread_t tid[NUM_THREADS];
+ int i;
+ int error;
+ CURLSH *share;
+ struct initurl url[NUM_THREADS];
+
+ /* Must initialize libcurl before any threads are started */
+ curl_global_init(CURL_GLOBAL_ALL);
+
+ share = curl_share_init();
+ curl_share_setopt(share, CURLSHOPT_LOCKFUNC, lock_cb);
+ curl_share_setopt(share, CURLSHOPT_UNLOCKFUNC, unlock_cb);
+ curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_CONNECT);
+
+ init_locks();
+
+ for(i = 0; i< NUM_THREADS; i++) {
+ url[i].url = URL;
+ url[i].share = share;
+ url[i].threadno = i;
+ error = pthread_create(&tid[i], NULL, run_thread, &url[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, URL);
+ }
+
+ /* now wait for all threads to terminate */
+ for(i = 0; i< NUM_THREADS; i++) {
+ error = pthread_join(tid[i], NULL);
+ fprintf(stderr, "Thread %d terminated\n", i);
+ }
+
+ kill_locks();
+
+ curl_share_cleanup(share);
+ curl_global_cleanup();
+ return 0;
+}
--
To stop receiving notification emails like this one, please contact
address@hidden
- [GNUnet-SVN] [gnurl] 36/125: openldap: fix checksrc nits, (continued)
- [GNUnet-SVN] [gnurl] 36/125: openldap: fix checksrc nits, gnunet, 2018/01/21
- [GNUnet-SVN] [gnurl] 49/125: RESOLVE: output verbose text when trying to set a duplicate name, gnunet, 2018/01/21
- [GNUnet-SVN] [gnurl] 44/125: openssl: improve data-pending check for https proxy, gnunet, 2018/01/21
- [GNUnet-SVN] [gnurl] 29/125: curl-config: add --ssl-backends, gnunet, 2018/01/21
- [GNUnet-SVN] [gnurl] 23/125: sasl_getmesssage: make sure we have a long enough string to pass, gnunet, 2018/01/21
- [GNUnet-SVN] [gnurl] 24/125: libssh: remove dead code in sftp_qoute, gnunet, 2018/01/21
- [GNUnet-SVN] [gnurl] 34/125: examples: move threaded-shared-conn.c to the "complicated" ones, gnunet, 2018/01/21
- [GNUnet-SVN] [gnurl] 33/125: RELEASE-NOTES: synced with b261c44e8, gnunet, 2018/01/21
- [GNUnet-SVN] [gnurl] 20/125: libssh: fixed dereference in statvfs access, gnunet, 2018/01/21
- [GNUnet-SVN] [gnurl] 27/125: threaded-shared-conn.c: fixed typo in commenta, gnunet, 2018/01/21
- [GNUnet-SVN] [gnurl] 26/125: threaded-shared-conn.c: new example,
gnunet <=
- [GNUnet-SVN] [gnurl] 54/125: tests: mark data files as non-executable in git, gnunet, 2018/01/21
- [GNUnet-SVN] [gnurl] 50/125: openssl: Disable file buffering for Win32 SSLKEYLOGFILE, gnunet, 2018/01/21
- [GNUnet-SVN] [gnurl] 51/125: mailmap: fixup two old git Author "aliases", gnunet, 2018/01/21
- [GNUnet-SVN] [gnurl] 35/125: openldap: add commented out debug possibilities, gnunet, 2018/01/21
- [GNUnet-SVN] [gnurl] 53/125: tests: update .gitignore for libtests, gnunet, 2018/01/21
- [GNUnet-SVN] [gnurl] 69/125: krb5: fix a potential access of uninitialized memory, gnunet, 2018/01/21
- [GNUnet-SVN] [gnurl] 61/125: vtls: replaced getenv() with curl_getenv(), gnunet, 2018/01/21
- [GNUnet-SVN] [gnurl] 58/125: curl.1: mention http:// and https:// as valid proxy prefixes, gnunet, 2018/01/21
- [GNUnet-SVN] [gnurl] 56/125: CURLOPT_DNS_LOCAL_IP4.3: fixed the seel also to not self-reference, gnunet, 2018/01/21
- [GNUnet-SVN] [gnurl] 30/125: configure: check for netinet/in6.h, gnunet, 2018/01/21