From 8aeeb692a7bc86d62dc51598699de4d4f59b0c1c Mon Sep 17 00:00:00 2001 From: Subham Date: Sat, 2 Aug 2025 07:37:09 +0530 Subject: [PATCH 1/3] Update build.cmd --- Code Samples/Fib/build.cmd | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Code Samples/Fib/build.cmd b/Code Samples/Fib/build.cmd index 821c62747..8b78dafa9 100644 --- a/Code Samples/Fib/build.cmd +++ b/Code Samples/Fib/build.cmd @@ -1,2 +1,3 @@ +@@ -1,2 +0,0 @@ SET PATH=%PATH%;%1 -g++ -g *.cpp -lpthread --std=c++11 -O0 -o %2 \ No newline at end of file +g++ -g *.cpp -lpthread --std=c++11 -O0 -o %2 From 6b7cebe174020a48d66ae89823c304ce8358b3de Mon Sep 17 00:00:00 2001 From: Subham Sangwan Date: Sun, 9 Nov 2025 10:30:31 +0530 Subject: [PATCH 2/3] samples(Fib): remove build task, update README, remove preLaunchTask from launch.json --- Code Samples/Fib/.vscode/launch.json | 1 - Code Samples/Fib/.vscode/tasks.json | 72 +++++++++++----------------- Code Samples/Fib/README.md | 48 +++++++++++++++++++ 3 files changed, 76 insertions(+), 45 deletions(-) diff --git a/Code Samples/Fib/.vscode/launch.json b/Code Samples/Fib/.vscode/launch.json index 301f8c5f2..b1349d1a4 100644 --- a/Code Samples/Fib/.vscode/launch.json +++ b/Code Samples/Fib/.vscode/launch.json @@ -3,7 +3,6 @@ "configurations": [ { "name": "(gdb) Launch", - "preLaunchTask": "build", "type": "cppdbg", "request": "launch", "args": [], diff --git a/Code Samples/Fib/.vscode/tasks.json b/Code Samples/Fib/.vscode/tasks.json index 104a10aad..49c3004f1 100644 --- a/Code Samples/Fib/.vscode/tasks.json +++ b/Code Samples/Fib/.vscode/tasks.json @@ -1,50 +1,34 @@ +{ + "version": "2.0.0", + "tasks": [] +} +```jsonc +{ + "version": "2.0.0", + "tasks": [] +} +``` +{ + // See https://go.microsoft.com/fwlink/?LinkId=733558 + // for the documentation about the tasks.json format + "version": "2.0.0", + // The explicit "build" task was removed because the sample now uses a direct g++ command + // (the Makefile was removed). If you prefer a build task, add one that runs the + // appropriate g++ command for your platform or call `build.cmd` on Windows. + "tasks": [] +} { // See https://go.microsoft.com/fwlink/?LinkId=733558 // for the documentation about the tasks.json format "version": "2.0.0", - "tasks": [ - { - "label": "build", - "type": "shell", - "group": { - "kind": "build", - "isDefault": true - }, - "presentation": { + { + // See https://go.microsoft.com/fwlink/?LinkId=733558 + // for the documentation about the tasks.json format + "version": "2.0.0", + // The explicit "build" task was removed because the sample now uses a direct g++ command + // (the Makefile was removed). If you prefer a build task, add one that runs the + // appropriate g++ command for your platform or call `build.cmd` on Windows. + "tasks": [] + } "echo": true, - "reveal": "always", - "focus": false, - "panel": "shared" - }, - "windows": { - "command": "${workspaceRoot}/build.cmd", - "args": [ - "", // Path to the bin folder containing g++ to compile - "fib.exe" // Output executable name - ] - }, - "linux": { - "command": "g++", - "args": [ - "-g", - "*.cpp", - "-lpthread", - "--std=c++11", - "-o", - "fib.out" - ] - }, - "osx": { - "command": "g++", - "args": [ - "-g", - "*.cpp", - "-lpthread", - "--std=c++11", - "-o", - "fib.out" - ] - } - } - ] } diff --git a/Code Samples/Fib/README.md b/Code Samples/Fib/README.md index 6402c911b..a9f9eb045 100644 --- a/Code Samples/Fib/README.md +++ b/Code Samples/Fib/README.md @@ -1,3 +1,51 @@ # 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. + +## Building + +Use one of the commands below to build the sample. The Makefile was removed and the sample is built with g++ directly. + +```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` if you prefer (it expects the path to a MinGW/Cygwin `bin` folder and an output name): + +```powershell +.\build.cmd fib.exe +``` + +After building, use the `launch.json` in this folder (or your own) to debug the produced binary. +```markdown +# 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. + +## Building + +Use one of the commands below to build the sample. The Makefile was removed and the sample is built with g++ directly. + +```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` if you prefer (it expects the path to a MinGW/Cygwin `bin` folder and an output name): + +```powershell +.\build.cmd fib.exe +``` + +After building, use the `launch.json` in this folder (or your own) to debug the produced binary. +``` +# 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. \ No newline at end of file From c4831a0ef0109beaf2843308ce4f999eb43f1f8c Mon Sep 17 00:00:00 2001 From: Subham Sangwan Date: Wed, 14 Oct 2026 14:41:20 +0530 Subject: [PATCH 3/3] samples(Fib): complete std::thread migration and fix all review issues - main.cpp: replace pthread_t/pthread_create/pthread_join with std::thread use std::atomic for test counter; remove srand and unneeded C headers - thread.cpp: fully remove pthread threading primitives; replace nanosleep+rand with std::this_thread::sleep_for; pass tid directly instead of void* ctx preserve thread naming via OS shim (pthread_setname_np/prctl) so named threads still appear in the debugger Call Stack view - thread.h: update signature to void thread_proc(int tid) - build.cmd: no -lpthread needed now that code is genuinely pthread-free - .vscode/tasks.json: replace malformed multi-object file with single valid JSONC build task (g++ -std=c++11) restoring F5 build-and-debug flow - .vscode/launch.json: restore preLaunchTask: build so F5 auto-builds - README.md: remove duplicated content and embedded markdown fences - .gitignore: restore .vscode/settings.json entry; add fib.out ignore rules --- .gitignore | 4 ++ Code Samples/Fib/.vscode/launch.json | 1 + Code Samples/Fib/.vscode/tasks.json | 61 +++++++++++++++------------- Code Samples/Fib/README.md | 37 ++++------------- Code Samples/Fib/build.cmd | 6 +-- Code Samples/Fib/main.cpp | 23 ++++------- Code Samples/Fib/thread.cpp | 59 +++++++++++++++------------ Code Samples/Fib/thread.h | 2 +- 8 files changed, 91 insertions(+), 102 deletions(-) diff --git a/.gitignore b/.gitignore index 89defab0e..4c969fc85 100644 --- a/.gitignore +++ b/.gitignore @@ -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/ diff --git a/Code Samples/Fib/.vscode/launch.json b/Code Samples/Fib/.vscode/launch.json index b1349d1a4..1a310c4ac 100644 --- a/Code Samples/Fib/.vscode/launch.json +++ b/Code Samples/Fib/.vscode/launch.json @@ -10,6 +10,7 @@ "cwd": "${workspaceRoot}", "environment": [], "externalConsole": true, + "preLaunchTask": "build", "setupCommands": [ { "description": "Enable pretty-printing for gdb", diff --git a/Code Samples/Fib/.vscode/tasks.json b/Code Samples/Fib/.vscode/tasks.json index 49c3004f1..eebfc1015 100644 --- a/Code Samples/Fib/.vscode/tasks.json +++ b/Code Samples/Fib/.vscode/tasks.json @@ -1,34 +1,37 @@ -{ - "version": "2.0.0", - "tasks": [] -} -```jsonc -{ - "version": "2.0.0", - "tasks": [] -} -``` -{ - // See https://go.microsoft.com/fwlink/?LinkId=733558 - // for the documentation about the tasks.json format - "version": "2.0.0", - // The explicit "build" task was removed because the sample now uses a direct g++ command - // (the Makefile was removed). If you prefer a build task, add one that runs the - // appropriate g++ command for your platform or call `build.cmd` on Windows. - "tasks": [] -} { // See https://go.microsoft.com/fwlink/?LinkId=733558 // for the documentation about the tasks.json format "version": "2.0.0", - { - // See https://go.microsoft.com/fwlink/?LinkId=733558 - // for the documentation about the tasks.json format - "version": "2.0.0", - // The explicit "build" task was removed because the sample now uses a direct g++ command - // (the Makefile was removed). If you prefer a build task, add one that runs the - // appropriate g++ command for your platform or call `build.cmd` on Windows. - "tasks": [] - } - "echo": true, + "tasks": [ + { + "label": "build", + "type": "shell", + "command": "g++", + "args": [ + "-g", + "*.cpp", + "-std=c++11", + "-o", + "${workspaceFolder}/fib.out" + ], + "options": { + "cwd": "${workspaceFolder}" + }, + "windows": { + "command": "g++", + "args": [ + "-g", + "*.cpp", + "-std=c++11", + "-o", + "${workspaceFolder}/fib.exe" + ] + }, + "group": { + "kind": "build", + "isDefault": true + }, + "problemMatcher": ["$gcc"] + } + ] } diff --git a/Code Samples/Fib/README.md b/Code Samples/Fib/README.md index a9f9eb045..ce2b25d0c 100644 --- a/Code Samples/Fib/README.md +++ b/Code Samples/Fib/README.md @@ -1,10 +1,10 @@ # 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. The Makefile was removed and the sample is built with g++ directly. +Use one of the commands below to build the sample. ```bash # Linux / macOS @@ -14,38 +14,19 @@ g++ -g *.cpp -std=c++11 -o fib.out g++ -g *.cpp -std=c++11 -o fib.exe ``` -On Windows you can also run the included `build.cmd` if you prefer (it expects the path to a MinGW/Cygwin `bin` folder and an output name): +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 fib.exe ``` -After building, use the `launch.json` in this folder (or your own) to debug the produced binary. -```markdown -# 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. - -## Building - -Use one of the commands below to build the sample. The Makefile was removed and the sample is built with g++ directly. - -```bash -# Linux / macOS -g++ -g *.cpp -std=c++11 -o fib.out +## Debugging -# Windows (MinGW) -g++ -g *.cpp -std=c++11 -o fib.exe -``` +Update `launch.json` in the `.vscode` folder to point to your debugger if needed (e.g. set `miDebuggerPath` on Windows). -On Windows you can also run the included `build.cmd` if you prefer (it expects the path to a MinGW/Cygwin `bin` folder and an output name): +Press **F5** to build and launch the debugger. The build task compiles the sample automatically before starting the debug session. -```powershell -.\build.cmd fib.exe -``` - -After building, use the `launch.json` in this folder (or your own) to debug the produced binary. -``` -# Fib +## Notes -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. \ No newline at end of file +- 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. \ No newline at end of file diff --git a/Code Samples/Fib/build.cmd b/Code Samples/Fib/build.cmd index 8b78dafa9..05879b7cf 100644 --- a/Code Samples/Fib/build.cmd +++ b/Code Samples/Fib/build.cmd @@ -1,3 +1,3 @@ -@@ -1,2 +0,0 @@ -SET PATH=%PATH%;%1 -g++ -g *.cpp -lpthread --std=c++11 -O0 -o %2 +@echo off +SET PATH=%PATH%;%1 +g++ -g *.cpp --std=c++11 -O0 -o %2 diff --git a/Code Samples/Fib/main.cpp b/Code Samples/Fib/main.cpp index e7ab0c4c9..d6ece9855 100644 --- a/Code Samples/Fib/main.cpp +++ b/Code Samples/Fib/main.cpp @@ -1,30 +1,25 @@ #include -#include -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include #include "thread.h" #define THREAD_COUNT 10 static char block[] = "--block"; -int test = 0; +std::atomic 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])) { @@ -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; } diff --git a/Code Samples/Fib/thread.cpp b/Code Samples/Fib/thread.cpp index bef34e9c7..74b42d64a 100644 --- a/Code Samples/Fib/thread.cpp +++ b/Code Samples/Fib/thread.cpp @@ -1,17 +1,34 @@ #include -#include -#include -#include -#include -#include -#include -#include +#include +#include +#include + +// 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 +#elif defined(__linux__) +# include +#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; @@ -19,29 +36,17 @@ static int fib(int n){ } } -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; diff --git a/Code Samples/Fib/thread.h b/Code Samples/Fib/thread.h index 6af19e651..16082a326 100644 --- a/Code Samples/Fib/thread.h +++ b/Code Samples/Fib/thread.h @@ -1 +1 @@ -void * thread_proc(void* ctx); +void thread_proc(int tid);