Skip to content

Instantly share code, notes, and snippets.

@ngo
Created July 17, 2019 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ngo/07bedacec236b63e4494ba33994f3368 to your computer and use it in GitHub Desktop.
Save ngo/07bedacec236b63e4494ba33994f3368 to your computer and use it in GitHub Desktop.
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#define NUM_THREADS 5
void *PrintHello(void *threadid)
{
long tid;
tid = (long)threadid;
if (tid == 0){
sleep(100);
}
printf("Hello World! It's me, thread #%ld!\n", tid);
pthread_exit(NULL);
}
int main (int argc, char *argv[])
{
pthread_t threads[NUM_THREADS];
int rc;
long t;
for(t=0; t<NUM_THREADS; t++){
printf("In main: creating thread %ld\n", t);
rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
if (rc){
printf("ERROR; return code from pthread_create() is %d\n", rc);
return -1;
}
}
/* Last thing that main() should do */
pthread_exit(NULL);
}
% gdb ./a.out
GNU gdb (GDB) 8.2.1
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-pc-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./a.out...done.
(gdb) b 8
Breakpoint 1 at 0x1185: file 1.c, line 9.
(gdb) r
Starting program: ./a.out
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/usr/lib/libthread_db.so.1".
In main: creating thread 0
[New Thread 0x7fa6cb71c700 (LWP 4677)]
In main: creating thread 1
[New Thread 0x7fa6caf1b700 (LWP 4678)]
In main: creating thread 2
[New Thread 0x7fa6ca71a700 (LWP 4679)]
[Switching to Thread 0x7fa6cb71c700 (LWP 4677)]
Thread 2 "a.out" hit Breakpoint 1, PrintHello (threadid=0x0) at 1.c:9
9 tid = (long)threadid;
(gdb) del br
Delete all breakpoints? (y or n) y
(gdb) p tid
$1 = 0
(gdb) thread
[Current thread is 2 (Thread 0x7fa6cb71c700 (LWP 4677))]
(gdb) s
In main: creating thread 3
Hello World! It's me, thread #2!
[New Thread 0x7fa6c9f19700 (LWP 5539)]
Hello World! It's me, thread #1!
Hello World! It's me, thread #3!
In main: creating thread 4
[New Thread 0x7fa6c9718700 (LWP 5540)]
Hello World! It's me, thread #4!
10 if (tid == 0){
(gdb) thread
[Current thread is 2 (Thread 0x7fa6cb71c700 (LWP 4677))]
(gdb) s
11 sleep(100);
(gdb) s
[Thread 0x7fa6caf1b700 (LWP 4678) exited]
[Thread 0x7fa6c9f19700 (LWP 5539) exited]
[Thread 0x7fa6ca71a700 (LWP 4679) exited]
[Thread 0x7fa6c9718700 (LWP 5540) exited]
[Thread 0x7fa6cb71d740 (LWP 4662) exited]
13 printf("Hello World! It's me, thread #%ld!\n", tid);
(gdb) thread
[Current thread is 2 (Thread 0x7fa6cb71c700 (LWP 4677))]
(gdb) c
Continuing.
Hello World! It's me, thread #0!
[Inferior 1 (process 4662) exited normally]
(gdb)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment