c++的new thread
时间: 2023-09-20 08:06:58 浏览: 42
您好!关于C语言中创建新线程的问题,可以使用pthread库来实现。下面是一个简单的例子供参考:
```c
#include <stdio.h>
#include <pthread.h>
void *thread_function(void *arg) {
printf("Hello from a new thread!\n");
pthread_exit(NULL);
}
int main() {
pthread_t thread;
int result = pthread_create(&thread, NULL, thread_function, NULL);
if (result != 0) {
printf("Failed to create thread.\n");
return 1;
}
printf("Hello from the main thread!\n");
pthread_join(thread, NULL);
return 0;
}
```
在上面的例子中,`pthread_create` 函数用于创建一个新线程,它接受四个参数:线程标识符、线程属性、线程函数、传递给线程函数的参数。这里我们定义了一个名为 `thread_function` 的函数作为新线程的执行函数。`pthread_join` 函数用于等待新线程的结束。
编译并运行上述代码,您将看到输出中有来自主线程和新线程的问候信息。
希望这可以回答您的问题!如果您还有其他问题,请随时提问。