도전2022

testthread.c Test 본문

작업/OvmStart(2010)

testthread.c Test

hotdigi 2010. 4. 1. 17:30
testthread.c를 조금 수정하여 다시 컴파일하기
(인터넷에 공개되어 있는 코드 수정)

소스를 컴파일하자.
# gcc testthread.c -lpthread

파일

소스
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <inttypes.h>
#include <pthread.h>
#include <sys/wait.h>
#include <sched.h>
 
void *thread1_func(void *arg)
{
    int i;
    char buf[512];
 
    for(i=0;i<10;i++) {
        snprintf(buf, sizeof(buf), "thread1: %d %s\n", i, (char *)arg);
        //qemu_write(1, buf, strlen(buf));
        printf("%s", buf);
        usleep(100 * 1000);
    }
    return NULL;
}
 
void *thread2_func(void *arg)
{
    int i;
    char buf[512];
    for(i=0;i<20;i++) {
        snprintf(buf, sizeof(buf), "thread2: %d %s\n", i, (char *)arg);
        //qemu_write(1, buf, strlen(buf));
        printf("%s", buf);
        usleep(150 * 1000);
    }
    return NULL;
}
 
void test_pthread(void)
{
    pthread_t tid1, tid2;
 
    pthread_create(&tid1, NULL, thread1_func, "hello1");
    pthread_create(&tid2, NULL, thread2_func, "hello2");
    pthread_join(tid1, NULL);
    pthread_join(tid2, NULL);
    printf("End of pthread test.\n");
}
 
int main(int argc, char **argv)
{
    test_pthread();
    return 0;
}


실행 내용
# ./a.out
thread2: 0 hello2
thread1: 0 hello1
thread1: 1 hello1
thread2: 1 hello2
thread1: 2 hello1
thread2: 2 hello2
thread1: 3 hello1
thread1: 4 hello1
thread2: 3 hello2
thread1: 5 hello1
thread2: 4 hello2
thread1: 6 hello1
thread1: 7 hello1
thread2: 5 hello2
thread1: 8 hello1
thread2: 6 hello2
thread1: 9 hello1
thread2: 7 hello2
thread2: 8 hello2
thread2: 9 hello2
thread2: 10 hello2
thread2: 11 hello2
thread2: 12 hello2
thread2: 13 hello2
thread2: 14 hello2
thread2: 15 hello2
thread2: 16 hello2
thread2: 17 hello2
thread2: 18 hello2
thread2: 19 hello2
End of pthread test.


'작업 > OvmStart(2010)' 카테고리의 다른 글

wine 컴파일(wine-1.1.33 --without-pthread) 실패  (0) 2010.04.02
wine 관련 글  (0) 2010.04.02
QEMU Configure Options List  (0) 2010.04.01
Wine internet explorer 실행 테스트  (0) 2010.03.24
Wine architecture  (0) 2010.03.24