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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:16:57+00:00 2026-05-23T19:16:57+00:00

I want to debug a multi-threaded program by controlling which threads execute when. I

  • 0

I want to debug a multi-threaded program by controlling which threads execute when. I am using C++ and gdb. I have two threads besides the main thread (for the example program) and I want to debug one thread while keeping the other stopped.

Here is the example program I wrote:

#include <iostream>
#include <pthread.h>
#include <stdlib.h>

#define NUM_THREADS 2

using namespace std;

void * run (void *) {
  for (int i = 0; i < 3; ++i) {
    sleep(1);
    cout << i << " " << pthread_self() << endl;
  }
  pthread_exit(NULL);
}

int main (int argc, char** argv) {
  cout << "Start..." << endl;
  int rc;

  pthread_t threads[NUM_THREADS];
  for (int i = 0; i < NUM_THREADS; ++i) {
    rc = pthread_create(&threads[i], NULL, run, NULL);
    if (rc) {
      cout << "pthread_create returned error: " << rc << endl;
      exit(-1);
    }
  }
  pthread_exit(NULL);

}

I run gdb and set the breakpoint at line with sleep(1). Then I run the program. I get three threads (thread 2 and 3 are pthreads) and the program is at thread 2 (waiting at sleep(1)). Now, I want to keep thread 3 wherever it is, and keep stepping through thread 2 (by executing c in gdb).

What I have tried is set scheduler-locking on, but it does not seem to work as I expected. I am in thread 2, I set scheduler-locking on, continue a couple times (so far so good, I am still in thread 2), switch to thread 3, set scheduler-locking on, continue, and for some reason, I am back in thread 2… when I should not be (according to my understanding). Is there something I am missing?

  • 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-23T19:16:57+00:00Added an answer on May 23, 2026 at 7:16 pm

    It looks like scheduler-locking is only useful when you single-step or next. Once you continue your current thread, they all run, and the next thread to hit a break point will grab the prompts. At least, that’s my interpretation of the manual:

    http://ftp.gnu.org/old-gnu/Manuals/gdb-5.1.1/html_node/gdb_39.html

    So, once you are in thread 3, the other threads are stopped, and as long as you step/next, they will not run. But once you continue, they all run, and the next thread (2 in your example) that hits the break point in the sleep(1) will grab the prompt.

    Maybe let all the threads hit the sleep, but then only continue one of them at a time.

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

Sidebar

Related Questions

I have a program and want to debug it in gdb. Will I see
I'm working on a multithreaded application, and I want to debug it using GDB.
I want to debug TCP/IP interactions for a program I'm enhancing. I don't have
I have a multi-threaded C++ program that deadlocks in some rare cases. The problem
I'm using VS2010 Beta 2 to debug some multi-threaded code. Love parallel stacks, etc.
Ok, I got this multi-threaded C++ program that I try to debug in Visual
I want to debug an application in Visual Studio but I ONLY have the
I want to use remote debugging. The program that I want to debug runs
When using Google Chrome, I want to debug some JavaScript code. How can I
I basically want to debug a cgi programm with gdb by emulating the environment

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.