windows vscode使用C11标准库自带的线程库
时间: 2024-01-25 18:03:50 浏览: 22
C11 标准库中包含了线程库,您可以使用该库在 Windows 上进行多线程编程。
以下是一个使用 C11 标准库的示例代码:
```c
#include <stdio.h>
#include <threads.h>
int thread_func(void* arg) {
printf("Hello from thread!\n");
return 0;
}
int main() {
thrd_t t;
thrd_create(&t, thread_func, NULL);
thrd_join(t, NULL);
return 0;
}
```
在这个例子中,我们使用 thrd_create() 函数创建了一个新的线程,该线程执行 thread_func() 函数。我们通过调用 thrd_join() 等待线程执行完毕。
请注意,您需要使用 -std=c11 编译器选项启用 C11 标准库支持。在 VS Code 中,您可以在 tasks.json 文件中添加以下选项来编译和链接您的代码:
```json
{
"tasks": [
{
"label": "build",
"type": "shell",
"command": "gcc -std=c11 ${file} -o ${fileBasenameNoExtension}.exe",
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
```
请将上面的代码示例保存在名为 thread_example.c 的文件中,并使用上面的任务来编译和运行它。