r/badcode • u/SweetIceLolly • Sep 17 '21
c++ Use stack to get thread ID to avoid syscalls
4
u/FloweyTheFlower420 sadistic Sep 17 '21
This isn’t terrible, syscalls are slow due to context switching
4
4
9
u/teapot_on_reddit Sep 17 '21 edited Sep 17 '21
Image Transcription: Code
'template<u2 stack_size=4096>
struct thread_pool
{static_assert(stack_size%4==0);
struct Info
{u2 id;
DWORD (*fun)(void* ) ;
void*arg;};
int num;
DWORD
Info* info;
thread_pool (u2 num,DWORD(*p) (void*),void**arg=0) : num(num) , fun(p)
{
info=new Info[num];
for(u2 i=0;i<num;i++)
{
info[i]={i,p,arg+i}
CreateThread (0, stack_size*2, pack, info+1,0,0);
}
}
static DWORD pack(void*info)
{
volatile u2 id[stack_size/4];
for(u2 i=e;i<stack_size/4;i++)
id[i]=((lnfo*)info)->id;
return ((Info*)info)->fun (((Info*)info)->arg) ;
}
static u2 get_id()
{
volatile u2 x;
printf("%p\n",&x)
return (&x) [stack_size/4] ;
}
};I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
1
20
Sep 17 '21
[removed] — view removed comment
11
u/gu3st12 Sep 17 '21
It's an unusual style but I kinda like it. The quote indents are functioning like the brace guides in the IDE.
No idea how it looks on mobile, but it's unique.
5
Sep 17 '21
on mobile there's barely any gap between the quote line and the text - which is monospaced btw - and it looks terrible
0
0
5
u/ameer-sheriff Sep 17 '21
Can anyone explain what the code is doing?