外包优化网站个人盈利网站怎么建立
好的, 现在简单的写一个例子,
1、线程函数
函数int runThread(void *arg)
做为一个thread,实现简单的打印信息功能。
2、在int testSimpleThread()中,
- thrd_create(),创建线程。三个参数粉分别是thrd_t,线程函数,线程的参数。
- thrd_join(),等待进程结束。
可以直接在main()调用这个函数,做个简单的实验。
注意:本篇主要是在Windows下实现多进程的验证学习,所以GetCurrentThreadId()是Windows专属的函数,要是在Mac或者Linux下面,要注意兼容性,回头再做讨论。
#include "tinycthread.h"int runThread(void *arg)
{const char *threadName = "Thread1";const char *threadMsg = (const char *)arg;unsigned long threadId = (unsigned long)GetCurrentThreadId();printf("Thread %s(%lu): %s\n", threadName, (unsigned long)threadId, threadMsg);return 0;
}int testSimpleThread()
{thrd_t thread;const char *threadMsg = "Hello from thread!";// create a threadint result = thrd_create(&thread, runThread, (void *)threadMsg);if (result != thrd_success){printf("Failed to create thread.\n");return -1;}int threadResult;if (thrd_join(thread, &threadResult) != thrd_success){printf("Failed to join thread.\n");return -1;}printf("Thread exited with: %d\n", threadResult);return 0;
}