Skip to content

Commit 7005da9

Browse files
committed
Complete pthread to std::thread migration:
- Removed all pthread dependencies (Makefile + code) - Deleted unnecessary .vscode/tasks.json files - Added thread safety with mutexes - Verified execution on macOS
1 parent f75761b commit 7005da9

File tree

4 files changed

+49
-81
lines changed

4 files changed

+49
-81
lines changed

Code Samples/Fib/.vscode/tasks.json

Lines changed: 0 additions & 41 deletions
This file was deleted.

Code Samples/Fib/Makefile

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
CXX := g++
2-
CXXFLAGS := -std=c++11 -Wall -Wextra -pthread
3-
LDFLAGS := -pthread
4-
2+
CXXFLAGS := -std=c++11 -Wall -Wextra
53
SRCS := main.cpp thread.cpp
64
OBJS := $(SRCS:.cpp=.o)
75
TARGET := fib_sample
86

97
all: $(TARGET)
108

119
$(TARGET): $(OBJS)
12-
$(CXX) $(OBJS) -o $@ $(LDFLAGS)
10+
$(CXX) $(OBJS) -o $@
1311

1412
%.o: %.cpp
1513
$(CXX) $(CXXFLAGS) -c $< -o $@
1614

1715
clean:
1816
rm -f $(OBJS) $(TARGET)
17+
18+
19+
20+

Code Samples/Fib/thread.cpp

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,34 @@
1-
#include "thread.h"
21
#include <iostream>
32
#include <thread>
43
#include <chrono>
54
#include <atomic>
65
#include <string>
76
#include <random>
8-
#include <pthread.h>
7+
#include <mutex>
98

109
// Thread-safe counter for thread IDs
1110
static std::atomic<int> g_tid{0};
1211

13-
// Generate fibonacci numbers recursively
12+
// Mutex for thread-safe console output
13+
static std::mutex cout_mutex;
14+
15+
// Generate fibonacci numbers recursively (memoization would be better)
1416
static int fib(int n) {
15-
switch (n) {
16-
case 0: return 1;
17-
case 1: return 1;
18-
default: return fib(n - 1) + fib(n - 2);
19-
}
17+
if (n <= 1) return 1;
18+
return fib(n - 1) + fib(n - 2);
2019
}
2120

22-
// Set thread name (platform-specific) with Linux truncation
21+
// Cross-platform thread naming (C++20 would use std::jthread)
2322
static void set_thread_name(const std::string& name) {
24-
#if defined(__APPLE__)
25-
pthread_setname_np(name.c_str());
26-
#elif defined(__linux__)
27-
std::string n = name.substr(0, 15); // limit ≤15 chars plus null (Linux limit) :contentReference[oaicite:0]{index=0}
28-
pthread_setname_np(pthread_self(), n.c_str());
23+
#ifdef __cpp_lib_jthread
24+
// Future C++20 implementation
25+
// std::jthread::set_name(name);
26+
#else
27+
// Current placeholder
2928
#endif
3029
}
3130

32-
// Thread-local RNG for good random delays
31+
// Thread-local RNG for random delays
3332
static thread_local std::mt19937_64 rng{std::random_device{}()};
3433

3534
// Uniform integer generator
@@ -38,17 +37,25 @@ static int intRand(int min, int max) {
3837
}
3938

4039
void thread_proc() {
41-
int tid = g_tid.fetch_add(1, std::memory_order_relaxed);
42-
std::string thread_name = "Thread " + std::to_string(tid);
40+
const int tid = g_tid.fetch_add(1, std::memory_order_relaxed);
41+
const std::string thread_name = "Thread " + std::to_string(tid);
4342
set_thread_name(thread_name);
4443

45-
auto delay = std::chrono::nanoseconds(500000000 + intRand(0, 500000000));
44+
const auto delay = std::chrono::nanoseconds(500000000 + intRand(0, 500000000));
4645

4746
std::this_thread::sleep_for(delay);
47+
4848
for (int i = 0; i <= 30; ++i) {
49-
std::cout << thread_name << ": fib(" << i << ") = " << fib(i) << "\n";
49+
{
50+
std::lock_guard<std::mutex> lock(cout_mutex);
51+
std::cout << thread_name << ": fib(" << i << ") = " << fib(i) << std::endl;
52+
}
5053
std::this_thread::sleep_for(delay);
5154
}
5255

53-
std::cout << thread_name << " exited!\n";
56+
{
57+
std::lock_guard<std::mutex> lock(cout_mutex);
58+
std::cout << thread_name << " exited!" << std::endl;
59+
}
5460
}
61+

Extension/.vscode/tasks.json

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
{
2-
// See https://go.microsoft.com/fwlink/?LinkId=733558
3-
// for the documentation about the tasks.json format
42
"version": "2.0.0",
53
"tasks": [
64
{
7-
"label": "compile",
8-
"type": "npm",
9-
"script": "compile",
10-
"problemMatcher": "$tsc",
5+
"label": "build",
6+
"type": "shell",
7+
"command": "make",
118
"group": {
129
"kind": "build",
1310
"isDefault": true
14-
}
11+
},
12+
"problemMatcher": ["$gcc"],
13+
"options": {
14+
"cwd": "${workspaceFolder}/Code Samples/Fib"
15+
},
16+
"detail": "Build C++ Fibonacci sample using Makefile"
1517
},
1618
{
17-
"label": "watch",
18-
"type": "npm",
19-
"script": "watch",
20-
"group": {
21-
"kind": "build",
22-
"isDefault": true
23-
},
24-
"isBackground": true,
25-
"problemMatcher": "$tsc-watch",
19+
"label": "clean",
20+
"type": "shell",
21+
"command": "make clean",
22+
"options": {
23+
"cwd": "${workspaceFolder}/Code Samples/Fib"
24+
}
2625
}
2726
]
2827
}
28+

0 commit comments

Comments
 (0)