|
问题:
196 sem = rt sem create("sem", 1, RT IPC FLAG FIFO); 为什么这个信号量的值要设为1
为什么生产和消费,都有,
rt_sem_take();
rt_sem_release();
函数.
没看明白,怎么发一个信号,怎么接收一个信号
/* 生产者线程入口*/
135 static void thread entry(void* parameter)
136 {
137 rt bool t result;
138 rt uint8 t data buffer[BUFFER ITEM];
139
140 while (1)
141 {
142 /* 持有信号量*/
143 rt sem take(sem, RT WAITING FOREVER);
144 /* 从环buffer中获得数据*/
145 result = rb get(&working rb, &data buffer[0], BUFFER ITEM);
146 /* 释放信号量*/
147 rt sem release(sem);
148
149 if (result == RT TRUE)
150 {
151 /* 获取数据成功,打印数据*/
152 rt kprintf("%s\n", data buffer);
153 }
154
155 /* 做一个5 OS Tick的休眠*/
156 rt thread delay(5);
157 }
158 }
159
160 /* worker线程入口*/
161 static void worker entry(void* parameter)
162 {
163 rt bool t result;
164 rt uint32 t index, setchar;
165 rt uint8 t data buffer[BUFFER ITEM];
166
167 setchar = 0x21;
168 while (1)
169 {
170 /* 构造数据*/
171 for(index = 0; index < BUFFER ITEM; index++)
172 {
173 data buffer[index] = setchar;
174 if (++setchar == 0x7f)
175 setchar = 0x21;
176 }
177
178 /* 持有信号量*/
179 rt sem take(sem, RT WAITING FOREVER);
180 /* 把数据放到环形buffer中*/
181 result = rb put(&working rb, &data buffer[0], BUFFER ITEM);
182 /* 释放信号量*/
183 rt sem release(sem);
184
185 /* 放入成功,做一个10 OS Tick的休眠*/
186 rt thread delay(10);
187 }
188 }
189
190 int semaphore buffer worker init()
191 {
192 /* 初始化ring buffer */
193 rb init(&working rb, working buffer, BUFFER SIZE);
194
195 /* 创建信号量*/
196 sem = rt sem create("sem", 1, RT IPC FLAG FIFO);
197 if (sem == RT NULL)
198 {
199 tc stat(TC STAT END j TC STAT FAILED);
200 return 0;
201 }
202
203 /* 创建线程1 */
204 tid = rt thread create("thread",
205 thread entry, RT NULL, /* 线程入口是thread entry, 入口参数是RT NULL */
206 THREAD STACK SIZE, THREAD PRIORITY, THREAD TIMESLICE);
207 if (tid != RT NULL)
208 rt thread startup(tid);
209 else
210 tc stat(TC STAT END j TC STAT FAILED);
211
212 /* 创建线程2 */
213 worker = rt thread create("worker",
214 worker entry, RT NULL, /* 线程入口是worker entry, 入口参数是RT NULL */
215 THREAD STACK SIZE, THREAD PRIORITY, THREAD TIMESLICE);
216 if (worker != RT NULL)
217 rt thread startup(worker);
218 else
219 tc stat(TC STAT END j TC STAT FAILED);
220
221 return 0;
222 }
223 |
阿莫论坛20周年了!感谢大家的支持与爱护!!
曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……
|