• Source
    1. #include <stdio.h>
    2.  
    3. typedef enum{
    4. eLOCK = 0,
    5. eUNLOCK
    6. }teStatus;
    7.  
    8. int mutex(teStatus status )
    9. {
    10. static int lock = 0;
    11.  
    12. if(status == eLOCK )
    13. {
    14. if(!lock)
    15. {
    16. lock = 1;
    17. return 1;
    18. }
    19. else
    20. {
    21. return 0;
    22. }
    23. }
    24.  
    25. if(status == eUNLOCK)
    26. {
    27. if(lock)
    28. {
    29. lock = 0;
    30. return 1;
    31. }
    32. else
    33. {
    34. return 0;
    35. }
    36. }
    37. }
    38.  
    39. void function1()
    40. {
    41. while(!mutex(eLOCK));
    42. // do some task here.
    43. mutex(eUNLOCK);
    44. }
    45.  
    46. void function2()
    47. {
    48. while(!mutex(eLOCK));
    49. // do some task here.
    50. mutex(eUNLOCK);
    51. }
    52.  
    53.  
    54. int main(void) {
    55. // your code goes here
    56. return 0;
    57. }
    58.