C++ ?? ??? ???? GDB? ??? ? ????. 1. ??? ?? ???? ??????. 2. ???? ?????. 3. ???? ??? ???
C++ ?? ???? ?? ??? ??: ?? ??? ??? ??? ????? ??? ??????
??
?? ??? ?????? ??????? ??? ?? ???? ? ??? ??? ????? ?? ??????. ? ????? C++?? ?? ??? ??? ????? ??? ??? ???? ??? ??? ???? ?? ??? ?????.
GDB? ??? ?? ??? ???
GDB(GNU ???)? C++ ?? ??? ??? ????? ?? ??? ?????. GDB? ???? ????? ??? ?????? ?? ??? ????.
- ??? ???? ? ??? ??? ??????(?:
g++ -gmulti...
).g++ -gmulti ...
)。 - 在 GDB 中設(shè)置斷點(diǎn)(例如:
break main
)。 - 運(yùn)行程序并在所需位置停止(例如:
run args
)。 - 使用
info threads
命令查看線程列表。 - 使用
thread <n>
命令切換到特定的線程。 - 使用其他 GDB 命令進(jìn)行調(diào)試,例如
next
、stepi
和locals
,分別用于單步執(zhí)行、逐行執(zhí)行和檢查局部變量。
實(shí)戰(zhàn)案例:調(diào)試一個(gè)死鎖多線程函數(shù)
以下是調(diào)試一個(gè)死鎖多線程函數(shù)的實(shí)戰(zhàn)案例:
#include <iostream> #include <thread> #include <mutex> std::mutex mutex; void thread_func() { while (true) { std::lock_guard<std::mutex> guard(mutex); std::cout << "Thread is holding the lock" << std::endl; std::this_thread::sleep_for(std::chrono::seconds(1)); } } int main() { std::thread t(thread_func); // Start the thread std::lock_guard<std::mutex> guard(mutex); // Attempt to acquire the lock in main std::cout << "Main thread is waiting for the lock" << std::endl; t.join(); // Wait for the thread to finish }
調(diào)試過(guò)程
在 GDB 中調(diào)試此函數(shù)時(shí),我們發(fā)現(xiàn)它死鎖了,因?yàn)橹骶€程嘗試獲取由另一個(gè)線程持有的鎖。要解決此問(wèn)題,我們可以執(zhí)行以下步驟:
- 使用
thread apply all bt
命令在所有線程中打印調(diào)用堆棧。 - 觀察到主線程和另一個(gè)線程都在等待相同的鎖。
- 使用
thread info <n>
命令檢查另一個(gè)線程的狀態(tài),發(fā)現(xiàn)它正在休眠。 - 使用
next
GDB? ???? ?????(?:
break main
). ????? ???? ??? ???? ?????(?: run args
). ??? ??? ??? info thread
??? ?????.
thread <n>
??? ?????. ????? ?? ???? ????? next
, stepi
? locals
? ?? ???? ?? GDB ??? ?????. ?? ??? ?? ?????. ?????????? ??: ?? ?? ?? ??? ?? ??? ??????? ?? ?? ?? ??? ?? ???? ?? ?????. ??#include <iostream> #include <thread> #include <mutex> #include <condition_variable> std::mutex mutex; std::condition_variable cv; void thread_func() { while (true) { std::unique_lock<std::mutex> guard(mutex); cv.wait(guard); // Wait for the condition variable to be notified std::cout << "Thread is holding the lock" << std::endl; std::this_thread::sleep_for(std::chrono::seconds(1)); } } int main() { std::thread t(thread_func); // Start the thread std::unique_lock<std::mutex> guard(mutex); cv.notify_all(); // Notify the other thread to acquire the lock guard.unlock(); // Release the lock in main t.join(); // Wait for the thread to finish }??????? ????????GDB?? ? ??? ???? ? ??? ??? ?? ?? ??????. ?? ?????. ?? ???? ?? ???? ??? ??? ????? ???? ?????. ? ??? ????? ?? ??? ??? ? ????. ??????
thread apply all bt
??? ???? ?? ???? ?? ??? ?????. ???? ?? ???? ?? ???? ?? ??? ??? ???? ??? ??????. ????thread info <n>
??? ???? ?? ???? ??? ???? ?? ???? ?? ???? ?????. ???? next
??? ???? ?? ???? ??? ?? ??? ??? ? ?? ?? ??? ?????. ?????????? ??????? ?? ??? ????? ?? ??? ???? ??? ? ???? ??? ? ????. ??? ?? ??? ??? ????. ??rrreee? ??? C++ ?? ???? ?? ??? ??: ?? ??? ??? ??? ????? ??? ??????? ?? ?????. ??? ??? PHP ??? ????? ?? ?? ??? ?????!

? AI ??

Undress AI Tool
??? ???? ??

Undresser.AI Undress
???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover
???? ?? ???? ??? AI ?????.

Clothoff.io
AI ? ???

