À̺¥Æ® °´Ã¼
ÃÑ ÆäÀÌÁö ¼ö : 3224

Àüü ÇÔ¼ö/¿ë¾î»çÀü
Facebook Joinc ±×·ì   Joinc QA »çÀÌÆ®



joinc´Â Firefox¿Í chrome¿¡¼­ Å×½ºÆ® Çß½À´Ï´Ù. IE¿¡¼­´Â Å×À̺íÀÌ ±úÁö°Å³ª À̹ÌÁö°¡ º¸ÀÌÁö ¾ÊÀ» ¼ö ÀÖ½À´Ï´Ù. ƯÈ÷ ±¸±Û DocsÀ̹ÌÁöÀÇ °æ¿ì ¿¢¹Úó¸®µÉ ¼ö ÀÖ½À´Ï´Ù.

À©µµ ³×Æ®¿öÅ© ÇÁ·Î±×·¡¹ÖÀ» ÇÏ´Ù º¸´Ï À̺¥Æ®¸¦ ´Ù·ç¾î¾ß ÇÏ´Â °æ¿ì°¡ »ý°å´Ù. ±×·¡¼­ À̺¥Æ® °´Ã¼¿¡ ´ëÇØ¼­ °øºÎÇØº¼·Á°í À§Å°ÆäÀÌÁö¸¦ ¸¸µé¾ú´Ù.

À̺¥Æ® °´Ã¼¿Í ½Ã±×³Î

À¯´Ð½º¿Í ¸®´ª½º´Â ½Ã±×³ÎÀ» ÀÌ¿ëÇØ¼­ »óÅ Á¤º¸¸¦ Àü´ÞÇÏ°í »ç°ÇÀ» µ¿±âÈ­ ½Ãų ¼ö ÀÖ´Ù. À©µµ´Â ½Ã±×³Î ½Ã½ºÅÛÀ» Á¦°øÇÏÁö ¾Ê´Â´Ù. ´ë½Å À̺¥Æ® °´Ã¼¸¦ ÀÌ¿ëÇØ¼­ µ¿±âÈ­¸¦ ´Þ¼ºÇÑ´Ù.

À̺¥Æ® °´Ã¼´Â µ¶¸³ÀûÀÎ °´Ã¼·Î½á ÀÛµ¿ÇÑ´Ù. Áï °ü½ÉÀÖ´Â °´Ã¼¿Í À̺¥Æ® °´Ã¼¸¦ ¼­·Î ¿¬°áÇÑ´Ù. ¿¹¸¦ µé¾î ¼ÒÄÏ¿¡ Àбâ/¾²±â À̺¥Æ®¸¦ °ü¸®ÇÏ±æ ¿øÇÑ´Ù¸é, À̺¥Æ® °´Ã¼¸¦ ¸¸µé¾î¼­ ¼ÒÄÏÀÇ Àбâ/¾²±â À̺¥Æ®¿Í ¿¬°áÇÑ´Ù. ÀÌÁ¦ ¼ÒÄÏ¿¡ Àбâ/¾²±â µ¥ÀÌÅͰ¡ ÁغñµÇ¸é, Ä¿³ÎÀº À̺¥Æ® °´Ã¼¸¦ "½ÅÈ£ »óÅÂ"·Î ¸¸µç´Ù. ½ÅÈ£ »óÅÂÀÇ À̺¥Æ® °´Ã¼´Â wait ÇÔ¼ö·Î °Ë»çÇÒ ¼ö ÀÖ´Ù.

SetEvent ÇÔ¼ö·Î À̺¥Æ® °´Ã¼¸¦ ½ÅÈ£ »óÅ·Π¸¸µé ¼ö ÀÖ´Ù.
  1. ºñ ½ÅÈ£ »óÅ (non signaled stat) : À̺¥Æ®°¡ ¹ß»ýÇÏÁö ¾ÊÀº »óÅ·ΠÀ̺¥Æ® °´Ã¼¸¦ ¸¸µé¸é ºñ ½ÅÈ£ »óÅ¿¡ ³õÀδÙ.
  2. ½ÅÈ£ »óÅ (signaled stat) : À̺¥Æ® °´Ã¼°¡ ±â´Ù¸®´Â À̺¥Æ®°¡ ¹ß»ýÇÑ »óÅ´Ù. ½ÅÈ£ »óÅ¿¡ ³õÀ̸é wait ÇÔ¼ö°¡ ¹ÝȯµÇ°í, À̺¥Æ® °´Ã¼¸¦ °Ë»çÇØ¼­ ¾î¶² °´Ã¼¿¡ ¾î¶² À̺¥Æ®°¡ ¹ß»ýÇß´ÂÁö¸¦ °Ë»çÇÒ ¼ö ÀÖ´Ù.

À̺¥Æ® °´Ã¼´Â µÎ °¡Áö Á¾·ù°¡ ÀÖ´Ù.
  • Manual reset event : ½ÅÈ£ »óÅ¿¡ ³õÀÎ À̺¥Æ® °´Ã¼¸¦ ResetEvent ÇÔ¼ö¸¦ È£ÃâÇØ¼­ ºñ ½ÅÈ£ »óÅ·Π¸¸µé¾î¾ß ÇÏ´Â À̺¥Æ® °´Ã¼.
  • Auto reset Event : ½ÅÈ£ »óÅ¿¡ ³õÀÎ À̺¥Æ® °´Ã¼¸¦ ÀÚµ¿À¸·Î ºñ ½ÅÈ£ »óÅ·Π¼³Á¤ÇÑ´Ù.

