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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:44:33+00:00 2026-06-11T11:44:33+00:00

In my program, I use a single thread pool to dispatch all my task,

  • 0

In my program, I use a single thread pool to dispatch all my task, like timer task, non-blocking socket I/O, etc. A task is actually an callback function, which will be executed when specific event received.

The architecture is :

  1. The main thread calls epoll() to harvest the I/O event, then dispatch the I/O callback to the thread pool.

  2. The main thread also handle timer timeout, and dispatch the timeout callback to the thread pool

  3. In an I/O callback, one timer task may be cancelled, depending on I/O processing result.

  4. During one I/O callback is running, the coresponding socket is not monitored for further identical event.

  5. Durning one timer callback is running, that timer task will temporarily removed from the timer task queue.

Here is the problem:

  1. During thread A in the pool is running a timer callback T.

  2. Thread B in the pool may be running another callback(registered for an socket I/O read event), after processing request received, thread B decide to delete the timer task T, but that timer task T is being executed by thread A right now.

I can add an lock for the timer task, but where should I place the lock? I can’t place the lock object in the timer task structure, because when I decide to free the task object, I must have already acquired the lock, free and held lock, may lead to undefined behaviours:

pthread_mutex_lock(T->mutex);
 free(T);
 /*without a pthread_mutex_unlock(T->mutex);*/

And what happened if another thread is blocked on :

pthread_mutex_lock(T->mutex);

Without these problem being addressed, I can’t continue my work.Please HELP me out!

Should I use separate thread pool for task of different types in my single process? Or just use single thread?

Any suggestion is appreciated!

  • 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-11T11:44:34+00:00Added an answer on June 11, 2026 at 11:44 am

    You can use a global table of timers protected by its own mutex. The table does not actually need to be global but can belong to some collection, such as whatever owns all the things you are doing I/O on.

    Then use this logic:

    To create a timer:

    1. Lock the global table.

    2. Add the timer to the global table in state “pending”.

    3. Unlock the global table.

    4. Schedule the timer with the thread pool.

    To fire a timer:

    1. Lock the global table.

    2. Check the timer’s state. If it’s not “pending”, delete the timer, unlock the table, and stop.

    3. Change the timer’s state to “firing”.

    4. Unlock the global table.

    5. Perform the timer operation.

    6. Lock the global table.

    7. Remove the timer from the table.

    8. Unlock the global table.

    To cancel a timer:

    1. Lock the global table.

    2. Find the timer. If it’s state is “pending”, change it to “cancelled”. Do not delete it.

    3. Unlock the global table.

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

Sidebar

Related Questions

Is there any apt-get -like program for use with Cygwin? I've already tried cyg-apt
I am changing a single thread program into multi thread using boost:thread library. The
I am trying to create a java socket program with multithread concept. Every single
My program use UdpClient to try to receive 27 responses from 27 hosts. The
I'm making a program use JNI to call some native directives. My code is:
I have a flight reservation program use mssql ,For reserving flights i want to
This program I use has it's own variables to set when you run it,
I wrote a little program that use activemq embedded broker. Program run on one
I wirte a simple program that use unix sockets to download a html file
I created a hello world java program and use lauch4j to convert executable jar

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.