xianglyc opened a new pull request, #13973:
URL: https://github.com/apache/nuttx/pull/13973
## Summary
add support for thread_local
## Impact
no impact
## Testing
case:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define NUM_THREADS 5
/* Thread-local variable */
__thread int tls_var = 0;
void *thread_function(void *arg)
{
int thread_num = *((int *)arg);
/* Initialize the thread-local variable */
tls_var = thread_num;
/* Sleep to simulate some work */
sleep(1);
/* Print the value of the thread-local variable */
printf("Thread %d, tls_var = %d\n", thread_num, tls_var);
/* Modify the thread-local variable */
tls_var += 10;
/* Print the modified value of the thread-local variable */
printf("Thread %d, tls_var after modification = %d\n", thread_num,
tls_var);
return NULL;
}
int main()
{
pthread_t threads[NUM_THREADS];
int args[NUM_THREADS];
int ret;
int i;
/* Create threads */
for (i = 0; i < NUM_THREADS; ++i)
{
args[i] = i;
ret = pthread_create(&threads[i], NULL, thread_function, (void
*)&args[i]);
if (ret)
{
printf("ERROR: pthread_create() ret= %d\n", ret);
exit(EXIT_FAILURE);
}
}
/* Join threads */
for (i = 0; i < NUM_THREADS; ++i)
{
pthread_join(threads[i], NULL);
}
printf("done\n");
return 0;
}
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]