Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <tchar.h>
- #include <windows.h>
- #define S 8
- #define M 2
- #define TIME 2
- #define _DEBUG_
- typedef struct{
- int field[S];
- int p;
- } sField;
- int id[M];
- sField buffer[M];
- int buf_size;
- sField work[M];
- HANDLE hMutex,
- hBufferFull,
- hMutexOut;
- HANDLE Processes[M]; // олдскульая массив потоков
- bool Finish = false;
- int N = 0; // количество найденных комбинаций
- void print(int *a){
- WaitForSingleObject(hMutexOut, INFINITE); // входим к критическую секцию
- #ifdef _DEBUG_
- printf("Id%d: ", GetCurrentThreadId());
- for (int i = 0; i< S; i++) printf("%d", a[i]);
- printf("\n");
- #endif
- N++;
- ReleaseSemaphore(hMutexOut, 1, NULL);
- }
- int test(int p, int *a){
- for (int i = 0; i < p; i++){
- if (a[i] == a[p]) return 0;
- if (a[i] - a[p] == p - i || a[p] - a[i] == p - i) return 0;
- }
- return 1;
- }
- void f(int p, int *a){
- int contin;
- if (p == S)
- print(a);
- else {
- for (int i = 0; i < S; i++){
- a[p] = i;
- if (test(p, a)){
- contin = 1;
- if (p<2)
- if (WaitForSingleObject(hMutex, 0) == WAIT_OBJECT_0){
- // если не нужно долго ждать входа в критическую секцию
- if (buf_size < M){// и если есть место в буфере, отдаем задание кому-нибудь
- for (int i = 0; i < S; i++)
- buffer[buf_size].field[i] = a[i];
- buffer[buf_size].p = p + 1;
- buf_size++;
- ReleaseSemaphore(hBufferFull, 1, NULL);
- #ifdef _DEBUG_
- printf("Id %d out: %d, %d%d%d%d%d%d%d%d the [%d]-th buf\n", GetCurrentThreadId(), p + 1, a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], buf_size - 1);
- #endif
- contin = 0;
- }
- ReleaseSemaphore(hMutex, 1, NULL);
- }
- if (contin)
- f(p + 1, a);
- }
- }
- }
- if (p == 0)
- Finish = true;
- }
- DWORD WINAPI Run(LPVOID lpParam){
- int pnum = *((int*)lpParam); // номер процесса
- while (Finish == false || buf_size > 0){
- if (WaitForSingleObject(hBufferFull, TIME) == WAIT_OBJECT_0){
- // дожидаемся появления в буфере (нового) задания
- WaitForSingleObject(hMutex, INFINITE); // входим в критическую секцию
- // копируем задание из буфера в свою рабочую область
- buf_size--;
- for (int i = 0; i<S; i++)
- work[pnum].field[i] = buffer[buf_size].field[i];
- work[pnum].p = buffer[buf_size].p;
- #ifdef _DEBUG_
- printf("Id %d get %d, %d%d%d%d%d%d%d%d the [%d]-th buf\n", GetCurrentThreadId(), work[pnum].p, work[pnum].field[0], work[pnum].field[1], work[pnum].field[2], work[pnum].field[3], work[pnum].field[4], work[pnum].field[5], work[pnum].field[6], work[pnum].field[7], buf_size);
- #endif
- ReleaseSemaphore(hMutex, 1, NULL);// выходим из критической секции
- // запускаем решение задачи с этого места
- f(work[pnum].p, work[pnum].field);
- }
- }
- return 0;
- }
- int main(){
- hMutex = CreateSemaphore(NULL, 1, 1, NULL);
- hMutexOut = CreateSemaphore(NULL, 1, 1, NULL);
- hBufferFull = CreateSemaphore(NULL, 0, M, NULL);
- buffer[0].p = 0;
- buf_size = 1;
- for (int i = 0; i < M; i++)
- id[i] = i;
- ReleaseSemaphore(hBufferFull, 1, NULL);
- for (int i = 0; i < M; i++)
- Processes[i] = CreateThread(NULL, 0, Run, &id[i], 0, NULL); // вроде мы по другому теперь создаем трэды
- WaitForMultipleObjects(M, Processes, TRUE, INFINITE);
- printf("S = %d\nDetected combinations: %d\nThreads: %d\n", S, N, M);
- fgetc(stdin); // задержку мы по-дргому делаем
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment