curl-library
Re: how to write the lock funcition in share interface
Date: Tue, 24 Jan 2006 00:14:38 +0100 (CET)
On Mon, 23 Jan 2006, Yi Li wrote:
> In order to share DNS cache among multi_threads, I will need to achieve the
> following steps.
>
> 1: call curl_share_init() to create a share handler
> 2: call curl_share_setopt( sharehandler, CURLSHOPT_LOCKFUNC, lock);
> 3: call curl_share_setopt(sharehandler,CURLSHOPT_UNLOCKFUNC,unlock);
> 4: call curl_share_setopt(share,CURLSHOPT_SHARE,CURL_LOCK_DATA_DNS);
> 5: create an easy handler and call
> curl_easy_setopt(easyHandler,CURLOPT_SHARE,shareHandler);
Yes, that seems correct.
> in step 4, I telled curl to share DNS cache. is it right?
Yes.
> Do it means that curl liberary will provide some kind of mutex mechanisem to
> share the DNS or should I write my "own" mutex to tell curl lib how to share
> ? If I need to write my own mutex, where I write it? in Lock and UnLock?
Well, given that you pass this share object to more than one easy handle with
curl_easy_setopt() + CURLOPT_SHARE, then yes.
Then libcurl will call the lock and unlock function callbacks you set and it
will assume that you do the mutex magic for them: When libcurl locks the DNS
for single access, you must not let another handle get the lock until the
first handle unlocks the DNS again.
> I'm still not clear about what I should write in lock and unlock function.
Call your thread system's mutex functions.
> in void lock(CURL *handle, curl_lock_data data, curl_lock_access access,void
> *useptr ), I understand "data" can only be
> CURL_LOCK_DATA_SHARE: to share user own data
> CURL_LOCK_DATA_DNS: to share dns
> CURL_LOCK_DATA_COOKIE: to share cookies.
Correct. That tells your function what particular data libcurl wants to lock
access to. You can then have a single mutex for all kinds of data, or opt to
have one mutext for each kind. Your call!
> I understand if I want to share my own data among multi_threads, the
> "useptr" should point to my own data.
That pointer is the one you set with CURLSHOPT_USERDATA. You can set that to
whatever you want, and use it for whatever purpose you want.
> But how about DNS and cookies. what pointer I should passed?
For libcurl it doesn't matter.
-- Commercial curl and libcurl Technical Support: http://haxx.se/curl.htmlReceived on 2006-01-24