À̺¥Æ® °´Ã¼ÀÇ ÀÌ¿ë

À̺¥Æ® °´Ã¼´Â CreateEventÇÔ¼ö·Î ¸¸µé ¼ö ÀÖ´Ù. ÀÌ ÇÔ¼ö·Î ¸¸µé¾îÁø À̺¥Æ® °´Ã¼´Â manual reset À̺¥Æ® °´Ã¼·Î ºñ ½ÅÈ£ »óÅ¿¡ ³õÀδÙ.

À̺¥Æ® °´Ã¼ÀÇ ½ÅÈ£ »óÅ´ WaitForSingleObjectÇÔ¼ö·Î ±â´Ù¸± ¼ö ÀÖ´Ù.

#include <windows.h> 
#include <stdio.h> 
 
#define THREADCOUNT 4  
 
HANDLE ghWriteEvent;  
HANDLE ghThreads[THREADCOUNT]; 
 
DWORD WINAPI ThreadProc(LPVOID); 
 
void CreateEventsAndThreads(void)  
{ 
    int i;  
    DWORD dwThreadID;  
 
    // Create a manual-reset event object. The write thread sets this 
    // object to the nonsignaled state when it finishes writing to a  
    // shared buffer.  
 
    ghWriteEvent = CreateEvent(  
        NULL,               // default security attributes 
        TRUE,               // manual-reset event 
        FALSE,              // initial state is nonsignaled 
        TEXT("WriteEvent")  // object name 
        );  
 
    if (ghWriteEvent == NULL)  
    {  
        printf("CreateEvent failed (%d)\n", GetLastError()); 
        return; 
    } 
 
    // Create multiple threads to read from the buffer. 
 
    for(i = 0; i < THREADCOUNT; i++)  
    { 
        // TODO: More complex scenarios may require use of a parameter 
        //   to the thread procedure, such as an event per thread to   
        //   be used for synchronization. 
        ghThreads[i] = CreateThread( 
            NULL,              // default security 
            0,                 // default stack size 
            ThreadProc,        // name of the thread function 
            NULL,              // no thread parameters 
            0,                 // default startup flags 
            &dwThreadID);  
 
        if (ghThreads[i] == NULL)  
        { 
            printf("CreateThread failed (%d)\n", GetLastError()); 
            return; 
        } 
    } 
} 
 
void WriteToBuffer(VOID)  
{ 
    // TODO: Write to the shared buffer. 
     
    printf("Main thread writing to the shared buffer...\n"); 
 
    // Set ghWriteEvent to signaled 
 
    if (! SetEvent(ghWriteEvent) )  
    { 
        printf("SetEvent failed (%d)\n", GetLastError()); 
        return; 
    } 
} 
 
void CloseEvents() 
{ 
    // Close all event handles (currently, only one global handle). 
     
    CloseHandle(ghWriteEvent); 
} 
 
void main() 
{ 
    DWORD dwWaitResult; 
 
    // TODO: Create the shared buffer 
 
    // Create events and THREADCOUNT threads to read from the buffer 
 
    CreateEventsAndThreads(); 
 
    // At this point, the reader threads have started and are most 
    // likely waiting for the global event to be signaled. However,  
    // it is safe to write to the buffer because the event is a  
    // manual-reset event. 
     
    WriteToBuffer(); 
 
    printf("Main thread waiting for threads to exit...\n"); 
 
    // The handle for each thread is signaled when the thread is 
    // terminated. 
    dwWaitResult = WaitForMultipleObjects( 
        THREADCOUNT,   // number of handles in array 
        ghThreads,     // array of thread handles 
        TRUE,          // wait until all are signaled 
        INFINITE); 
 
    switch (dwWaitResult)  
    { 
        // All thread objects were signaled 
        case WAIT_OBJECT_0:  
            printf("All threads ended, cleaning up for application exit...\n"); 
            break; 
 
        // An error occurred 
        default:  
            printf("WaitForMultipleObjects failed (%d)\n", GetLastError()); 
            return; 
    }  
             
    // Close the events to clean up 
 
    CloseEvents(); 
} 
 
DWORD WINAPI ThreadProc(LPVOID lpParam)  
{ 
    DWORD dwWaitResult; 
 
    printf("Thread %d waiting for write event...\n", GetCurrentThreadId()); 
     
    dwWaitResult = WaitForSingleObject(  
        ghWriteEvent, // event handle 
        INFINITE);    // indefinite wait 
 
    switch (dwWaitResult)  
    { 
        // Event object was signaled 
        case WAIT_OBJECT_0:  
            // 
            // TODO: Read from the shared buffer 
            // 
            printf("Thread %d reading from buffer\n",  
                   GetCurrentThreadId()); 
            break;  
 
        // An error occurred 
        default:  
            printf("Wait error (%d)\n", GetLastError());  
            return 0;  
    } 
 
    // Now that we are done reading the buffer, we could use another 
    // event to signal that this thread is no longer reading. This 
    // example simply uses the thread handle for synchronization (the 
    // handle is signaled when the thread terminates.) 
 
    printf("Thread %d exiting\n", GetCurrentThreadId()); 
    return 1; 
} 
 
EmailÀ» ±âÀÔÇϸé, ´ñ±ÛÀÌ ¸ÞÀÏ·Î Àü´ÞµË´Ï´Ù.