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

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


Category

  김영대(2003-03-17 01:52:40, Hit : 5147, Vote : 1354
 [Thread] pthread_create() 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;
}

typedef struct {

  int   value;
  char  string[128];
} thread_parm_t;

void *threadfunc(void *parm)
{

  thread_parm_t *p = (thread_parm_t *)parm;

  printf("%s, parm = %dn", p->string, p->value);
  free(p);
  return NULL;
}

int main(int argc, char **argv)
{

  pthread_t             thread;
  int                   rc=0;
  pthread_attr_t        pta;
  thread_parm_t        *parm=NULL;

  printf("Enter Testcase - %sn", argv[0]);
  printf("Create a thread attributes objectn");

  rc = pthread_attr_init(&pta);
  checkResults("pthread_attr_init()n", rc);

  /* Create 2 threads using default attributes in different ways */
  printf("Create thread using the NULL attributesn");
  /* Set up multiple parameters to pass to the thread */

  parm = malloc(sizeof(thread_parm_t));
  parm->value = 5;
  strcpy(parm->string, "Inside secondary thread");

  rc = pthread_create(&thread, NULL, threadfunc, (void *)parm);
  checkResults("pthread_create(NULL)n", rc);

  printf("Create thread using the default attributesn");

  /* Set up multiple parameters to pass to the thread */
  parm = malloc(sizeof(thread_parm_t));
  parm->value = 77;

  strcpy(parm->string, "Inside secondary thread");

  rc = pthread_create(&thread, &pta, threadfunc, (void *)parm);
  checkResults("pthread_create(&pta)n", rc);

  printf("Destroy thread attributes objectn");

  rc = pthread_attr_destroy(&pta);
  checkResults("pthread_attr_destroy()n", rc);

  /* sleep() isn't a very robust way to wait for the thread */
  sleep(5);

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





43   [Unix/Linux] [Thread] pthread_equal() function  김영대 2003/03/17 4857 1372
42   [Unix/Linux] [Thread] pthread_detach() function  김영대 2003/03/17 5281 1426
  [Unix/Linux] [Thread] pthread_create() function  김영대 2003/03/17 5147 1354
40   [Unix/Linux] [Thread] pthread_cond_wait() function  김영대 2003/03/17 4861 1323
39   [Unix/Linux] [Thread] pthread_cond_timedwait() function  김영대 2003/03/17 6064 1784
38   [Unix/Linux] [Thread] pthread_cond_signal() function  김영대 2003/03/17 4356 1196
37   [Unix/Linux] [Thread] pthread_cond_init() function  김영대 2003/03/17 5571 1515
36   [Unix/Linux] [Thread] pthread_cond_destroy() function  김영대 2003/03/17 4708 1243
35   [Unix/Linux] [Thread] pthread_cond_broadcast() function  김영대 2003/03/17 4823 1395
34   [Unix/Linux] [Thread] pthread_condattr_destroy() function  김영대 2003/03/17 4693 1348
33   [Unix/Linux] [Thread] pthread_cond() function  김영대 2003/03/17 6184 1635
32   [Unix/Linux] [Thread] pthread_cleanup_push() function  김영대 2003/03/17 5047 1452
31   [Unix/Linux] [Thread] pthread_cleanup_pop() function  김영대 2003/03/17 5034 1482
30   [Unix/Linux] [Thread] pthread_cancel() function  김영대 2003/03/17 5985 1701
29   [Unix/Linux] [Thread] pthread_attr_setinheritsched() function  김영대 2003/03/17 5221 1456
28   [Unix/Linux] [Thread] pthread_attr_setdetachstate() function  김영대 2003/03/17 5433 1540
27   [Unix/Linux] [Thread] pthread_attr_init() function  김영대 2003/03/17 4882 1382
26   [Unix/Linux] [Thread] pthread_attr_getschedparam() function  김영대 2003/03/17 5031 1416
25   [Unix/Linux] [Thread] pthread_attr_getinheritsched() function  김영대 2003/03/17 5082 1559
24   [Unix/Linux] [Thread] pthread_attr_getdetachstate() function  김영대 2003/03/17 5139 1490

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

Copyright 1999-2021 Zeroboard / skin by zero