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

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


Category

  김영대(2003-03-17 02:09:50, Hit : 4732, Vote : 1215
 [Thread] pthread_rwlock_init() 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 <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <pthread.h>
#include <stdio.h>

#define _MULTI_THREADED

static void checkResults(char *string, int rc) {

  if (rc) {
    printf("Error on : %s, rc=%d", string, rc);
    exit(EXIT_FAILURE);
  }
  return;
}

pthread_rwlock_t       rwlock;

void *rdlockThread(void *arg)
{

  int rc;

  printf("Entered thread, getting read lockn");

  rc = pthread_rwlock_rdlock(&rwlock);
  checkResults("pthread_rwlock_rdlock()n", rc);

  printf("got the rwlock read lockn");
  sleep(5);

  printf("unlock the read lockn");

  rc = pthread_rwlock_unlock(&rwlock);
  checkResults("pthread_rwlock_unlock()n", rc);

  printf("Secondary thread unlockedn");
  return NULL;
}

void *wrlockThread(void *arg)
{
  int rc;

  printf("Entered thread, getting write lockn");

  rc = pthread_rwlock_wrlock(&rwlock);
  checkResults("pthread_rwlock_wrlock()n", rc);

  printf("Got the rwlock write lock, now unlockn");

  rc = pthread_rwlock_unlock(&rwlock);
  checkResults("pthread_rwlock_unlock()n", rc);

  printf("Secondary thread unlockedn");
  return NULL;
}

int main(int argc, char **argv)
{
  int                   rc=0;
  pthread_t             thread, thread1;

  printf("Enter Testcase - %sn", argv[0]);
  printf("Main, initialize the read write lockn");

  rc = pthread_rwlock_init(&rwlock, NULL);
  checkResults("pthread_rwlock_init()n", rc);

  printf("Main, grab a read lockn");

  rc = pthread_rwlock_rdlock(&rwlock);
  checkResults("pthread_rwlock_rdlock()n",rc);

  printf("Main, grab the same read lock againn");

  rc = pthread_rwlock_rdlock(&rwlock);
  checkResults("pthread_rwlock_rdlock() secondn", rc);

  printf("Main, create the read lock threadn");

  rc = pthread_create(&thread, NULL, rdlockThread, NULL);
  checkResults("pthread_createn", rc);

  printf("Main - unlock the first read lockn");

  rc = pthread_rwlock_unlock(&rwlock);
  checkResults("pthread_rwlock_unlock()n", rc);

  printf("Main, create the write lock threadn");

  rc = pthread_create(&thread1, NULL, wrlockThread, NULL);
  checkResults("pthread_createn", rc);

  sleep(5);
  printf("Main - unlock the second read lockn");

  rc = pthread_rwlock_unlock(&rwlock);
  checkResults("pthread_rwlock_unlock()n", rc);

  printf("Main, wait for the threadsn");

  rc = pthread_join(thread, NULL);
  checkResults("pthread_joinn", rc);

  rc = pthread_join(thread1, NULL);
  checkResults("pthread_joinn", rc);

  rc = pthread_rwlock_destroy(&rwlock);
  checkResults("pthread_rwlock_destroy()n", rc);

  printf("Main completedn");
  return 0;
}





23   [Unix/Linux] [System V IPC] semop() function - 세마포어  김영대 2003/03/17 5109 1335
22   [Unix/Linux] [Thread] pthread_condattr_destroy() function  김영대 2003/03/17 4571 1333
21   [Unix/Linux] [Thread] pthread_create() function  김영대 2003/03/17 4999 1330
20   [Unix/Linux] [Thread] pthread_getschedparam() function  김영대 2003/03/17 4617 1328
19   [Unix/Linux] [Thread] 읽고/쓰기  김영대 2003/03/17 6172 1320
18   [Unix/Linux] [Thread] pthread_exit() function  김영대 2003/03/17 4589 1313
17   [Unix/Linux] [Thread] pthread_cond_wait() function  김영대 2003/03/17 4752 1309
16   [Unix/Linux] [System V IPC] semctl() function - 세마포어  김영대 2003/03/17 5426 1305
15   [Unix/Linux] [Thread] pthread_setcancelstate() function  김영대 2003/03/17 4780 1298
14   [Unix/Linux] [Thread] pthread_rwlock_init() function  김영대 2003/03/17 5006 1294
13   [Unix/Linux] [소스] top for SunOS 5.x (Solaris 2.x)  김영대 2004/02/20 11292 1237
12   [Unix/Linux] [Thread] pthread_mutex_unlock() function  김영대 2003/03/17 4501 1235
11   [Unix/Linux] [Thread] pthread_cond_destroy() function  김영대 2003/03/17 4590 1233
10   [Unix/Linux] [Thread] pthread_key_delete() function  김영대 2003/03/17 4391 1219
9   [Unix/Linux] [Thread] pthread_setcanceltype() function  김영대 2003/03/17 4469 1215
  [Unix/Linux] [Thread] pthread_rwlock_init() function  김영대 2003/03/17 4732 1215
7   [Unix/Linux] [Thread] pthread_join() function  김영대 2003/03/17 4759 1213
6   [GRID] [강좌] Globus Toolkit 3.0.2 + SimpleCA 설치  김영대 2003/11/27 6974 1202
5   [Unix/Linux] [소스] top for System V Release 4, Intel or Sparc CPU  김영대 2004/02/20 9582 1199
4   [Unix/Linux] [Thread] pthread_mutexattr_destroy() function  김영대 2003/03/17 4421 1186

[1][2][3][4][5][6][7][8] 9 [10]
 

Copyright 1999-2019 Zeroboard / skin by zero