
GetThreadId 根据线程句柄得到线程ID。
GetWindowThreadProcessId ,根据窗口句柄得到此窗口所在线程的ID(也同时得到进程的ID)
OpenThread,能根据ID得到线程的句柄
boost如何获取线程id?
已经创建了一个线程对象,为boost::thread pthread = new boost::thread();
我想获取这个线程对象的id,
就像win32的函数GetCurrentThreadId返回值一样。
阅读文档发现pthread->get_id()能够返回一个boost::thread::id类型的对象
但是这个对象没有办法将其中的thread id打印出来,有谁知道该怎么办吗?
调试窗口能够看到这个threadid的值,但是无法转为int值
native_handle()方法可以
boost::thread class has members native_handle_type and native_handle providing access to the underlying native handle
This native handle can be used to change for example the scheduling
11
XML to PDF by RenderX XEP XSL-FO Formatter, visit us at
Thread
In general, it is not safe to use this handle with operations that can conflict with the ones provided by BoostThread An example of
bad usage could be detaching a thread directly as it will not change the internals of the boost::thread instance, so for example
the joinable function will continue to return true, while the native thread is no more joinable
thread t(fct);
thread::native_handle_type hnd=tnative_handle();
pthread_detach(hnd);
assert(tjoinable());
通过查看资料,发现一种比较简单的方法就是在代码中使用printf将当前线程的id打印出来。
而这也分成两种情况:
1 如果是pthread,则使用,
#include <pthreadh>
pthread_t pthread_self(void);
2 如果不是pthread,即是由内核创建的线程,则使用,
#include <sys/typesh>
pid_t gettid(void);
获取线程所在的进程的id,方法如下:
#include <sys/typesh>
#include <unistdh>
pid_t getpid(void);
pid_t getppid(void);
所以,我们在代码中使用如下的语句打印:
printf("\ntid=%lu, pid=%lu\n", gettid(), getpid());
这样就能获取当前代码所在的线程和进程了。
根据打印出来的进程的pid,获取进程名的方法是:
ls -lh /proc/pid/exe
lrwxrwxrwx 1 root root 0 Jan 1 20:48 /proc/pid/exe ->
sh-32#
查看thread id的方法有:
1 sh-32# ps -efL | grep process,
ps命令指定-L命令选项可以用来查看进程下所包含的所有线程。
2 sh-32# ls -l /proc/pid/task/
查看进程下当前有哪些task,这些task指的就是线程。
线程是进程内部的一个执行单元,一个进程可以有多个线程,但是这些线程仅存在于该进程中,纯种是在它所属的进程地址空间里执行的,并在进程的地址空间对数据进行 *** 作。
输入下面的语句:
Requirements
Client
Requires Windows Vista
Server
Requires Windows Server 2008 or Windows Server 2003
Header
Declared in Winbaseh; include Windowsh
Library
Use Kernel32lib
DLL
Requires Kernel32dll
,线程是进程内部的一个执行单元,一个进程可以有多个线程,但是这些线程仅存在于该进程中,纯种是在它所属的进程地址空间里执行的,并在进程的地址空间对数据进行 *** 作。
以上对进程和线程做了简单的描述,
我个人认为,进程就好比 一条马路,汽车就好比线程,汽车在路上跑……就好比线程在进程的地址空间里运行……仅此而已……
用 GetThreadID( );函数可以得到线程ID
线程是进程内部的一个执行单元,一个进程可以有多个线程,但是这些线程仅存在于该进程中,纯种是在它所属的进程地址空间里执行的,并在进程的地址空间对数据进行 *** 作。
输入下面的语句:
Requirements
Client
Requires Windows Vista
Server
Requires Windows Server 2008 or Windows Server 2003
Header
Declared in Winbaseh; include Windowsh
Library
Use Kernel32lib
DLL
Requires Kernel32dll
以上就是关于c# 怎样获得想要的线程ID全部的内容,包括:c# 怎样获得想要的线程ID、boost怎么获取线程id、如何获取linux内核线程的pid等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)