CURLSHOPT_LOCKFUNC(3)

πŸ’… CURLSHOPT_LOCKFUNC(3) β€” like, the mutex lock callback one, bestie

okay so real talk: this is a page from Library Functions Manual (that's the manual name, you're welcome ✨) and it's all about CURLSHOPT_LOCKFUNC β€” the lock callback your share object needs when you're out here living that multithreaded life. πŸ§΅πŸ’–


🌸 NAME 🌸

CURLSHOPT_LOCKFUNC β€” the mutex lock callback, periodt πŸŽ€


✨ SYNOPSIS ✨

#include <curl/curl.h>

void lockcb(CURL *handle, curl_lock_data data, curl_lock_access access,
            void *clientp);

CURLSHcode curl_share_setopt(CURLSH *share, CURLSHOPT_LOCKFUNC, lockcb);

πŸ¦‹ DESCRIPTION πŸ¦‹

So you're setting a mutex lock callback on the share object β€” the whole point is to let that share object get used by multiple threads at the same time without everything going feral. πŸ”₯ And heads up: there's a matching CURLSHOPT_UNLOCKFUNC(3) callback that gets called when the mutex is released again, so like… they're a pair, iykyk. 🫢

The lockcb argument must be a pointer to a function that matches the prototype shown up there ⬆️ exactly. No improvising, bestie. And here's what the callback gets handed:

  • handle β€” this is the currently active easy handle in use when the share object is intended to get used. main character energy, honestly πŸ‘‘
  • data β€” tells you what kind of data libcurl wants to lock. And fr fr: make sure your callback uses a different lock for each kind of data. Not me stressing this. Okay maybe me stressing this. 😭
  • access β€” defines what access type libcurl wants β€” shared or single. pick your vibe πŸ’«
  • clientp β€” the private pointer you set with CURLSHOPT_USERDATA(3). libcurl itself does not use this pointer, it's all yours, do whatever πŸ’…

🌈 PROTOCOLS 🌈

This functionality affects all supported protocols β€” every single one, no cap πŸ’€βœ¨


πŸ“ EXAMPLE πŸ“

extern void mutex_lock(CURL *handle, curl_lock_data data,
                       curl_lock_access access, void *clientp);

int main(void)
{
  CURLSHcode sh;
  CURLSH *share = curl_share_init();
  sh = curl_share_setopt(share, CURLSHOPT_LOCKFUNC, mutex_lock);
  if(sh)
    printf("Error: %s\n", curl_share_strerror(sh));
}

look at her go 🧚 she's iconic, she's setting the lock function, she's even handling the error like a responsible queen πŸ‘‘


⭐ AVAILABILITY ⭐

Added in curl 7.10.3 β€” vintage, but make it cute πŸŽ€


πŸ’œ RETURN VALUE πŸ’œ

CURLSHE_OK (zero) means the option was set properly πŸŽ‰ β€” non-zero means an error occurred 😭. Peep libcurl-errors(3) for the full list with descriptions if you wanna know exactly what went wrong.

no cap: zero = slay, non-zero = go read the error page, bestie 🫢

πŸ”₯ SEE ALSO πŸ”₯


okay that's the whole page, you ate that πŸ’…βœ¨ this lil masterpiece is brought to you by libcurl, dated 2026-10-12 β€” mwah, go forth and lock your mutexes responsibly, bestie πŸ¦‹πŸ’–

πŸ“¦ where this page comes from