如何在Windows和Linux下获取当前线程的ID号

如何在Windows和Linux下获取当前线程的ID号,第1张

Linux下获取当前线程ID号函数:

pthread_t pthread_self();

返回:当前线程的ID号

pthread_t 数据类型的定义如下:

typedef unsigned long int pthread_t;

sizeof(pthread_t) = 4,4个字节的整数。

Windows下获取当前线程ID号的函数:

DWORD GetCurrentThreadId();

返回值:当前线程的ID号

DWORD数据类型定义:

typedef unsigned long DWORD;

在Windows下pthread-win库的pthread_t定义如下:

typedef struct {

void p; /Pointer to actual object /

unsigned int x; /Extra information - reuse count etc /

} ptw32_handle_t;

typedef ptw32_handle_t pthread_t;

与Linux的thread_t不一样,它是一个结构,不是一个整数值

在Windows和Linux下可用的获取线程ID号的内联函数如下:

#ifdef WIN32

#include <windowsh>

#else

#include <pthreadh>

#endif

inline unsigned int PthreadSelf()

{

#ifdef WIN32

return::GetCurrentThreadId();

#else

returnthread_self();

#endif

}

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

但是box的insertItem()函数不起作用。

我的线程放在了Global类里,现在需要在线程里对别的窗口内的控件进行 *** 作。

------解决方案--------------------------------------------------------我建议的做法是用FindWindowEx取窗口句柄,然后再用GetDlgItem之类的取控件句柄,通过API进行 *** 作。

------解决方案--------------------------------------------------------线程最好不要对别的窗口内的控件进行 *** 作,而是发送消息给窗口,让主线程来 *** 作界面。

还有你说不起作用可能是你未强制重画,试试插入项后调用一下

CListCtrl::RedrawItems 或

Cwnd::UpdateWindow

GetWindowThreadProcessId,它根据窗口的句柄来获取进程和线程ID,VB原型如下

'Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long

'参数:

'hwnd:目标窗口的句柄

'lpdwProcessId:一个接受返回进程ID的Long变量,

'顺便提一下,在我们调用API的时候,如果一个变量不是ByVal传递,则表示这个变量是用来接受返回值的(但也不是绝对的)

'返回值:

'窗口的线程ID

以上就是关于如何在Windows和Linux下获取当前线程的ID号全部的内容,包括:如何在Windows和Linux下获取当前线程的ID号、boost怎么获取线程id、如何获得一个调用某个函数时产生的线程的句柄或ID等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9483623.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-28
下一篇2023-04-28

发表评论

登录后才能评论

评论列表(0条)

    保存