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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:24:57+00:00 2026-05-27T04:24:57+00:00

OS is Linux, working with pthreads I have two worker threads that run forever,

  • 0

OS is Linux, working with pthreads

I have two worker threads that run forever, until a stop variable takes value true, and the threads terminate gracefully. Instead of doing busy waiting both threads call pthread_cond_wait until a signal notifies for a new task. The system works well.

It is requested to create an “info” thread that will print some debugging information. The info thread will try to read and print information every 30 seconds.Part of this info, I would like to be the STATE of each worker thread. Is it possible to find if a thread is blocked in “pthread_cond_wait”? If the thread waits is pthread_cond_wait then STATE==waiting else the STATE==running.

 while ( (sharedvaluffer == 0) && (doneflag == 0) ) {
            pthread_cond_wait (&taks_added, &buffer);
        }    

Of course we can do that we more code. We can add to the above snippet a global variable that
marks that thread as locked. The code can be done

while ( (sharedvaluffer == 0) && (doneflag == 0) ) {
                lock;
                i_am_waiting = truel
                unlock
                pthread_cond_wait (&taks_added, &buffer);
 } 

The question is, if there is an easier more scalable way. The stack of a waiting thread is

Thread 6 (Thread 0x40800940 (LWP 20732)):
#0  0x00002ba4567a9326 in pthread_cond_wait@@GLIBC_2.3.2 ()
#1  0x00000000007ce2ed in worker(void*) ()
#2  0x00002ba4567a5193 in start_thread () from /lib64/libpthread.so.0
#3  0x00002ba458a82f0d in clone () from /lib64/libc.so.6
  • 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-27T04:24:57+00:00Added an answer on May 27, 2026 at 4:24 am

    You could use the return of pthread_self() as identifier for the mutex ownership. Store and compare. Since a mutex can be acquired by a single thread at once, you will know which thread is running (not waiting), and which are not.

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

Sidebar

Related Questions

I'm working in linux. I have two programs that run for infinite time (
I am working with linux kernel 2.6.38 and have a question about two fields
I am working in Linux and i have a little bit confusion that whether
I have a (from what I can tell) perfectly working Linux setup (Ubuntu 8.04)
Actually I have a file . I am working in linux environment. I need
Iam working on Ubuntu.(Linux) I want to redirect from the page one.php to two.php,
I'm working on a Linux based server system in which there are two network
I'm working on an embedded Linux project that interfaces an ARM9 to a hardware
I am working on linux. I have a HTTP client which requests some data
Working in Linux and using C++. I have a program which initiates a thread.

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.