sccx
March 28th, 2006, 10:38 PM
Like to get 123412341234... as output fastest and safest way possible. How to do it with createsemaphore/or any other fastest way using two threads to obtain 12121212... as output?
#include <afxmt.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <process.h>
void main(void);
void Thread1();
void Thread2();
void Thread3();
void Thread4();
static HANDLE mutexHandle = NULL;
void main(void)
{
HANDLE thread1;
HANDLE thread2;
HANDLE thread3;
HANDLE thread4;
mutexHandle = CreateMutex(NULL, false, NULL);
thread1 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) Thread1, NULL, 0, NULL);
Sleep(100);
thread2 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) Thread2, NULL, 0, NULL);
Sleep(100);
thread3 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) Thread3, NULL, 0, NULL);
Sleep(100);
thread4 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) Thread4, NULL, 0, NULL);
Sleep(100);
while(1)
{}
}
void Thread1()
{
printf("1");
WaitForSingleObject(mutexHandle, INFINITE);
ReleaseMutex(mutexHandle);
Thread1();
}
void Thread2()
{
printf("2");
WaitForSingleObject(mutexHandle, INFINITE);
ReleaseMutex(mutexHandle);
Thread2();
}
void Thread3()
{
printf("3");
WaitForSingleObject(mutexHandle, INFINITE);
ReleaseMutex(mutexHandle);
Thread3();
}
void Thread4()
{
printf("4");
WaitForSingleObject(mutexHandle, INFINITE);
ReleaseMutex(mutexHandle);
Thread4();
}
#include <afxmt.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <process.h>
void main(void);
void Thread1();
void Thread2();
void Thread3();
void Thread4();
static HANDLE mutexHandle = NULL;
void main(void)
{
HANDLE thread1;
HANDLE thread2;
HANDLE thread3;
HANDLE thread4;
mutexHandle = CreateMutex(NULL, false, NULL);
thread1 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) Thread1, NULL, 0, NULL);
Sleep(100);
thread2 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) Thread2, NULL, 0, NULL);
Sleep(100);
thread3 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) Thread3, NULL, 0, NULL);
Sleep(100);
thread4 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) Thread4, NULL, 0, NULL);
Sleep(100);
while(1)
{}
}
void Thread1()
{
printf("1");
WaitForSingleObject(mutexHandle, INFINITE);
ReleaseMutex(mutexHandle);
Thread1();
}
void Thread2()
{
printf("2");
WaitForSingleObject(mutexHandle, INFINITE);
ReleaseMutex(mutexHandle);
Thread2();
}
void Thread3()
{
printf("3");
WaitForSingleObject(mutexHandle, INFINITE);
ReleaseMutex(mutexHandle);
Thread3();
}
void Thread4()
{
printf("4");
WaitForSingleObject(mutexHandle, INFINITE);
ReleaseMutex(mutexHandle);
Thread4();
}