#include "stdafx.h"
#include <windows.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
int g_nIndex = 0;
const int MAX_TIMES =70;
DWORD g_dwTimes [MAX_TIMES];
CRITICAL_SECTION g_CriticalSection;
HANDLE hStdout;
static DWORD WINAPI FirstThread(void* pv)
{
COORD pos;
int a=1;
int i=1;
pos.X=3;
BOOL fDone=FALSE;
while (!fDone)
{
::EnterCriticalSection(&g_CriticalSection);
if (g_nIndex >= MAX_TIMES)
fDone = TRUE;
else
{
a=a+5;
pos.Y = i;
SetConsoleCursorPosition (hStdout ,pos);
printf(" %d", a);
i++;
g_dwTimes [g_nIndex++]=::GetTickCount();
}
::LeaveCriticalSection(&g_CriticalSection);
}
return 0;
}
static DWORD WINAPI SecondThread(void* pv)
{
COORD pos;
pos.X=25;
int b=2;
int i=1;
BOOL fDone=FALSE;
while (!fDone)
{
::EnterCriticalSection(&g_CriticalSection);
if (g_nIndex >= MAX_TIMES)
fDone = TRUE;
else
{
b=b*2;
pos.Y = i;
SetConsoleCursorPosition (hStdout ,pos);
printf(" %d",b);
i++;
g_dwTimes [++g_nIndex-1]=::GetTickCount();
}
::LeaveCriticalSection(&g_CriticalSection);
Sleep(10);
}
return 0;
}
static DWORD WINAPI ThirdThread(void* pv)
{
COORD pos;
pos.X=45;
int c=3;
int i=1;
BOOL fDone=FALSE;
while (!fDone)
{
::EnterCriticalSection(&g_CriticalSection);
if (g_nIndex >= MAX_TIMES)
fDone = TRUE;
else
{
pos.Y = i;
SetConsoleCursorPosition (hStdout ,pos);
printf(" %d",c);
i++;
g_dwTimes [++g_nIndex-2]=::GetTickCount();
}
::LeaveCriticalSection(&g_CriticalSection);
}
return 0;
}
int main(int argc, char* argv[])
{
HANDLE hThreads[3];
hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
::InitializeCriticalSection(&g_CriticalSection);
DWORD dwThreadID;
hThreads[0]=::CreateThread(NULL, 0, FirstThread, &dwThreadID, 0, &dwThreadID);
hThreads[1]=::CreateThread(NULL, 0, SecondThread, &dwThreadID, 0, &dwThreadID);
hThreads[2]=::CreateThread(NULL, 0, ThirdThread, &dwThreadID, 0, &dwThreadID);
::WaitForMultipleObjects(3, hThreads, TRUE, INFINITE);
::CloseHandle(hThreads[0]);
::CloseHandle(hThreads[1]);
::CloseHandle(hThreads[2]);
:

eleteCriticalSection(&g_CriticalSection);
getch();
return 0;
}
Добавлено через 1 минуту
ну это с 3 потоками! посмотрите пожалуйста правильно или нет?