使用c的标准库函数创建线程

c 复制代码
#include <stdio.h>
#include <threads.h>
#include <time.h>

int thrd_proc(void * varg){
	 
	// 打印10次
    int times = 10;
	struct timespec ts = {1,0}; // 1秒, 0纳秒
	
	while(times--){
		printf("%s\n",(char *)varg);
		
		// 每隔1秒,打印一次
		thrd_sleep(&ts,0);
	}
}

// 使用c的标准库函数创建线程
int main(void)
{
    thrd_t t1, t2;
	
	printf("hello\n");
	thrd_create(&t1, thrd_proc, "thread 1");
	thrd_create(&t2, thrd_proc, "thread 2");
	
	thrd_join(t1,0);
	thrd_join(t2,0);
	
    return 0;
}

gcc mainthread.c 编译报错。

gcc mainthread.c -lpthread 加上 -lpthread 后,即编译正常.

./a.out 运行程序,正常运行

相关推荐
古一|几秒前
Vue3中ref与reactive实战指南:使用场景与代码示例
开发语言·javascript·ecmascript
宠友信息13 分钟前
java微服务驱动的社区平台:友猫社区的功能模块与实现逻辑
java·开发语言·微服务
驰羽19 分钟前
[GO]golang接口入门:从一个简单示例看懂接口的多态与实现
开发语言·后端·golang
ii_best38 分钟前
IOS/ 安卓开发工具按键精灵Sys.GetAppList 函数使用指南:轻松获取设备已安装 APP 列表
android·开发语言·ios·编辑器
王夏奇44 分钟前
C++友元函数和友元类!
开发语言·c++
Full Stack Developme1 小时前
jdk.random 包详解
java·开发语言·python
懒羊羊不懒@1 小时前
Java基础入门
java·开发语言
froginwe112 小时前
R 矩阵:解析与应用
开发语言
_OP_CHEN2 小时前
C++基础:(十六)priority_queue和deque的深度解析
开发语言·c++
C++ 老炮儿的技术栈2 小时前
include″″与includ<>的区别
c语言·开发语言·c++·算法·visual studio