Skip to content
Merged
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
40 changes: 40 additions & 0 deletions Code Samples/Fib/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"preLaunchTask": "build",
"type": "cppdbg",
"request": "launch",
"args": [],
"stopAtEntry": true,
"cwd": "${workspaceRoot}",
"environment": [],
"externalConsole": true,
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"logging": {
"engineLogging": false,
"trace": false
},
"windows": {
"program": "${workspaceRoot}/fib.exe",
"MIMode": "gdb",
"miDebuggerPath": "<Path/To/GDB>" // Path to gdb on windows
},
"linux": {
"program": "${workspaceRoot}/fib.out",
"MIMode": "gdb"
},
"osx": {
"program": "${workspaceRoot}/fib.out",
"MIMode": "lldb"
}
}
]
}
50 changes: 50 additions & 0 deletions Code Samples/Fib/.vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"taskName": "build",
"type": "shell",
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "shared"
},
"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",
"-o",
"fib.out"
]
},
"osx": {
"command": "g++",
"args": [
"-g",
"*.cpp",
"-lpthread",
"--std=c++11",
"-o",
"fib.out"
]
}
}
]
}
3 changes: 3 additions & 0 deletions Code Samples/Fib/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# 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.
2 changes: 2 additions & 0 deletions Code Samples/Fib/build.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SET PATH=%PATH%;%1
g++ -g *.cpp -lpthread --std=c++11 -O0 -o %2
58 changes: 58 additions & 0 deletions Code Samples/Fib/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#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.h"

#define THREAD_COUNT 10

static char block[] = "--block";
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];

if (argc == 2 && !strcmp(block, argv[1]))
{
std::cout << helpText << std::endl;
volatile int foo = 1;
while (foo)
;
}

if (argc == 2 && !strcmp("--crash", argv[1]))
{
int foo = 0;
int bar = 1 / foo;
}

for (int i = 0; i < THREAD_COUNT; i++)
{
std::cout << "Test " << i << std::endl;
pthread_create(&threads[i], NULL, &thread_proc, NULL);
}

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

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

return 1;
}
48 changes: 48 additions & 0 deletions Code Samples/Fib/thread.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#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.h"

static int g_tid = 0;

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)
{
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

// 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);

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

std::cout << thread_name << " exited!" << std::endl;
}
1 change: 1 addition & 0 deletions Code Samples/Fib/thread.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
void * thread_proc(void* ctx);