Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6126031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:19:13+00:00 2026-05-23T16:19:13+00:00

I have to use a library that makes database calls which are not thread-safe.

  • 0

I have to use a library that makes database calls which are not thread-safe. Also I occasionally have to load larger amounts of data in a background thread.
It is hard to say which library functions actually access the DB, so I think the safest approach for me is to protect every library call with a lock.

Let’s say I have a library object:

dbLib::SomeObject someObject;

Right now I can do something like this:

dbLib::ErrorCode errorCode = 0;
std::list<dbLib::Item> items;
{
    DbLock dbLock;
    errorCode = someObject.someFunction(&items);
} // dbLock goes out of scope

I would like to simplify that to something like this (or even simpler):

dbLib::ErrorCode errorCode =
    protectedCall(someObject, &dbLib::SomeObject::someFunction(&items));

The main advantage of this would be that I won’t have to duplicate the interface of dbLib::SomeObject in order to protect each call with a lock.

I’m pretty sure that this is a common pattern/idiom but I don’t know its name or what keywords to search for. (Looking at http://www.vincehuston.org/dp/gof_intents.html I think, it’s more an idiom than a pattern).

Where do I have to look for more information?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T16:19:13+00:00Added an answer on May 23, 2026 at 4:19 pm

    You could make protectedCall a template function that takes a functor without arguments (meaning you’d bind the arguments at the call-site), and then creates a scoped lock, calls the functor, and returns its value. For example something like:

    template <typename Ret>
    Ret protectedCall(boost::function<Ret ()> func)
    {
        DbLock lock;
        return func();
    }
    

    You’d then call it like this:

    dbLib::ErrorCode errorCode = protectedCall(boost::bind(&dbLib::SomeObject::someFunction, &items));
    

    EDIT. In case you’re using C++0x, you can use std::function and std::bind instead of the boost equivalents.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a database library in C# that we can use like this: DatabaseConnection
Does anyone use have a good regex library that they like to use? Most
Whenever I have a library that use across different websites/ applications I've always just
I have a C library that I'm planning to use in an iPhone application.
We have a Library Project that we use for all our central reused code
I have a library written in C that I would like to use in
I have a c-library which I use in gcc. The library has the extension
It should be simple, but it's not. I have a library that is hoping
I have a C++ library and a C++ application trying to use functions and
I have developed a simple library in Ruby and need to use this in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.