boost::thread简介和gdb调试线程

来源:互联网 发布:淘宝网美都袋鼠皮相 编辑:程序博客网 时间:2024/05/21 03:56

File:

#include <boost/thread/thread.hpp>

class thread{public:    class attributes; // EXTENSION    thread() noexcept;    thread(const thread&) = delete;    thread& operator=(const thread&) = delete;    thread(thread&&) noexcept;    thread& operator=(thread&&) noexcept;    ~thread();    template <class F>    explicit thread(F f);    template <class F>    thread(F &&f);    template <class F,class A1,class A2,...>    thread(F f,A1 a1,A2 a2,...);    template <class F, class ...Args>    explicit thread(F&& f, Args&&... args);    template <class F>    explicit thread(attributes& attrs, F f); // EXTENSION    template <class F>    thread(attributes& attrs, F &&f); // EXTENSION    template <class F, class ...Args>    explicit thread(attributes& attrs, F&& f, Args&&... args);    // move support    thread(thread && x) noexcept;    thread& operator=(thread && x) noexcept;    void swap(thread& x) noexcept;    class id;    id get_id() const noexcept;    bool joinable() const noexcept;    void join();    template <class Rep, class Period>    bool try_join_for(const chrono::duration<Rep, Period>& rel_time); // EXTENSION    template <class Clock, class Duration>    bool try_join_until(const chrono::time_point<Clock, Duration>& t); // EXTENSION    void detach();    static unsigned hardware_concurrency() noexcept;    typedef platform-specific-type native_handle_type;    native_handle_type native_handle();    void interrupt(); // EXTENSION    bool interruption_requested() const noexcept; // EXTENSION#if defined BOOST_THREAD_USES_DATETIME    bool timed_join(const system_time& wait_until); // DEPRECATED    template<typename TimeDuration>    bool timed_join(TimeDuration const& rel_time); // DEPRECATED    static void sleep(const system_time& xt);// DEPRECATED#endif#if defined BOOST_THREAD_PROVIDES_THREAD_EQ    bool operator==(const thread& other) const; // DEPRECATED    bool operator!=(const thread& other) const; // DEPRECATED#endif    static void yield() noexcept; // DEPRECATED};void swap(thread& lhs,thread& rhs) noexcept;
//具体提供的接口可以参考boost参考文档

namespace boost {
  class thread;
  class thread_group;
}

从名字可以看出来,thread为封装线程使用的对象,thread_group为线程组使用的对象


简单举例说明:

void Run(){prctl(PR_SET_NAME,(unsigned long)"yx");cout<<"thread:yx"<<endl;    sleep(100);}
boost::thread th(Run);th.join();


//这里顺便设置了下线程名词,具体可参考系统调用prctl,使用比较简单,这里不再介绍
yixiao@yixiao-p6-1319cx:/proc/2840$ ps -ef|grep -i a.out|grep -v grep

yixiao    3186  2101  0 22:05 pts/1    00:00:00 ./a.out


yixiao@yixiao-p6-1319cx:/proc/3186$ ps -L -p 3186
  PID   LWP TTY          TIME CMD
 3186  3186 pts/1    00:00:00 a.out
 3186  3187 pts/1    00:00:00 yx


thread_group的使用也较为简单:

void Run1(){prctl(PR_SET_NAME,(unsigned long)"yx_1");cout<<"thread:yx_1"<<endl;    sleep(100);}
boost::thread_group gp;gp.create_thread(Run);gp.add_thread(new boost::thread(Run1));gp.join_all();


yixiao@yixiao-p6-1319cx:/proc/3186$ ps -ef|grep -i a.out|grep -v grep
yixiao    3552  2101  0 22:10 pts/1    00:00:00 ./a.out


yixiao@yixiao-p6-1319cx:/proc/3186$ ps -L -p 3552
  PID   LWP TTY          TIME CMD
 3552  3552 pts/1    00:00:00 a.out
 3552  3553 pts/1    00:00:00 yx
 3552  3554 pts/1    00:00:00 yx_1


gdb调试线程:

基本命令:

info threads                      --显示当前可调试的所有进程
thread ID                         --切换当前调试的线程为指定ID的线程
thread apply ID1 ID2 command      --让一个或多个线程执行GDB的命令command
thread apply all command          --让所有被调试线程执行GDB命令command
set scheduler-locking off|on|step 
 -off  不锁定任何进程,所有线程都执行
 -on   只有当花钱被调试程序会执行
 -step 在单步的时候,除了next过一个函数的情况外,只有当前线程会执行


(gdb) info threads
  Id   Target Id         Frame 
  3    Thread 0x7ffff65d0700 (LWP 3696) "a.out" 0x00007ffff73df3c1 in clone () from /lib/x86_64-linux-gnu/libc.so.6
* 2    Thread 0x7ffff6dd1700 (LWP 3695) "a.out" Run () at test_thread.cc:10
  1    Thread 0x7ffff7fdf740 (LWP 3692) "a.out" 0x00007ffff73df3c1 in clone () from /lib/x86_64-linux-gnu/libc.so.6

(gdb) thread 2
[Switching to thread 2 (Thread 0x7ffff6dd1700 (LWP 3695))]
#0  Run () at test_thread.cc:10
10 prctl(PR_SET_NAME,(unsigned long)"yx");


(gdb) thread apply all continue


Thread 3 (Thread 0x7ffff65d0700 (LWP 3696)):
Continuing.
thread:yx


Breakpoint 1, Run1 () at test_thread.cc:17
17 prctl(PR_SET_NAME,(unsigned long)"yx_1");


Thread 2 (Thread 0x7ffff6dd1700 (LWP 3695)):
Continuing.

0 0
原创粉丝点击