pxjoke

Untitled

Mar 23rd, 2015
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.85 KB | None | 0 0
  1. // lab5.cpp: определяет точку входа для консольного приложения.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <windows.h>
  6. #include <stdio.h>
  7. #include <string>
  8. #include <conio.h>
  9. #include <iostream>
  10. #define THREADCOUNT 4
  11. HANDLE hNextEvent;
  12. HANDLE ghThreads[THREADCOUNT];
  13. DWORD WINAPI ThreadProc(LPVOID);
  14.  
  15. typedef struct MyData {
  16.     int val1;
  17.  
  18. } MYDATA, *PMYDATA;
  19.  
  20. int _tmain(int argc, _TCHAR* argv [])
  21. {
  22.     int i;
  23.     DWORD dwThreadID;
  24.     PMYDATA pDataArray[THREADCOUNT];
  25.     printf("Thread MAIN >> STARTED\n");
  26.     // создать объекты для синхронизации hNextEvent = CreateEvent(…);
  27.     hNextEvent = CreateEvent(
  28.         NULL,               // default security attributes
  29.         TRUE,               // manual-reset event
  30.         FALSE,              // initial state is nonsignaled
  31.         TEXT("NextEvent")  // object name
  32.         );
  33.     if (hNextEvent == NULL)
  34.     {
  35.         printf("CreateEvent failed (%d)\n", GetLastError());
  36.         return -1;
  37.     }
  38.     if (!SetEvent(hNextEvent))
  39.     {
  40.         printf("SetEvent failed (%d)\n", GetLastError());
  41.         return -1;
  42.     }
  43.     for (i = 0; i < THREADCOUNT; i++)
  44.     {
  45.         // TODO: More complex scenarios may require use of a parameter
  46.         //   to the thread procedure, such as an event per thread to  
  47.         //   be used for synchronization.
  48.         pDataArray[i] = (PMYDATA) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
  49.             sizeof(MYDATA));
  50.  
  51.         if (pDataArray[i] == NULL)
  52.         {
  53.             // If the array allocation fails, the system is out of memory
  54.             // so there is no point in trying to print an error message.
  55.             // Just terminate execution.
  56.             ExitProcess(2);
  57.         }
  58.  
  59.         // Generate unique data for each thread to work with.
  60.  
  61.         pDataArray[i]->val1 = i;
  62.  
  63.         ghThreads[i] = CreateThread(
  64.             NULL,              // default security
  65.             0,                 // default stack size
  66.             ThreadProc,        // name of the thread function
  67.             pDataArray[i],              // no thread parameters
  68.             0,                 // default startup flags
  69.             &dwThreadID);
  70.  
  71.         if (ghThreads[i] == NULL)
  72.         {
  73.             printf("CreateThread failed (%d)\n", GetLastError());
  74.             return -1;
  75.         }
  76.     }
  77.     system("pause");
  78.     return 0;
  79. }
  80.  
  81.  
  82.  
  83. DWORD WINAPI ThreadProc(LPVOID lpParameter)
  84. {
  85.     PMYDATA pDataArray;
  86.     int iThreadId;
  87.     char *str = "";
  88.  
  89.     pDataArray = (PMYDATA) lpParameter;
  90.     iThreadId = pDataArray->val1;
  91.     printf("Thread %d >> STARTED\n", iThreadId);
  92.     while (true)
  93.     {
  94.         WaitForSingleObject(hNextEvent, INFINITE);
  95.         while (true)
  96.         {
  97.             gets(str);
  98.             if (str == "exit"){
  99.                 printf("Thread %d >> EXIT\n", iThreadId);
  100.                 if (!SetEvent(hNextEvent))
  101.                 {
  102.                     printf("Thread %d >> SetEvent FAILD (%d)\n", iThreadId, GetLastError());
  103.                     return -1;
  104.                 }
  105.                 return 0;
  106.             }
  107.  
  108.             if (str == "next"){
  109.                 printf("Thread %d >> STOPED\n", iThreadId);
  110.                 break;
  111.             }
  112.  
  113.  
  114.             printf("Thread %d >> Your input is: /""%s/""\n", iThreadId, str);
  115.         }
  116.     }
  117.  
  118.  
  119.     return 0;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment