vxworks的线程及互斥锁

本文介绍了VxWorks 5.5开始支持的POSIX线程实时扩展,探讨了VxWorks中线程作为任务运行的特性,以及由于缺乏进程概念导致的数据直接访问。同时,详细阐述了线程的创建、等待、退出操作,并通过示例展示了互斥锁的使用,包括初始化、上锁、解锁和销毁等步骤,以确保线程安全。
摘要由CSDN通过智能技术生成

vxworks5.5开始支持POSIX线程实时扩展

vxworks本质只包括系统和任务两个概念,线程以任务形式实现。线程不属于任何进程,只属于整个系统,因而pthread在整个系统范围内竞争。

<1>vxworks任务在同一实地址空间运行,无任何保护机制,任何任务可以直接访问其他任务数据,POSIX中进程共享相关函数未实现。

<2>vxworks无用户和组概念,无进程概念。


创建 pthread_create

等待 ptherad_join

退出 pthread_exit

#include<pthread.h> 
#include<stdio.h> 
#include<errno.h>
#include<unistd.h>
/*线程1*/
void thread1() {
	int i=0;

	while (1) {
		printf("thread1:%d\n", i);
		if (i>3)
			pthread_exit(0);
		i++;
		sleep(1);
	}
}

/*线程2*/
void thread2() {
	int i=0;

	while (1) {
		printf("thread2:%d\n", i);
		if (i>5)
			pthread_exit(0);
		i++;
		sleep(1);
	}
}
int taskDemo() {
	pthread_t t1, t2;
	/*创建线程*/
	pthread_create(&t1, NULL, (void *)thread1, NULL);
	pthread_create(&t2, NULL, (void *)thread2, NULL);
	/*等待线程退出*/
	pthread_join(t1, NULL)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值