📄 pthread.c
字号:
#include "message.h"#include "led.h"#include "mwd.h"#include "ad.h"#include "sever.h"void * pwm(void *data);void init(struct prodcons * b){ pthread_mutex_init(&b->lock, NULL); pthread_cond_init(&b->notempty, NULL); pthread_cond_init(&b->notfull, NULL); b->readpos = 0; b->writepos = 0;}/*--------------------------------------------------------*//* Store an integer in the buffer */void sendmessage(struct prodcons * b, MSGE *m){ pthread_mutex_lock(&b->lock); /* Wait until buffer is not full */ while ((b->writepos + 1) % BUFFER_SIZE == b->readpos) { // printf("wait for not full\n"); pthread_cond_wait(&b->notfull, &b->lock); } /* Write the data and advance write pointer */ b->msg[b->writepos] = *m; b->writepos++; if (b->writepos >= BUFFER_SIZE) b->writepos = 0; /* Signal that the buffer is now not empty */ pthread_cond_signal(&b->notempty); pthread_mutex_unlock(&b->lock);}/*--------------------------------------------------------*//* Read and remove an integer from the buffer */int receivemessage(struct prodcons * b,MSGE *m){ pthread_mutex_lock(&b->lock); /* Wait until buffer is not empty */ while (b->writepos == b->readpos) { //printf("wait for not empty\n"); pthread_cond_wait(&b->notempty, &b->lock); } /* Read the data and advance read pointer */ *m = b->msg[b->readpos]; b->readpos++; if (b->readpos >= BUFFER_SIZE) b->readpos = 0; /* Signal that the buffer is now not full */ pthread_cond_signal(&b->notfull); pthread_mutex_unlock(&b->lock); return 0;}///////////////////////////////////////////////////////////////////*--------------------------------------------------------*/int main(void){ pthread_t mwd1, getmessage1,ad_com,socket; void * retval; init_ADdevice(); usleep(1); init(&mbuffer); pthread_create(&mwd1, NULL, mwd, 0); pthread_create(&getmessage1, NULL, getmessage, 0); pthread_create(&socket, NULL,sever, 0);// pthread_create(&pwm1,NULL,pwm,0);// pthread_create(&key1,NULL,keyboard,0); pthread_create(&ad_com, NULL, GetADresult, 0); /* Wait until producer and consumer finish. */ pthread_join(ad_com, &retval); pthread_join(mwd1, &retval); pthread_join(getmessage1, &retval); pthread_join(socket, &retval);// pthread_join(pwm1,&retval);// pthread_join(key1,&retval); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -