使用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 运行程序,正常运行

相关推荐
heath ceTide2 分钟前
轻量、优雅、高扩展的事件驱动框架——Hibiscus-Signal
java·开发语言
_extraordinary_3 分钟前
Java 常用的Arrays函数
java·开发语言
_extraordinary_6 分钟前
Java 类和对象
java·开发语言
Aliano21710 分钟前
TestNGException ClassCastException SAXParserFactoryImpl是Java自带的Xerces解析器——解决办法
java·开发语言·python
漫谈网络14 分钟前
回调函数应用示例
开发语言·python·回调函数
亚林瓜子29 分钟前
pyenv简单的Python版本管理器(macOS版)
开发语言·python·macos·pyenv
夜松云30 分钟前
Qt信号槽机制与UI设计完全指南:从基础原理到实战应用
开发语言·qt·ui·qt designer·布局管理·参数传递·qt信号槽
珂朵莉MM41 分钟前
2024 睿抗机器人开发者大赛CAIP-编程技能赛-专科组(国赛)解题报告 | 珂学家
开发语言·人工智能·算法·leetcode·职场和发展·深度优先·图论
菥菥爱嘻嘻1 小时前
JS手写代码篇---手写 new 操作符
开发语言·javascript·原型模式
少了一只鹅1 小时前
字符函数和字符串函数
c语言·算法