<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 10pt;
font-family:Verdana
}
--></style>
</head>
<body class='hmmessage'>
I think I resolved this in a simple efficient manner. Please take a look. Perhaps pthread_create is guaranteed to write back the pthread_t before the code runs though?<BR>
<BR>
Win32 isn't as amenable to the efficiency here due to the division between handles and thread ids.<BR>
pthread I'm not really so familiar with, not sure of the efficiency.<BR>
In particular, where pthreads has pthread_self(), Win32 has three analogs:<BR>
GetCurrentThreadId() <BR>
GetCurrentThread() <BR>
DuplicateHandle(GetCurrentThread()...) <BR>
<BR>
GetCurrentThread and GetCurrentThreadId are both fast -- no kernel call.<BR>
However you can't do all that much with their results, e.g. you can't Suspend/Resume/Close/Wait.<BR>
GetCurrentThread returns a special /constant/ that always means the current thread, relative to the caller.<BR>
DuplicateHandle(GetCurrentThread()...) gives you something you can Suspend/Resume/Close/Wait but of course is slower, a kernel call.<BR>
I have to pay for either the DuplicateHandle or ResumeThread, a wash.<BR>
<BR>
pthreads just has the one pthread_self. Fast? I don't know. A kernel call? I don't know. Usable with suspend/resume (np)? Yes.<BR>
<BR>
Well on Solaris it is fast:<BR>
-bash-3.00$ /usr/ccs/bin/dis -F pthread_self /usr/lib/libc.so<BR>disassembly for /usr/lib/libc.so<BR>
thr_self()<BR> thr_self: 81 c3 e0 08 retl<BR> thr_self+0x4: d0 01 e0 98 ld [%g7 + 0x98], %o0<BR>
<BR>
<BR>
Recent Linux/x86 is fast:<BR>
<BR>
objdump -d /usr/lib/libpthread_a<BR>
<BR>00001020 <__pthread_self>:<BR> 1020: 55 push %ebp<BR> 1021: 89 e5 mov %esp,%ebp<BR> 1023: 65 a1 50 00 00 00 mov %gs:0x50,%eax<BR> 1029: 5d pop %ebp<BR> 102a: c3 ret<BR> 102b: 90 nop<BR> 102c: 8d 74 26 00 lea 0x0(%esi),%esi<BR><BR>
<BR>
(geez though, six instructions where only two are needed, crazy..imagine if all code was unoptimal by that factor...)<BR>
<BR>
- Jay<BR><BR> <BR>
<HR id=stopSpelling>
From: jay.krell@cornell.edu<BR>To: hosking@cs.purdue.edu; m3devel@elegosoft.com<BR>Subject: race condition setting act.handle?<BR>Date: Mon, 7 Dec 2009 06:55:08 +0000<BR><BR>
<STYLE>
.ExternalClass .ecxhmmessage P
{padding:0px;}
.ExternalClass body.ecxhmmessage
{font-size:10pt;font-family:Verdana;}
</STYLE>
pthread_create(act.handle) followed at some point by use of act.handle in the new thread.<BR> Or possibly getting on the list of threads in the new thread and then a third thread looking at that list.<BR><BR><BR>What is to cause act.handle to be stored before the new thread starts and uses act.handle?<BR><BR><BR>For this reason in Win32 I create the thread suspended, store the handle, resume the thread.<BR>Apparently there is no direct equivalent in pthreads, you have to use your own mutex+condition variable.<BR><BR> - Jay<BR><BR> </body>
</html>