8.스레드

8-1.스레드 개요

8-2.스레드 기본

ex)

#include <stdio.h>

#include <unistd.h>

#include <stdlib.h>

#include <pthread.h>

void *thread_function(void *arg);

char message[]="Hello World";

int main()

{

int res;

pthread_t a_thread;

void *thread_result;

res=pthread_create(&a_thread,NULL,thread_function,(void *)message);

if(res !=0)

{

perror("Thread creation failed");

exit(0);

}

printf("Waiting for thread to finish...\n");

res = pthread_join(a_thread,&thread_result);

if(res!=0)

{

perror("Thread join failed");

exit(0);

}

printf("Thread joined, it returned %s\n",(char *)thread_result);

printf("Message is now %s\n",message);

exit(1);

}

void *thread_function(void *arg)

{

printf("thread_function is running. Argument was %s\n",(char *)arg);

sleep(5);

strcpy(message,"Bye!");

pthread_exit("Thank you for the CPU time");

}

컴파일 방법 gcc -D_REENTRANT exthread.c -lpthread -o thread

출력결과

[sylee@vision thread]$ ./a.out

Waiting for thread to finish...

thread_function is running. Argument was Hello World

Thread joined, it returned Thank you for the CPU time

Message is now Bye!

8-3 반복 스레드

ex)

#include <stdio.h>

#include <unistd.h>

#include <stdlib.h>

#include <pthread.h>

void *thread_function(void *arg);

char *message="test";

int flag;

int main()

{

int res;

char data[20];

pthread_t a_thread;

void *thread_result;

res=pthread_create(&a_thread,NULL,thread_function,(void *)message);

if(res !=0)

{

perror("Thread creation failed");

exit(0);

}

gets(data);

flag=1;

printf("Message is now %s\n",message);

exit(1);

}

void *thread_function(void *arg)

{

int i;

i=0;

while(!flag)

{

printf("num=%d\n",i);

i++;

sleep(1);

}

message="end";

}

출력 결과

[sylee@vision thread]$ ./a.out

num=0

num=1

num=2

num=3

Message is now test

문제점 찾기

8-4 세마포

ex)

#include <stdio.h>

#include <unistd.h>

#include <stdlib.h>

#include <string.h>

#include <pthread.h>

#include <semaphore.h>

void *thread_function(void *arg);

sem_t bin_sem;

#define WORK_SIZE 1024

char work_area[WORK_SIZE];

int main()

{

int res;

pthread_t a_thread;

void *thread_result;

res=sem_init(&bin_sem,0,0);

res=pthread_create(&a_thread,NULL,thread_function,NULL);

printf("Input some text.Enter 'end' to finish\n");

while(strncmp("end",work_area,3) !=0 )

{

fgets(work_area,WORK_SIZE,stdin);

sem_post(&bin_sem);

}

printf("\naiting for thread to finish...\n");

res=pthread_join(a_thread,&thread_result);

printf("Thread joined\n");

exit(1);

}

void *thread_function(void *arg)

{

sem_wait(&bin_sem);

while(strncmp("end",work_area,3) !=0)

{

printf("You input %d characters\n",strlen(work_area)-1);

sem_wait(&bin_sem);

}

pthread_exit(0);

}

출력 결과

[sylee@vision thread]$ ./a.out

Input some text.Enter 'end' to finish

sdkafjklas saldkfj

You input 18 characters

dsk

You input 3 characters

end

aiting for thread to finish...

Thread joined

8-5 뮤텍스

ex)

#include <stdio.h>

#include <unistd.h>

#include <stdlib.h>

#include <string.h>

#include <pthread.h>

#include <semaphore.h>

void *thread_function(void *arg);

pthread_mutex_t work_mutex;

#define WORK_SIZE 1024

char work_area[WORK_SIZE];

int time_to_exit=0;

int main()

{

int res;

pthread_t a_thread;

void *thread_result;

res=pthread_mutex_init(&work_mutex,NULL);

res=pthread_create(&a_thread,NULL,thread_function,NULL);

pthread_mutex_lock(&work_mutex);

printf("Input some text,Enter 'end' to finish\n");

while(!time_to_exit)

{

fgets(work_area,WORK_SIZE,stdin);

pthread_mutex_unlock(&work_mutex);

while(1)

{

pthread_mutex_lock(&work_mutex);

if(work_area[0]!=0)

{

pthread_mutex_unlock(&work_mutex);

sleep(1);

}

else

{

break;

}

}

}

pthread_mutex_unlock(&work_mutex);

printf("\nWaiting for thread to finish...\n");

res=pthread_join(a_thread,&thread_result);

printf("Thread joined\n");

pthread_mutex_destroy(&work_mutex);

exit(1);

}

void *thread_function(void *arg)

{

sleep(1);

pthread_mutex_lock(&work_mutex);

while(strncmp("end",work_area,3) !=0)

{

printf("You input %d characters\n",strlen(work_area)-1);

work_area[0]=0;

pthread_mutex_unlock(&work_mutex);

sleep(1);

pthread_mutex_lock(&work_mutex);

while(work_area[0]==0)

{

pthread_mutex_unlock(&work_mutex);

sleep(1);

pthread_mutex_lock(&work_mutex);

}

}

time_to_exit=1;

work_area[0]=0;

pthread_mutex_unlock(&work_mutex);

pthread_exit(0);

}

출력 결과

[sylee@vision thread]$ ./a.out

Input some text,Enter 'end' to finish

ksdfajlas

You input 9 characters

end

Waiting for thread to finish...

Thread joined