Thursday, March 25, 2010

Pooled resource pattern

Time to implement a pattern by means of a c++ template. I've used nearly identical code twice in the same project.

One case is a pool of easy curl objects:

CURL *WebResponse::getFromPool()

{

// all actions with the pool must be locked

boost::mutex::scoped_lock lock(poolMutex);

if (curlPool.empty())

{

CURL *pCurl = curl_easy_init();

if (!pCurl)

throw ("allocation failure CURL");

return pCurl;

}

CURL *p = curlPool.front();

curlPool.pop_front();

return p;

}

void WebResponse::putBackInPool(CURL *pCurl)

{

// all actions with the pool must be locked

boost::mutex::scoped_lock lock(poolMutex);

curlPool.push_back(pCurl);

}



The other a pool of libmemcached objects. This should be an interesting use of templates. I'll post what I come up with soon.

No comments:

chris' shared items

Twitter Updates

Official blog of Chris Lee Runyan

Fastest C++ in the west.