- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 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
- 85
- 86
- 87
- 88
- 89
- 90
- 91
//main.cpp:
#define THREAD_COUNT 2
#include "winparallel.h"
main1()
{
lock(1);
int i=0; while(i++<10)
printf("1: %d\n", i);
unlock(1);
}
main2()
{
lock(1);
int i=0; while(i++<10)
printf("2: %d\n", i);
unlock(1);
}
main3(){};
main4(){};
main5(){};
main6(){};
main7(){};
main8(){};
//=====================================================================================
//winparallels.h:
#include <windows.h>
#define _MAX_CORES 8
LPVOID _void;
#define main1() DWORD WINAPI _main1(LPVOID lpParam)
#define main2() DWORD WINAPI _main2(LPVOID lpParam)
#define main3() DWORD WINAPI _main3(LPVOID lpParam)
#define main4() DWORD WINAPI _main4(LPVOID lpParam)
#define main5() DWORD WINAPI _main5(LPVOID lpParam)
#define main6() DWORD WINAPI _main6(LPVOID lpParam)
#define main7() DWORD WINAPI _main7(LPVOID lpParam)
#define main8() DWORD WINAPI _main8(LPVOID lpParam)
DWORD WINAPI _main1(LPVOID);
DWORD WINAPI _main2(LPVOID);
DWORD WINAPI _main3(LPVOID);
DWORD WINAPI _main4(LPVOID);
DWORD WINAPI _main5(LPVOID);
DWORD WINAPI _main6(LPVOID);
DWORD WINAPI _main7(LPVOID);
DWORD WINAPI _main8(LPVOID);
HANDLE _locks[_MAX_CORES];
int _argc; char **_argv;
void lock(int n) { DWORD res = WaitForSingleObject(_locks[n], INFINITE); }
void unlock(int n) { ReleaseMutex(_locks[n]); }
int main(int argc,char **argv) {
HANDLE threads[_MAX_CORES];
DWORD id;
int THREADS = THREAD_COUNT;
if (THREADS<=0) {
SYSTEM_INFO sysinfo;
GetSystemInfo(&sysinfo);
THREADS = sysinfo.dwNumberOfProcessors;
if (THREADS>_MAX_CORES) THREADS = _MAX_CORES;
}
_argc = argc; _argv = argv;
_locks[0] = CreateMutex(NULL,FALSE,NULL);
_locks[1] = CreateMutex(NULL,FALSE,NULL);
_locks[2] = CreateMutex(NULL,FALSE,NULL);
_locks[3] = CreateMutex(NULL,FALSE,NULL);
_locks[4] = CreateMutex(NULL,FALSE,NULL);
_locks[5] = CreateMutex(NULL,FALSE,NULL);
_locks[6] = CreateMutex(NULL,FALSE,NULL);
_locks[7] = CreateMutex(NULL,FALSE,NULL);
threads[0]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)_main1,NULL,0,&id);
if (THREADS>=2)
threads[1]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)_main2,NULL,0,&id);
if (THREADS>=3)
threads[2]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)_main3,NULL,0,&id);
if (THREADS>=4)
threads[3]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)_main4,NULL,0,&id);
if (THREADS>=5)
threads[4]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)_main5,NULL,0,&id);
if (THREADS>=6)
threads[5]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)_main6,NULL,0,&id);
if (THREADS>=7)
threads[6]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)_main7,NULL,0,&id);
if (THREADS>=8)
threads[7]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)_main8,NULL,0,&id);
WaitForMultipleObjects(THREADS, threads, TRUE, INFINITE);
return 0;
}