public inbox for pthreads-win32@sourceware.org
 help / color / mirror / Atom feed
* isCurrentThread()
@ 2021-04-03  8:16 Markus Forrer
  2021-04-03 22:15 ` isCurrentThread() ross.johnson
  0 siblings, 1 reply; 3+ messages in thread
From: Markus Forrer @ 2021-04-03  8:16 UTC (permalink / raw)
  To: pthreads-win32

I have implemented a method in my thread class Win32Thread to find out if it is the current thread:

bool Win32Thread::isCurrentThread() const
{
    pthread_t currentThread = pthread_self();

    return pthread_equal(currentThread, mThread) != 0;
}


Unfortunately, this implementation does not work. The function pthread_self() gets the current win32 threadId with GetCurrentThreadId(). But how do I have to check in my method isCurrentThread() if the current instance of Win32Thread is the current thread? I have not found an alternative to pthread_equal() in the PThread Api.

Thanks for any hints
Markus

 
 

^ permalink raw reply	[flat|nested] 3+ messages in thread

* RE: isCurrentThread()
  2021-04-03  8:16 isCurrentThread() Markus Forrer
@ 2021-04-03 22:15 ` ross.johnson
  2021-04-08  4:43   ` isCurrentThread() Markus Forrer
  0 siblings, 1 reply; 3+ messages in thread
From: ross.johnson @ 2021-04-03 22:15 UTC (permalink / raw)
  To: Markus Forrer, pthreads-win32

In pthreads-win32 (now pthreads4w on SourceForge), the pthread handle pthread_t is not the W32ThreadHandle. If you want the corresponding W32 thread handle you can use the function:HANDLE
pthread_getw32threadhandle_np(pthread_t thread);

	Returns the win32 thread handle that the POSIX
	thread "thread" is running as.

	Applications can use the win32 handle to set
	win32 specific attributes of the thread.If you want the W32 thread ID use:DWORD
pthread_getw32threadid_np (pthread_t thread)

	Returns the Windows native thread ID that the POSIX
	thread "thread" is running as.

        Only valid when the library is built where
        ! (defined(__MINGW64__) || defined(__MINGW32__)) || defined (__MSVCRT__) || defined (__DMC__)
        and otherwise returns 0.
As the _np suffix indicates, these are not portable functions.Another way you may be able to track and manage your threads is to use:__int64
pthread_getunique_np (pthread_t thr)

		Returns the unique number associated with thread thr.
		The unique numbers are a simple way of positively identifying a thread when
		pthread_t cannot be relied upon to identify the true thread instance. I.e. a
		pthread_t value may be assigned to different threads throughout the life of a 
		process.
		
		Because pthreads4w (pthreads-win32) threads can be uniquely identified by their
		pthread_t values this routine is provided only for source code compatibility.
		
		NOTE: if the library is re-initialised, i.e. by calling pthread_win32_process_detach_np()
		followed by pthread_win32_process_attach_np(), then the unique number is reset along with
		several other library global values. Library reinitialisation should not be required,
		however, some older applications may still call these routines as they were once required to
		do when statically linking the library.Again, this is non portable but several other pthreads implementations provide it.Sent from my phone
-------- Original message --------From: Markus Forrer via Pthreads-win32 <pthreads-win32@sourceware.org> Date: 3/4/21  7:16 pm  (GMT+10:00) To: pthreads-win32@sourceware.org Subject: isCurrentThread() I have implemented a method in my thread class Win32Thread to find out if it is the current thread:bool Win32Thread::isCurrentThread() const{    pthread_t currentThread = pthread_self();    return pthread_equal(currentThread, mThread) != 0;}Unfortunately, this implementation does not work. The function pthread_self() gets the current win32 threadId with GetCurrentThreadId(). But how do I have to check in my method isCurrentThread() if the current instance of Win32Thread is the current thread? I have not found an alternative to pthread_equal() in the PThread Api.Thanks for any hintsMarkus  

^ permalink raw reply	[flat|nested] 3+ messages in thread

* RE: RE: isCurrentThread()
  2021-04-03 22:15 ` isCurrentThread() ross.johnson
@ 2021-04-08  4:43   ` Markus Forrer
  0 siblings, 0 replies; 3+ messages in thread
From: Markus Forrer @ 2021-04-08  4:43 UTC (permalink / raw)
  To: ross.johnson; +Cc: pthreads-win32

Thank you for your answer. With your hint I have solved the problem.


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-04-08  4:44 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-03  8:16 isCurrentThread() Markus Forrer
2021-04-03 22:15 ` isCurrentThread() ross.johnson
2021-04-08  4:43   ` isCurrentThread() Markus Forrer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).