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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:05:24+00:00 2026-06-10T05:05:24+00:00

If we need to implement a queue (being done by NSMutableArray ), and we

  • 0

If we need to implement a queue (being done by NSMutableArray), and we have Producer objects that add data to the queue. There can be 1 producer or multiple producers. And if the queue is full (at size of 100, for example), the thread should be “blocked” (waiting, until the queue is actually smaller size than 100 items). (the same goes for taking away data from the queue: when it is empty, the getting of data is also blocked).

How can this “blocking” be implemented? Right now I use a Mutex

@property (strong, atomic) NSLock *lock;

so that the count of the NSMutableArray is obtained and adding of item is done together (because the count can be 99, but when the item is added, the count can already be different).

But for the blocking effect, I actually check the count, and if 100, release the lock and sleep for 0.1 second, and then get the lock again and get the count, and repeat as above.

Is this a good way to implement the “blocking” behavior? There seems to be another way to get a canAddNowLock, which is simply blocking by the NSLock mechanism, and when a queue item is removed, then release this canAddNowLock, but since there can be multiple data producers and data consumers, what if several producers are unblocked, or what if multiple consumers unlock canAddNowLock in a row and only 1 producer is unblocked (when in fact several producers should be unblocked)? It just seem a more complicated design that way.

  • 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-10T05:05:26+00:00Added an answer on June 10, 2026 at 5:05 am

    use NSCondition *condition
    thread1:

    [condition lock];
    //check the queue is not full
    [condition wait];//if already full, wait
    //check again
    //put something into queue
    [condition unlock];
    

    thread2:

    [condition lock];
    //check the queue is not empty
    //get something from queue
    [condition signal];
    [condition unlock];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to implement a producer/consumer bounded queue, multiple consumers against a single producer.
I have a question regarding LinkedList that I'm using to implement a queue. How
I need to implement a basic FIFO queue of objects. Whats the best built-in
i need to implement this scenario Brief about that is have two java based
I'm writing a win32 library and I need to implement a producer-consumer queue using
I have a TI DSP (TMS320F28235 if anyone cares) that I need to implement
I need to implement a data structure that supports insertion deletion and search in
I need to implement a FIFO queue for messages on a game server so
I need to implement an efficient excel-like app. I'm looking for a data structure
I need to implement the following: There is a table A which is supposed

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.