::: 강좌/소스/문서 :::

강좌/소스/문서 성격에 맞지 않는 광고,비방,질문의 글은 즉시 삭제하며
내용을 복사하여 사용할 경우 반드시 이곳(http://www.howto.pe.kr)을 출처로 명시하여 주세요


Category

  김영대(2003-03-17 02:10:27, Hit : 4980, Vote : 1352
 [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;
}





62   [Unix/Linux] [소스] top for System V Release 4, Intel or Sparc CPU  김영대 2004/02/20 9577 1199
61   [Unix/Linux] [소스] top for SunOS 5.x (Solaris 2.x)  김영대 2004/02/20 11276 1237
60   [Unix/Linux] [소스] String 해쉬(hash) 함수  김영대 2003/07/29 6664 1455
59   [Unix/Linux] [강좌] 인텔 센트리노 무선랜 카드: ndiswrapper  김영대 2004/06/27 11255 2017
58   [Unix/Linux] [강좌] 웹로그분석을 위한 Webalizer + GDlib + PNGlib + Zlib 설치  김영대 2003/05/04 7745 1432
57   [Unix/Linux] [강좌] Apache + MySQL + PHP4 + Zend Optimizer 설치  김영대 2003/04/15 6864 1387
56   [Unix/Linux] [Thread] 읽고/쓰기  김영대 2003/03/17 5390 1320
55   [Unix/Linux] [Thread] 생산자/소비자 2  김영대 2003/03/17 5658 1440
54   [Unix/Linux] [Thread] 생산자/소비자  김영대 2003/03/17 5481 1417
53   [Unix/Linux] [Thread] pthread_setschedparam() function  김영대 2003/03/17 7990 1691
52   [Unix/Linux] [Thread] pthread_setcanceltype() function  김영대 2003/03/17 4467 1215
51   [Unix/Linux] [Thread] pthread_setcancelstate() function  김영대 2003/03/17 4775 1298
50   [Unix/Linux] [Thread] pthread_self() function  김영대 2003/03/17 5067 1453
49   [Unix/Linux] [Thread] pthread_rwlock_init() function  김영대 2003/03/17 5000 1293
48   [Unix/Linux] [Thread] pthread_rwlock_init() function  김영대 2003/03/17 4729 1215
47   [Unix/Linux] [Thread] pthread_once() function  김영대 2003/03/17 5592 1527
  [Unix/Linux] [Thread] pthread_muxtexcond() function  김영대 2003/03/17 4980 1352
45   [Unix/Linux] [Thread] pthread_mutex_unlock() function  김영대 2003/03/17 4497 1235
44   [Unix/Linux] [Thread] pthread_mutex_trylock() function  김영대 2003/03/17 4323 1031
43   [Unix/Linux] [Thread] pthread_mutex_lock() function  김영대 2003/03/17 4592 1180

1 [2][3][4]
 

Copyright 1999-2019 Zeroboard / skin by zero