我知道4常见的技术在并行使用规则(可能有多个),而在高水平共享内存,所以想分享。 随时分享你的例子!
警告:共享内存可以损害你的健康! 确保您理解到底发生了什么,以避免潜在的奇怪和难以解决运行时错误!
这是第一种方法,使用不安全的指针(引入工具13)。 这是一个很好的方式分享全球使用(n不安全)的指针。
优点:很熟悉C使用,速度快(没有边界检查)
缺点:共享内存(以外的正常和指针类型风险)额外的语法来声明不安全的部分,转发虽然延长它相当明确的其他读者,(可能)的可能。
代码:
#include <xs1.h>
#include <stdio.h>
#include <timer.h>
unsigned g_global = 0;
void task1(void){
volatile unsigned * unsafe glob_ptr;
unsafe {
glob_ptr = &g_global;
}
unsafe{
printf("Ptr set to %d\n", *glob_ptr);
while(*glob_ptr == 0);
printf("Ptr set to %d\n", *glob_ptr);
}
}
void task2(void){
volatile unsigned * unsafe glob_ptr;
unsafe {
glob_ptr = &g_global;
}
delay_microseconds(1);
unsafe{
*glob_ptr = 1234;
}
}
int main(void){
par{
task1();
task2();
}
return 0;
}