1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
// lab5.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <process.h>
using namespace std;
int ids [5] = { 100, 200, 300, 400, 500 };
int delays [5] = { 500, 600, 700, 800, 900 };
struct ThreadArgs
{
int id;
int delay;
};
unsigned __stdcall MyThread(void *data)
{
for(int y = 0; y < 10; ++y)
{
ThreadArgs *args = (ThreadArgs *) data;
cout << args->id << endl;
Sleep(args->delay);
}
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
int n = 0;
if(n < 10)
{
ThreadArgs args;
args.id = ids[0];
args.delay = delays[0];
HANDLE hThread = (HANDLE) _beginthreadex(NULL, 0, MyThread, (void *) &args, 0, NULL);
WaitForSingleObject(hThread, INFINITE);
}
if(n < 10)
{
ThreadArgs args;
args.id = ids[1];
args.delay = delays[1];
HANDLE hThread = (HANDLE) _beginthreadex(NULL, 0, MyThread, (void *) &args, 0, NULL);
WaitForSingleObject(hThread, INFINITE);
}
if(n < 10)
{
ThreadArgs args;
args.id = ids[2];
args.delay = delays[2];
HANDLE hThread1 = (HANDLE) _beginthreadex(NULL, 0, MyThread, (void *) &args, 0, NULL);
WaitForSingleObject(hThread1, INFINITE);
}
if(n < 10)
{
ThreadArgs args;
args.id = ids[3];
args.delay = delays[3];
HANDLE hThread = (HANDLE) _beginthreadex(NULL, 0, MyThread, (void *) &args, 0, NULL);
WaitForSingleObject(hThread, INFINITE);
}
if(n < 10)
{
ThreadArgs args;
args.id = ids[4];
args.delay = delays[4];
HANDLE hThread = (HANDLE) _beginthreadex(NULL, 0, MyThread, (void *) &args, 0, NULL);
WaitForSingleObject(hThread, INFINITE);
}
if(n < 10)
{
ThreadArgs args;
args.id = ids[5];
args.delay = delays[5];
HANDLE hThread = (HANDLE) _beginthreadex(NULL, 0, MyThread, (void *) &args, 0, NULL);
WaitForSingleObject(hThread, INFINITE);
}
return 0;
}
|