/*类型thrd_t范例*/
#ifdef __STDC_NO_THREADS__
#error "Implementation does not support multi-threads."
#endif
#include <stdio.h>
#include <stdlib.h>
#include <threads.h>
/*新线程中执行的函数。*/
int func(void *str)
{
puts((char *)str);
return 0;
}
int main(void)
{
thrd_t threadId;
char arr[] = "This is a new thread.";
/*创建线程。*/
if(thrd_create(&threadId, func, arr)!= thrd_success)
{
perror("thrd_create error");
exit(EXIT_FAILURE);
}
/*连接线程。*/
if(thrd_join(threadId, NULL)!= thrd_success)
{
perror("thrd_join error");
exit(EXIT_FAILURE);
}
puts("This is the main thread.");
return 0;
}
|