Thread Synchronization

I have created a program for thread synchronization using thread and binary semaphore. The program prints ping pong in synchronized fashion. The code of the program is given below; The code is compiled in Linux using gcc.

#include<stdio.h>
#include<pthread.h>
#include<stdlib.h>
#include <semaphore.h>

sem_t mutex1;
sem_t mutex2;
void * testThread1(void *parg);
void * testThread2(void *parg);
int main(void){
pthread_t thread1,thread2;
int rc1,rc2;

sem_init(&mutex1, 0, 1);
sem_init(&mutex2, 0, 1);

//sem_wait(&mutex1);
sem_wait(&mutex2);

rc1 = pthread_create(&thread1,NULL,testThread1,NULL);
//sem_wait(&mutex2);
rc2 = pthread_create(&thread2,NULL,testThread2,NULL);

pthread_join(thread1, NULL);
pthread_join(thread2, NULL);

return 0;

}

void * testThread1(void *parg){
int count = 0;
//sem_post(&mutex2);
while(count <5){
sem_wait(&mutex1);
printf(“Ping \n”);
count = count + 1;
//sem_post(&mutex2);
sem_post(&mutex2);
}
}

void * testThread2(void *parg){
int count = 0;
//sem_post(&mutex1);
while(count<5){
sem_wait(&mutex2);
printf(“Pong \n”);
count = count + 1;
//sem_post(&mutex1);
sem_post(&mutex1);
}
}

Callback Timer

I have created a callback timer using windows API.  I have tested this program in VS studio 2010 as console application.The code of the program is as follow;

// testTimer.cpp : Defines the entry point for the console application.
//
#include<Windows.h>
#include<stdio.h>

#pragma comment(lib, “winmm.lib”)
#define MAX 5
#include <mmsystem.h>
void InitializeTimer();
void CALLBACK TimerFunction(UINT wTimerID, UINT msg,
DWORD dwUser, DWORD dw1, DWORD dw2);

int count = 1;

static MMRESULT m_id;
void stopTimer();

int main(void){
InitializeTimer();

getchar();
}

void CALLBACK TimerFunction(UINT wTimerID, UINT msg,
DWORD dwUser, DWORD dw1, DWORD dw2)
{
// This is used only to call MMTimerHandler

// Typically, this function is static member of CTimersDlg
printf(“TimerFunction is called :%d\n”,count);
if(count == MAX){
stopTimer();

count  = 1;
}

count++;

}

void InitializeTimer()
{
TIMECAPS tc;
int minRes = 5000;

timeGetDevCaps(&tc, sizeof(TIMECAPS));

timeBeginPeriod(minRes);

m_id = timeSetEvent(5000,minRes,TimerFunction,0,TIME_PERIODIC);
}

void stopTimer()
{
timeKillEvent(m_id);
timeEndPeriod (5000);

}