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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:36:52+00:00 2026-06-10T08:36:52+00:00

In C, I have several threads producing long values, and one thread consuming them.

  • 0

In C, I have several threads producing long values, and one thread consuming them. Therefore I need a buffer of a fixed size implemented in a similar fashion to i.e. the Wikipedia implementation, and methods that access it in a thread-safe manner.

On a general level, the following should hold:

  • When adding to a full buffer, the thread should be blocked (no overwriting old values).
  • The consumer thread should be blocked until the buffer is full – it’s job has a high constant cost, and should do as much work as possible. (Does this call for a double-buffered solution?)

I would like to use a tried implementation, preferably from a library. Any ideas?


Motivation & explanation:

I am writing JNI code dealing with deleting global references kept as tags in heap objects.

When a ObjectFree JVMTI event occurs, I get a long tag representing a global reference I need to free using DeleteGlobalRef. For this, I need a JNIEnv reference – and getting it is really costly, so I want to buffer the requests and remove as many as possible at once.

There might be many threads receiving the ObjectFree event, and there will be one thread (mine) doing the reference deletion.

  • 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-10T08:36:54+00:00Added an answer on June 10, 2026 at 8:36 am

    You can use a single buffer, with a mutex when accessed. You’ll need to keep track of how many elements are used. For “signaling”, you can use condition variables. One that is triggered by the producer threads whenever they place data in the queue; this releases the consumer thread to process the queue until empty. Another that is triggered by the consumer thread when it has emptied the queue; this signals any blocked producer threads to fill the queue. For the consumer, I recommend locking the queue and taking out as much as possible before releasing the lock (to avoid too many locks), especially since the dequeue operation is simple and fast.

    Update
    A few useful links:
    * Wikipedia explanation
    * POSIX Threads
    * MSDN

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

Sidebar

Related Questions

I have several AsyncTask worker threads, and during runtime one of them shows up
I have several threads. And I'd like to create sub-threads in one of them.
I have several questions regarding Python threads. Is a Python thread a Python or
I have several threads which act as backup for the main one spending most
I have several threads that write to the same int. Each thread increment the
I have several threads which need to write to two different text files. So
I have a long-running service with several threads calling the following method hundreds of
I have a parallel bash script with several threads. Each thread checks if record
I have several Threads running in a ThreadPool in my WPF App. I need
I have several threads running and I know currently one or many threads are

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.