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

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


Category

  김영대(2003-03-17 02:06:51, Hit : 4682, Vote : 1190
 [Thread] pthread_mutex_lock() 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
#define            LOOPCONSTANT     100000
#define            THREADS          10

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

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

pthread_mutex_t    mutex = PTHREAD_MUTEX_INITIALIZER;

int                i,j,k,l;
int                uselock=1;

void *threadfunc(void *parm)
{
  int   loop = 0;
  int   rc;

  for (loop=0; loop<LOOPCONSTANT; ++loop) {

    if (uselock) {
      rc = pthread_mutex_lock(&mutex);
      checkResults("pthread_mutex_lock()n", rc);
    }

    ++i; ++j; ++k; ++l;
    if (uselock) {
      rc = pthread_mutex_unlock(&mutex);
      checkResults("pthread_mutex_unlock()n", rc);
    }
  }
  return NULL;
}

int main(int argc, char **argv)
{
  pthread_t             threadid[THREADS];
  pthread_attr_t        pta;
  
  int                   rc=0;
  int                   loop=0;

  printf("Entering testcasen");
  printf("Give any number of parameters to show data corruptionn");

  if (argc != 1) {
    printf("A parameter was specified, no serialization is being done!n");
    uselock = 0;
  }

  pthread_attr_init(&pta);
  pthread_attr_setdetachstate(&pta, PTHREAD_CREATE_JOINABLE);

  printf("Creating %d threadsn", THREADS);

  for (loop=0; loop<THREADS; ++loop) {
    rc = pthread_create(&threadid[loop], &pta, threadfunc, NULL);
    checkResults("pthread_create()n", rc);
  }

  printf("Wait for resultsn");
  for (loop=0; loop<THREADS; ++loop) {

    rc = pthread_join(threadid[loop], NULL);
    checkResults("pthread_join()n", rc);
  }  

  printf("Cleanup and show resultsn");

  pthread_attr_destroy(&pta);
  pthread_mutex_destroy(&mutex);

  printf("nUsing %d threads and LOOPCONSTANT = %dn",
         THREADS, LOOPCONSTANT);
  printf("Values are: (should be %d)n", THREADS * LOOPCONSTANT);
  printf("  ==>%d, %d, %d, %dn", i, j, k, l);
  printf("Main completedn");
  return 0;
}





63   [Unix/Linux] [Thread] 생산자/소비자  김영대 2003/03/17 5573 1440
62   [Unix/Linux] [Thread] pthread_setschedparam() function  김영대 2003/03/17 9879 1706
61   [Unix/Linux] [Thread] pthread_setcanceltype() function  김영대 2003/03/17 4574 1236
60   [Unix/Linux] [Thread] pthread_setcancelstate() function  김영대 2003/03/17 4857 1305
59   [Unix/Linux] [Thread] pthread_self() function  김영대 2003/03/17 5151 1466
58   [Unix/Linux] [Thread] pthread_rwlock_init() function  김영대 2003/03/17 5096 1312
57   [Unix/Linux] [Thread] pthread_rwlock_init() function  김영대 2003/03/17 4851 1243
56   [Unix/Linux] [Thread] pthread_once() function  김영대 2003/03/17 5693 1541
55   [Unix/Linux] [Thread] pthread_muxtexcond() function  김영대 2003/03/17 5104 1385
54   [Unix/Linux] [Thread] pthread_mutex_unlock() function  김영대 2003/03/17 4585 1248
53   [Unix/Linux] [Thread] pthread_mutex_trylock() function  김영대 2003/03/17 4529 1049
  [Unix/Linux] [Thread] pthread_mutex_lock() function  김영대 2003/03/17 4682 1190
51   [Unix/Linux] [Thread] pthread_mutex_init() function  김영대 2003/03/17 5380 1402
50   [Unix/Linux] [Thread] pthread_mutexattr_init() function  김영대 2003/03/17 5835 1581
49   [Unix/Linux] [Thread] pthread_mutexattr_destroy() function  김영대 2003/03/17 4493 1197
48   [Unix/Linux] [Thread] pthread_key_delete() function  김영대 2003/03/17 4469 1231
47   [Unix/Linux] [Thread] pthread_key_create() function  김영대 2003/03/17 5185 1363
46   [Unix/Linux] [Thread] pthread_join() function  김영대 2003/03/17 4836 1224
45   [Unix/Linux] [Thread] pthread_getschedparam() function  김영대 2003/03/17 4706 1351
44   [Unix/Linux] [Thread] pthread_exit() function  김영대 2003/03/17 4668 1328

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

Copyright 1999-2020 Zeroboard / skin by zero