Video Face Swap
??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

?? ??

??? ??

???++7.3.1
???? ?? ?? ?? ???

SublimeText3 ??? ??
??? ??, ???? ?? ????.

???? 13.0.1 ???
??? PHP ?? ?? ??

???? CS6
??? ? ?? ??

SublimeText3 Mac ??
? ??? ?? ?? ?????(SublimeText3)

??? ??? ?? ?? ???? ?? ????? ???? ?? ??? ? ?? ? ?????. ?? ?? ???? ????? ???? ??, ???? ? ??? ??? ?? ?????. ??? ??? ????? ???? ??? ??? ?? ??? ???? ???? ?? ? ?? ??? ??? ? ????. ???? ??? ??? ??? ?? ???? ? ??? ??? ???? ???? ?? ? ?????.

RAII? C? ?? ??? ???? ??? ?????. ??? ?? ????? ?? ??? ???? ???? ? ????. ?? ????? ??? ????. ??? ?? ??? ???? ??? ????? ?? ??? ?? ?? ??? ????. ?? ??, RAII??? ?? ?? ??? ???? fclose? ???????. ??? ??? ??? ?? ?? ?? ??? ?? ?? ?? ? ????. ?? ?? ???? ?? RAII? ??? ? ?? ??? ????? ???? ??? ?? ?? ??? ??????. 1.RAII? ?? ?? (? : std :: lock_guard), 2. ??? ?? (? : std :: ?? ?), 3. ?????? ? ???? ?? ?? ?? ?????.

std :: ??? ? ?? ??? ?? 4 ?? ???? ??? ????. 1. ?? () ???? ???? ??? ?? ?? ??? ??? ??? ?? ?? ???? ?? ????. 2. ?? [0]? ???? Front ()? ??? ? ??? ??? ?? ?? ??? ????????. 3. ?? ????? ? STL ????? ??? *?? () ??; 4. ???? ????? ?? ??? ?? ?? (0)? ???? ??? ?? ? ??? ?????. ?? ??? ?? ?? ??? ?????. ?? ??? ?? ? ()? ???? ?? ??? ??? ?? Front () ???? ???? ???? ?? ??? ??? ?? ? ?? ??? ?? ????.

PHP? AI ??? ?? ??? ??? ?? AI ??? API (? : OpenAI, HuggingFace)? ?????? ???? ??? ???, API ??, ?? ?? ? ?? ?????? ???? ????. 2. ??? ??? ??? ??? AI ???? ???? ????. ?? ??? API, ??? ?? ? ??? ??? ???? ????. 3. ?? ??? ?? ??, ??, ??, ???, ??? ????? ? GPT ?? BART/T5? ?? ?? ??? ???????. 4. ?? ????? ??, ??? ?, ?? ?? ? ?? ?? ??? ?????. ?? ??? ???? ????? ???? ??? ???? ?? ?? ?? ? ??, ???? ?? ??, ?? ??, ?? ?? ? ??? ???????.

?? ?? ??? ?? ??? ????? ??? ? ????. 1. I-th ??? 1?? ?????? : N & (1 ??

STD :: IS_SAME? ??? ???? ? ??? ??? ??? ? ??? ???? ?? ?? ???? ? ?????. 1. ?? ???? std :: is_same :: ?? t? u? ??? ?? ? ? ?? true???. ??? ??? ?????. const, reference, pointer ?? ?? ?? ???? ??? ????. 2. std :: remove_const, std :: remove_reference ? ?? ???? ?? ??? ??? ?? ?????? ??? ?? ??? ?? ? ? ????. 3. IFConstexpr? ??? ??? ??? ?? ?? ?? ???? ??? ?? ?? ??? ?? ???? ?? ??? ?? ?? ??? ?????. 4.

Move ???? MyClass (MyClass && Other) ???? ???? ?? ??? "??"?? ? ???? C? ?? ?????, ?? RValue ??? ???? ?? ????? ???? ? ??? ?????. 1. ?? ?? STL ?????? ??? ??? ???? ?? ??? ?????. 2. ?? ?? ???? ?? ???? ???? ?? ?? ??????. 3. ??? (? : ?? ???)? ???? ???? ???? ?? ? ? ??? ????? ??? ??? ?? ?? ????? ????? ??? ? ??? ?? ? ??? ????. 4. ?? ?? ???? Const ??? ??? ??? ??? ??? ??? ?? ?? ??? ??? ??? ???? ?? ?????.

C ABI? ????? ?? ??? ?? ? ? ??? ?? ????, ?? ??, ?? ????, ?? ?? ?? ?? ????? ?????. 1. ??? ??? ??? ???? ?? ??? ? ??? ?????. 2. ?? ???? ?? ??? ?? ????? ??, STL ??, ??? ??, ???? ??? ?? ? ??? ABI? ?? ? ? ????. ?? ? ?? ????? ABI ???? ?????? ???????.
