Thread: Win32 equivalent of pthread_equal

  1. #1
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,431

    Win32 equivalent of pthread_equal

    Doing an upgrade to my custom file/folder asset handling functions and decided to resolve the problem I think O_CLOEXEC attempts to resolve with process/thread checks, process check was easy enough with but thread checking is proving a little more difficult, anyone know what function is the equivalent or am I just gonna have to accept "asset->thread != thread" as the best method on win32? For reference here's the unfinished function I'm using:
    Code:
    dint TestLassetAccess( LASSET *Lasset, ach const *action )
    {
    	dint err = EACCES, pid = getpid();
    	SYNC *Sync = GetLassetSync(Lasset);
    	FILE *out = Sync->out;
    	thread_t thread;
    
    	if ( Lasset->pid != pid )
    	{
    		ECHO_ERROR( out, err );
    		fprintf( out, "File '%s' is assigned to process %d, process %d cannot %s it\n" );
    		return err;
    	}
    
    #ifdef _WIN32
    	thread = GetCurrentThread();
    	if ( Lasset->thread != thread )
    #else
    	thread = pthread_self();
    	if ( !pthread_equal( Lasset->thread, thread ) )
    #endif
    	{
    		ECHO_ERROR( out, err );
    		fprintf( out, "File '%s' is assigned to thread %d, thread %d cannot %s it\n" );
    		return err;
    	}
    
    	return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,811
    GetCurrentThread function (processthreadsapi.h) - Win32 apps | Microsoft Docs
    The function cannot be used by one thread to create a handle that can be used by other threads to refer to the first thread. The handle is always interpreted as referring to the thread that is using it. A thread can create a "real" handle to itself that can be used by other threads, or inherited by other processes, by specifying the pseudo handle as the source handle in a call to the DuplicateHandle function.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,431
    That didn't answer the question, just made it evident that comparing the handle won't work (which I will thank you for as I obviously missed it, thx)

  4. #4
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,431
    Ended up going with this:
    Code:
    ...
    #ifdef _WIN32
    typedef HANDLE		fasset_t;
    typedef HANDLE		thread_t;
    #else
    #include <fcntl.h>
    #include <pthread.h>
    typedef dint		fasset_t;
    typedef pthread_t	thread_t;
    #endif
    ...
    COMMON_EXPORT void
    	TestLassetAccess( LASSET *Lasset, thread_t *thread )
    {
    	assert( Lasset->pid == getpid() );
    	assert( Lasset->thread == thread );
    }
    Edit: Oh and this too:
    Code:
    COMMON_EXPORT void
    	ForkLasset( LASSET *Lasset, thread_t *thread )
    {
    	if ( thread )
    	{
    		/* Process and relavent thread take control here */
    		Lasset->pid = getpid();
    		Lasset->thread = thread;
    	}
    	else
    	{
    		/* Relinquish control here */
    		Lasset->pid = -1;
    		Lasset->thread = NULL;
    	}
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Win32 API, can it inFile and outFile, like Win32 console?
    By neutral87 in forum Windows Programming
    Replies: 6
    Last Post: 05-04-2013, 07:19 AM
  2. Win32 API, can it inFile and outFile, like Win32 console?
    By neutral87 in forum C++ Programming
    Replies: 0
    Last Post: 04-28-2013, 09:22 PM
  3. Win32 Console Application event handlers, or Win32 Project?
    By Joewbarber in forum C++ Programming
    Replies: 1
    Last Post: 03-29-2013, 07:02 AM
  4. Linux equivalent to Win32 ShellExecute
    By BobS0327 in forum Linux Programming
    Replies: 4
    Last Post: 06-07-2006, 04:35 AM
  5. win32 equivalent of system()
    By rotis23 in forum Windows Programming
    Replies: 16
    Last Post: 08-26-2004, 02:17 AM

Tags for this Thread