Processor

【라즈베리파이】 wiringPi C 언어 : 쓰레드

작성자 임베디드코리아 작성일26-02-19 23:44 조회114회 댓글0건
간단한 쓰레드
--->>> 예제 : multi_thread.c  <<<-------------------
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

void* threadFunc(void* arg) {
    int id = *(int*)arg;
    printf("Thread %d is running\n", id);
    sleep(1);
    printf("Thread %d is exiting\n", id);
    return NULL;
}

int main() {
    pthread_t threads[3];
    int thread_ids[3] = {1, 2, 3};

    for (int i = 0; i < 3; i++) {
        pthread_create(&threads[i], NULL, threadFunc, &thread_ids[i]);
    }

    for (int i = 0; i < 3; i++) {
        pthread_join(threads[i], NULL);
    }

    printf("All threads have finished\n");
    return 0;
}
---------------------------------------------------------------
$ gcc  -o  multi_thread  Step_multi_thread.c  -lpthread
$ sudo  ./multi_thread

◆ 사용자가 특정 문자를 입력했을 때, 스레드가 생성하여 스레드 ID를 보여주고 스레드 취소 및 종료한다.
◆ 출력 결과를 보면 부모 프로세서에 다른 스레드 LWP(Light Weight Process)가 생성된 것을 확인할 수 있다.
--->>>  pthread_id.c  <<<------------------------------------------------
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/syscall.h>
#define MAX_THREAD_NUM 100

void* newThread(void *arg){
        pthread_t tid;
        tid = pthread_self();
        //printf("thread[%lu] start\n", tid);
        pid_t lwp = syscall(SYS_gettid);
        printf("pthread_self() ID: %lu, LWP (syscall): %d\n", (unsigned long)tid, lwp);
    if ((unsigned long)tid == (unsigned long)lwp) {
        printf("pthread_t and LWP are identical!\n");
    } else {
        printf("pthread_t and LWP differ.\n");
    }
        pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); //
        while(1){
                sleep(1);
                printf("thread[%lu] work\n", tid);
        }//while
}//newThread

void main(void) {
int result;
pthread_t thread[MAX_THREAD_NUM];
int thread_num = 0;

printf("n: make new thread\n"
                "d: delete last created thread\n"
                "h: help\n"
                "e: exit\n");

while(1) {
        char inputChar;
        scanf("%c", &inputChar);
        switch(inputChar) {
                case 'n': {
                        printf("create thread");
                        result = pthread_create(&thread[thread_num],NULL,newThread,NULL);
                        if(result) {
                                printf(" ERROR [%d]\n", result);
                        } //if
                        else {
                                printf(" SUCCESS [%lu]\n", thread[thread_num]);
                                thread_num++;
                        }//else
                }//n
                break;
                case 'd': {
                        int i;
                        if(thread_num){
                                printf("delte thread");
                                result = pthread_cancel(thread[thread_num-1]);
                                if(result) {
                                        printf(" ERROR [%d]\n", result);
                                } //if
                                else {
                                        printf(" SUCCESS [%lu]\n", thread[thread_num-1]);
                                        thread_num--;
                                }//else
                        }//if
                        else {
                                printf("There is no thread to delete.\n");
                        }//else
                }//d
                case 'h' : {
                        printf("n: make new thread\n"
                                        "d: delete last created thread\n"
                                        "h: help\n"
                                        "e: exit\n");
                }// h
                break;
                case 'e' : {
                        printf("Exit Program\n");
                        return;
                }
                break;
                default:
                break;
        }//switch
}//while
}//main
----------------------------------------------------------------------
$ gcc pthread_id.c  -o  pthread_id  -lpthread
$ ./pthread_id