Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ OneLocBuild
vscode-translations-import

.vscode/settings.json

# Ignore Fib sample build artifacts
Code Samples/Fib/fib.out
Code Samples/Fib/fib.out.dSYM/
2 changes: 1 addition & 1 deletion Code Samples/Fib/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
"configurations": [
{
"name": "(gdb) Launch",
"preLaunchTask": "build",
"type": "cppdbg",
"request": "launch",
"args": [],
"stopAtEntry": true,
"cwd": "${workspaceRoot}",
"environment": [],
"externalConsole": true,
"preLaunchTask": "build",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
Expand Down
47 changes: 17 additions & 30 deletions Code Samples/Fib/.vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,32 @@
{
"label": "build",
"type": "shell",
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "shared"
"command": "g++",
"args": [
"-g",
"*.cpp",
"-std=c++11",
"-o",
"${workspaceFolder}/fib.out"
],
"options": {
"cwd": "${workspaceFolder}"
},
"windows": {
"command": "${workspaceRoot}/build.cmd",
"args": [
"<Path/To/MinGW/Cygwin/Bin/Folder>", // Path to the bin folder containing g++ to compile
"fib.exe" // Output executable name
]
},
"linux": {
"command": "g++",
"args": [
"-g",
"*.cpp",
"-lpthread",
"--std=c++11",
"-std=c++11",
"-o",
"fib.out"
"${workspaceFolder}/fib.exe"
]
},
"osx": {
"command": "g++",
"args": [
"-g",
"*.cpp",
"-lpthread",
"--std=c++11",
"-o",
"fib.out"
]
}
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": ["$gcc"]
}
]
}
31 changes: 30 additions & 1 deletion Code Samples/Fib/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,32 @@
# Fib

This code sample is to show debugging. Update `launch.json` and `tasks.json` in the `.vscode` folder to use your setup to build and debug.
This code sample demonstrates multithreaded debugging with C++11. It spawns 10 named threads, each computing Fibonacci numbers, and is designed to show how VS Code with the C/C++ extension can display named threads in the Threads and Call Stack views.

## Building

Use one of the commands below to build the sample.

```bash
# Linux / macOS
g++ -g *.cpp -std=c++11 -o fib.out

# Windows (MinGW)
g++ -g *.cpp -std=c++11 -o fib.exe
```

On Windows you can also run the included `build.cmd` (it expects the path to a MinGW `bin` folder and an output name):

```powershell
.\build.cmd <Path\To\MinGW\Bin> fib.exe
```

## Debugging

Update `launch.json` in the `.vscode` folder to point to your debugger if needed (e.g. set `miDebuggerPath` on Windows).

Press **F5** to build and launch the debugger. The build task compiles the sample automatically before starting the debug session.

## Notes

- Thread naming uses `pthread_setname_np` on macOS/Linux so threads appear by name in the debugger's Threads / Call Stack view. On Windows the names are visible via the thread IDs.
- No `-lpthread` flag is needed: the sample uses `std::thread` for threading and only calls the native thread-naming API for debugger visibility.
5 changes: 3 additions & 2 deletions Code Samples/Fib/build.cmd
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
SET PATH=%PATH%;%1
g++ -g *.cpp -lpthread --std=c++11 -O0 -o %2
@echo off
Comment thread
Subham-KRLX marked this conversation as resolved.
SET PATH=%PATH%;%1
g++ -g *.cpp --std=c++11 -O0 -o %2
23 changes: 9 additions & 14 deletions Code Samples/Fib/main.cpp
Original file line number Diff line number Diff line change
@@ -1,30 +1,25 @@
#include <iostream>
#include <unistd.h>
#include <sys/types.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <thread>
#include <atomic>
#include <string>
#include <cstring>

#include "thread.h"

#define THREAD_COUNT 10

static char block[] = "--block";
int test = 0;
std::atomic<int> test(0);

int main(int argc, char **argv)
{
srand(time(NULL));

static char pidText[] = "PID: ";
std::string helpText = "Attach a debugger and execute 'set foo=0' to continue";
char helloText[] = "Hello World!";

std::cout << helloText << std::endl;

pthread_t threads[THREAD_COUNT];
std::thread threads[THREAD_COUNT];

if (argc == 2 && !strcmp(block, argv[1]))
{
Expand All @@ -43,16 +38,16 @@ int main(int argc, char **argv)
for (int i = 0; i < THREAD_COUNT; i++)
{
std::cout << "Test " << i << std::endl;
pthread_create(&threads[i], NULL, &thread_proc, NULL);
threads[i] = std::thread(thread_proc, i);
}

for (int i = 0; i < THREAD_COUNT; i++)
{
pthread_join(threads[i], NULL);
threads[i].join();
test++;
}

std::cout << "All threads exited!" << std::endl;

return 1;
return 0;
}
59 changes: 32 additions & 27 deletions Code Samples/Fib/thread.cpp
Original file line number Diff line number Diff line change
@@ -1,47 +1,52 @@
#include <iostream>
#include <unistd.h>
#include <sys/types.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <thread>
#include <chrono>
#include <cstdio>

// Platform-specific headers for thread naming only.
// Thread naming is preserved so named threads appear in the debugger's
// Threads / Call Stack view — a key feature of this debugging sample.
#if defined(__APPLE__)
# include <pthread.h>
#elif defined(__linux__)
# include <sys/prctl.h>
#endif

#include "thread.h"

static int g_tid = 0;
// Set the name of the calling thread for debugger visibility.
// std::thread has no portable name API, so we use OS-specific calls.
static void set_thread_name(const char* name)
{
#if defined(__APPLE__)
pthread_setname_np(name); // macOS: sets the current thread's name
#elif defined(__linux__)
prctl(PR_SET_NAME, name, 0, 0, 0); // Linux: sets the current thread's name
#else
(void)name; // Silently ignored on unsupported platforms (e.g. Windows/MinGW)
#endif
}

static int fib(int n){
static int fib(int n)
{
switch (n) {
case 0: return 1;
case 1: return 1;
default: return (fib(n-2) + fib(n-1));
}
}

void * thread_proc(void* ctx)
void thread_proc(int tid)
{
int tid = g_tid++;

char thread_name[16];
sprintf(thread_name, "Thread %d", tid);
#ifdef __APPLE__
pthread_setname_np(thread_name);
#else
pthread_setname_np(pthread_self(), thread_name);
#endif
snprintf(thread_name, sizeof(thread_name), "Thread %d", tid);
set_thread_name(thread_name);

// Random delay, 0 - 0.5 sec
timespec ts;
ts.tv_sec = 0;
ts.tv_nsec = 500000000 + ((float)rand() / (float)RAND_MAX) * 500000000;
nanosleep(&ts, NULL);
std::chrono::milliseconds delay(500);

volatile int i = 0;
while (i <= 30) {
for (int i = 0; i <= 30; i++) {
std::cout << "Thread " << tid << ": fib(" << i << ") = " << fib(i) << std::endl;
i++;
nanosleep(&ts, NULL);
std::this_thread::sleep_for(delay);
}

std::cout << thread_name << " exited!" << std::endl;
Expand Down
2 changes: 1 addition & 1 deletion Code Samples/Fib/thread.h
Original file line number Diff line number Diff line change
@@ -1 +1 @@
void * thread_proc(void* ctx);
void thread_proc(int tid);