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

  • SEARCH
  • Home
  • 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 235899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:18:07+00:00 2026-05-11T20:18:07+00:00

I have functions lock(), unlock(), query1(), query2() and query3(). The query functions just run

  • 0

I have functions lock(), unlock(), query1(), query2() and query3(). The query functions just run some query on a database and can be considered r/w access to it. They do not lock. My system is multithreaded.

The functionality I want is: If lock() is called from thread p1, only queries from thread p1 will run and queries from all other threads wait for unlock. How do I do this?

I’m using pthreads from C. To do this, the thread must know that it holds the lock. But pthreads doesn’t have such a function.

Is the design wrong??

EDIT:

Function1(){
lock();
query1();
query2();
doQuery3();
unlock();
}

doQuery3(){
lock();
query3();
unlock();
}

The behaviour I want with lock() is that if the thread already is holding that lock, it should not wait on the lock. It should just run. The thing is my lock() function actually starts a transaction. I want to run a bunch of stuff in the transaction. And unlock() ends the transaction. I want to be able to chain queries. One workaround is to call query3() in Function1() instead of doQuery3(); That would mean for every function there are two versions, one with the locking and one vanilla query()

Again, these lock and unlock functions may or maynot be mutex locks. I tried implementing it with pthread mutex, but couldn’t. Because a pthread_mutex_lock on the same thread blocks! Any cool tricks??

  • 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-11T20:18:07+00:00Added an answer on May 11, 2026 at 8:18 pm

    I’m not sure I’ve understood your question/design, however …

    The functionality I want is: If lock() is called from thread p1, only queries from thread p1 will run and queries from all other threads wait for unlock. How do I do this?

    If the unit of work called by the p1 thread is like this …

    void doQuery1()
    {
      //get the lock
      //if someone else has the lock, block until it's released
      lock();
    
      //now we have the lock
      //do query
      query1();
    
      //our work is done
      //release the lock, let another thread run
      unlock();
    }
    

    … and the work called by the others threads are similar …

    void doQuery2()
    {
      lock();
      query2();
      unlock();
    }
    
    void doQuery3()
    {
      lock();
      query3();
      unlock();
    }
    

    … then if p1 has the lock, then because other threads also always call the lock function before they try to query, and because the lock function waits for unlock, then I think that’s the answer to your question: the result is that queries from other threads will wait for unlock (i.e.because all threads always call lock, and because lock waits for unlock).

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

Sidebar

Ask A Question

Stats

  • Questions 183k
  • Answers 183k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can use ispc/isunix/ismac functions to determine the platform or… May 12, 2026 at 4:36 pm
  • Editorial Team
    Editorial Team added an answer I'd place the query in a view. When you add… May 12, 2026 at 4:36 pm
  • Editorial Team
    Editorial Team added an answer That depends. I usually try not to group them at… May 12, 2026 at 4:36 pm

Related Questions

I have a MDB (Message driven bean) that receives messages with String which represent
In my C++ program I have a class CEvent with trigger and wait member
I am trying to port a project (from linux) that uses Semaphores to Mac
I have these two methods for thread-exclusive access to a CMyBuffer object: Header: class

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.