::: °­ÁÂ/¼Ò½º/¹®¼­ :::

°­ÁÂ/¼Ò½º/¹®¼­ ¼º°Ý¿¡ ¸ÂÁö ¾Ê´Â ±¤°í,ºñ¹æ,Áú¹®ÀÇ ±ÛÀº Áï½Ã »èÁ¦Çϸç
³»¿ëÀ» º¹»çÇÏ¿© »ç¿ëÇÒ °æ¿ì ¹Ýµå½Ã ÀÌ°÷(http://www.howto.pe.kr)À» Ãâó·Î ¸í½ÃÇÏ¿© ÁÖ¼¼¿ä


Category

  ±è¿µ´ë(2003-03-17 02:10:27, Hit : 5739, Vote : 1477
 [Thread] pthread_muxtexcond() function

/***************************************************
*--------------------------------------------------
*   Creat  : 2001. 04. 10  (programed by Cori-Young )
*   Site: http://www.byoneself.co.kr
*   Update :
*--------------------------------------------------
*   Compile : cc -o xxx xxx.c -lpthread -lrt
*--------------------------------------------------
* Machine hardware:   sun4u
* OS version:         5.7
* Processor type:     sparc
* Hardware:           SUNW,Ultra-60
***************************************************/


#include <pthread.h>
#include <stdio.h>

void* compute_thread(void*);

pthread_mutex_t my_sync;
pthread_cond_t rx;

int word_count = 0;


main() {

        pthread_t tid;        
        pthread_attr_t attr;
        
        char hello[] {"Hello, "};
        char thread[] {"thread"};
                
        /* Initialize the thread attributes,   locks and condition variables */
        pthread_attr_init(&attr);
                
         /* Initialize the mutex (default attributes) */
         pthread_mutex_init(&my_sync,NULL);
        
         /* Initialize the condition variable (default attr) */
        pthread_cond_init(&rx,NULL);
                
        pthread_create(&tid, &attr, compute_thread, thread);
        /* We use the word_count to coordinate the threads */
        
        pthread_mutex_lock(&my_sync);
        printf(hello);
        word_count++;
        
        pthread_cond_signal(&rx);
        pthread_mutex_unlock(&my_sync);
        
        /* Lock the mutex again to wait for thread to finish */
        pthread_mutex_lock(&my_sync);
        
        while (word_count != 2)
                pthread_cond_wait(&rx,&my_sync);
        
        printf("n");
        pthread_mutex_unlock(&my_sync);
        exit(0);
}

void* compute_thread(void* dummy) {

         /* wait until its our turn */
        pthread_mutex_lock(&my_sync);
        
        while (word_count != 1)
            pthread_cond_wait(&rx,&my_sync);
        
        printf(dummy);
                
        /* set the predicate and signal the other thread */
        word_count++;
        pthread_cond_signal(&rx);
        pthread_mutex_unlock(&my_sync);
        
        return;
}





42   [Unix/Linux] [Thread] pthread_cond() function  ±è¿µ´ë 2003/03/17 6655 1697
41   [Unix/Linux] [System V IPC] shmop() function - °øÀ¯¸Þ¸ð¸®  ±è¿µ´ë 2003/03/17 6539 1584
40   [Unix/Linux] [System V IPC] msgctl() function - ¸Þ¼¼ÁöÅ¥  ±è¿µ´ë 2003/03/17 6495 1703
39   [Unix/Linux] [Thread] pthread_mutexattr_init() function  ±è¿µ´ë 2003/03/17 6460 1940
38   [Unix/Linux] [System V IPC] msgget() function - ¸Þ¼¼ÁöÅ¥  ±è¿µ´ë 2003/03/17 6390 1603
37   [Unix/Linux] [System V IPC] semctl() function - ¼¼¸¶Æ÷¾î  ±è¿µ´ë 2003/03/17 6167 1412
36   [Unix/Linux] [Thread] pthread_once() function  ±è¿µ´ë 2003/03/17 6133 1605
35   [Unix/Linux] [Thread] »ý»êÀÚ/¼ÒºñÀÚ  ±è¿µ´ë 2003/03/17 6071 1553
34   [Unix/Linux] [Thread] pthread_cond_init() function  ±è¿µ´ë 2003/03/17 6020 1569
33   [Unix/Linux] [Thread] pthread_mutex_init() function  ±è¿µ´ë 2003/03/17 5939 1460
32   [Unix/Linux] [Thread] pthread_attr_setdetachstate() function  ±è¿µ´ë 2003/03/17 5932 1633
31   [Unix/Linux] [Thread] pthread_attr_destroy() function  ±è¿µ´ë 2003/03/17 5924 1557
30   [Unix/Linux] [Thread] pthread_detach() function  ±è¿µ´ë 2003/03/17 5836 1471
29   [Unix/Linux] [Thread] pthread_attr_setinheritsched() function  ±è¿µ´ë 2003/03/17 5821 1537
28   [Unix/Linux] [System V IPC] semop() function - ¼¼¸¶Æ÷¾î  ±è¿µ´ë 2003/03/17 5791 1439
27   [Unix/Linux] [Thread] pthread_key_create() function  ±è¿µ´ë 2003/03/17 5783 1421
  [Unix/Linux] [Thread] pthread_muxtexcond() function  ±è¿µ´ë 2003/03/17 5739 1477
25   [Unix/Linux] [Thread] pthread_create() function  ±è¿µ´ë 2003/03/17 5703 1415
24   [Unix/Linux] [Thread] pthread_self() function  ±è¿µ´ë 2003/03/17 5597 1548
23   [Unix/Linux] [Thread] pthread_attr_getdetachstate() function  ±è¿µ´ë 2003/03/17 5539 1642

[1] 2 [3][4]
 

Copyright 1999-2024 Zeroboard / skin by zero