2 ms·
> Thread-local variables are referenced with the thread_local keyword. You must include threads.h in order to use it: #include <threads.h> void some_fu
by eqvinox 5y ago
> Thread-local variables are referenced with the thread_local keyword. You must include threads.h in order to use it:
#include <threads.h>
void some_function(void) {
thread_local char scratchpad[500000];
memset(scratchpad, 'A', sizeof scratchpad);
}
As an important note, thread-local storage through this keyword still isn't supported on OpenBSD. It's a serious PITA.
[——— also, copied from a reply I posted below: ———]
The autofree macro is wrong since __attribute__((cleanup)) expects a function that takes an additional level of pointer. In this case, it'll call "free(&scratchpad);". Which doesn't get you a compiler warning in C because passing a char ** as a void * is perfectly fine. But your heap is f*cked after this.
Correct way to do it is
void free2(char **p)
{
free(*p);
}
#define autofree __attribute__((cleanup(free2)))