xref: /curl/docs/libcurl/curl_multi_timeout.md (revision e3fe0200)
1---
2c: Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
3SPDX-License-Identifier: curl
4Title: curl_multi_timeout
5Section: 3
6Source: libcurl
7See-also:
8  - curl_multi_fdset (3)
9  - curl_multi_info_read (3)
10  - curl_multi_setopt (3)
11  - curl_multi_socket (3)
12Protocol:
13  - All
14---
15
16# NAME
17
18curl_multi_timeout - how long to wait for action before proceeding
19
20# SYNOPSIS
21
22~~~c
23#include <curl/curl.h>
24
25CURLMcode curl_multi_timeout(CURLM *multi_handle, long *timeout);
26~~~
27
28# DESCRIPTION
29
30An application using the libcurl multi interface should call
31curl_multi_timeout(3) to figure out how long it should wait for socket
32actions - at most - before proceeding.
33
34Proceeding means either doing the socket-style timeout action: call the
35curl_multi_socket_action(3) function with the **sockfd** argument set
36to CURL_SOCKET_TIMEOUT, or call curl_multi_perform(3) if you are using
37the simpler and older multi interface approach.
38
39The timeout value returned in the long **timeout** points to, is in number
40of milliseconds at this moment. If 0, it means you should proceed immediately
41without waiting for anything. If it returns -1, there is no timeout at all set.
42
43An application that uses the *multi_socket* API should not use this function.
44It should instead use the CURLMOPT_TIMERFUNCTION(3) option for proper and
45desired behavior.
46
47Note: if libcurl returns a -1 timeout here, it just means that libcurl
48currently has no stored timeout value. You must not wait too long (more than a
49few seconds perhaps) before you call curl_multi_perform(3) again.
50
51# EXAMPLE
52
53~~~c
54int main(void)
55{
56  struct timeval timeout;
57  long timeo;
58  fd_set fdread;
59  fd_set fdwrite;
60  fd_set fdexcep;
61  int maxfd;
62  CURLM *multi = curl_multi_init();
63
64  curl_multi_timeout(multi, &timeo);
65  if(timeo < 0)
66    /* no set timeout, use a default */
67    timeo = 980;
68
69  timeout.tv_sec = timeo / 1000;
70  timeout.tv_usec = (timeo % 1000) * 1000;
71
72  /* wait for activities no longer than the set timeout */
73  select(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout);
74}
75~~~
76
77# TYPICAL USAGE
78
79Call curl_multi_timeout(3), then wait for action on the sockets. Figure
80out which sockets to wait for by calling curl_multi_fdset(3).
81
82When there is activity or timeout, call curl_multi_perform(3) and then
83loop - until all transfers are complete.
84
85# AVAILABILITY
86
87This function was added in libcurl 7.15.4.
88
89# RETURN VALUE
90
91The standard CURLMcode for multi interface error codes.
92