Umut Aradağ / Mbed 2 deprecated Semaphores

Dependencies:   mbed

main.cpp

Committer:
umutaradag
Date:
2011-08-04
Revision:
2:ac3746d21033
Parent:
1:5c7d8d760ece

File content as of revision 2:ac3746d21033:

#include "mbed.h"
#include "semaphore.h"

Ticker ticker;
semaphore sharedVariableSem;

volatile int sharedVariable;

#define SEM_ENABLE 1

void incrementer()
{
#if SEM_ENABLE==1
    if(semPend(&sharedVariableSem, 5) == SEM_ERR)
        return;
#endif

    sharedVariable++;

#if SEM_ENABLE==1
    semPost(&sharedVariableSem);
#endif
}

int main() 
{
    sharedVariable = 0;
    semInit(&sharedVariableSem, 1);
    ticker.attach(&incrementer, 0.25);

    while(1)
    {
#if SEM_ENABLE==1
    semPend(&sharedVariableSem, 10000);
#endif
    
    printf("\nfirst val = %d\n",sharedVariable);
    sharedVariable++;
    wait(1);
    printf("second val = %d\n",sharedVariable);
    
#if SEM_ENABLE==1
    semPost(&sharedVariableSem);
#endif

    wait(1);
    }
}