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);
pthread_join(t2, NULL);
return 0;
}
运行
thread1:0
thread2:0

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

被折叠的 条评论
为什么被折叠?



