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 7882463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:17:28+00:00 2026-06-03T04:17:28+00:00

I have a C++ program in which I have multiple threads writing into my

  • 0

I have a C++ program in which I have multiple threads writing into my SQLite tables(WAL mode is enabled) of a single database. Each thread creates a SQLite handle, does a sqlite3_open(), writes into the tables(there writes are with in a transaction) and then does a sqlite3_close(), and then SQLite handles are deleted. Then the thread dies.

Even after all the threads die, SQLite handles are still open. Why are not SQLite handles getting closed? What am I missing here?

My C++ program is running on a CentOS 5.5.

[Edit]
Here is my sample program using pthread

void threadFunction(void* pArg) {
    sqlite3 *handle;
    sqlite3_open("a.cm", &handle);    
    printf("Worker thread - Opened \n");
    sleep(10);    
    int r = sqlite3_close(handle);
    printf("Ret: %d\n", r);
    printf("Worker thread - Closed \n");
}

int main() {
    int i(0);
    pthread_t thread1, thread2;
    printf("Creating a worker thread\n");
    printf("SQLite libversion: %s\n", sqlite3_libversion());
    sqlite3 *handle;
    sqlite3_open("a.cm", &handle);    
    sqlite3_exec(handle, "pragma journal_mode = WAL", NULL, NULL, NULL);    
    printf("Main thread - Opened \n");

    pthread_create(&thread1, NULL, threadFunction, NULL);
    pthread_create(&thread2, NULL, threadFunction, NULL);

    pthread_join( thread1, NULL);
    pthread_join( thread2, NULL);
    sleep(200);
    sqlite3_close(handle);
    printf("Main thread - close \n");
    return 0;
}
  • 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-06-03T04:17:29+00:00Added an answer on June 3, 2026 at 4:17 am

    Contacted the SQLite team: Here is their reply-

    When you close one connection (In WAL mode),
    SQLite checks if any other connection in the process is holding
    a POSIX lock on the database file being closed. If so, it defers
    closing the file-handle until the other connection drops its
    POSIX lock (the file descriptor will be reused if yet another
    connection to the same file is opened, but otherwise it just
    hangs around waiting until it can be safely closed).

    The conclusion is:
    So until the connection is closed from the main() function
    the handles opened from the other threads will not be closed!

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

Sidebar

Related Questions

I have a C program which has multiple worker threads. There is a main
I have a program in which the user adds multiple objects to a scene.
I am writing a OpenCL program in which multiple kernels are being called on
I have a program that scales badly to multiple threads, although – theoretically –
I currently program a program which uses multiple threads. To be able to share
I have a program which displays one ellipses (Ellipse2D) . Should I directly add
I have a program which needs to behave slightly differently on Tiger than on
I have a program which deliberately performs a divide by zero (and stores the
I have a program which I would like to run every X min the
I have a program which runs an external, command line utility and reads the

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.