#include <stdio.h>
#include <pthread.h>
void *user_thread(void *arg)
{
int i = 5;
while (i--) {
printf("L%d, test_thread run %d.\n", __LINE__, i);
sleep(3);
}
return (0);
}
int main (int argc, char **argv)
{
int rst;
pthread_t tid;
pthread_attr_t attr;
rst = pthread_attr_init(&attr);
if (rst != 0) {
fprintf(stderr, "pthread attr init failed.\n");
return (-1);
}
int stacksize = 4096;
rst = pthread_attr_setstacksize(&attr, stacksize);
if (rst != 0) {
fprintf(stderr, "pthread attr setstacksize failed.\n");
}
const char *thread_name = "user_thread_name";
rst = pthread_attr_setname(&attr, thread_name);
if (rst != 0) {
fprintf(stderr, "pthread attr setname failed.\n");
}
rst = pthread_create(&tid, &attr, user_thread, NULL);
if (rst != 0) {
fprintf(stderr, "pthread create failed.\n");
return (-1);
}
pthread_join(tid, NULL);
pthread_attr_destroy(&attr);
printf("L%d, test_thread finish.\n", __LINE__);
return (0);
}
翼辉实时系统线程示例
于 2021-09-22 16:10:02 首次发布