Skip to content

Instantly share code, notes, and snippets.

@deeev-sb
Created May 12, 2020 18:03
Show Gist options
  • Save deeev-sb/acb3ad9a3188ce59be4f1bf1b7115510 to your computer and use it in GitHub Desktop.
Save deeev-sb/acb3ad9a3188ce59be4f1bf1b7115510 to your computer and use it in GitHub Desktop.
#include <pthread.h>
#include <stdio.h>
#define NUM_THREADS 3 // 3개의 thread 생성
#define TCOUNT 10 // 2개의 thread는 count를 10씩 증가시킴
#define COUNT_LIMIT 12 // 나머지 1개의 thread는 count가 12가 될 때까지 기다림
int count = 0;
int thread_ids[3] = {0, 1, 2}; // thread id
pthread_mutex_t count_mutex = PTHREAD_MUTEX_INITIALIZER; // mutex를 위한 변수 초기화
pthread_cond_t count_threshold_cv = PTHREAD_COND_INITIALIZER; // condition variable을 위한 변수 초기화
void *inc_count(void *idp) // 2개의 thread에서 작업 할 내용이 정의된 함수
{
int i, j;
double result = 0.0;
int *my_id = idp;
for (i = 0; i < TCOUNT; i++) // TCOUNT(10)까지 count 값을 증가시키는 작업 수행
{
pthread_mutex_lock(&count_mutex); // 한 번에 하나의 thread만 접근 할 수 있도록 lock을 얻음
count++;
/* Check the value of count and signal waiting thread when condition is reached.
Note that this occurs while mutex is locked. */
// if 문은 condition variable에 대한 signal을 전달해 주는 부분
if (count == COUNT_LIMIT) // 어떤 thread든 COUNT_LIMIT(12)에 도달하면
{
pthread_cond_signal(&count_threshold_cv);
// count가 COUNT_LIMIT에 도달하기를 기다리는 thread에게 signal 전달
// wait되어 있던 thread가 signal을 받음으로써 block이 해제되고 그 다음 작업 수행
printf("inc_count(): thread %d, count = %d Threshold reached.\n", *my_id, count);
}
printf("inc_count(): thread %d, count = %d, unlocking mutex\n", *my_id, count);
pthread_mutex_unlock(&count_mutex); // 그 다음에 mutex lock을 unlock 해주면 됨
/* Do some work so threads can alternate on mutex */
for (j=0; j < 1000; j++) // 특정 시간 동안 다른 작업을 하면서 기다리는 용도로 만들어 놓은 것. sleep 역할
result = result + (double)random();
}
pthread_exit(NULL); // 10번 수행 후 pthread 종료
}
void *watch_count(void *pid)
{
int *my_id = pid;
printf("Starting watch_count(): thread %d\n", *my_id);
pthread_mutex_lock(&count_mutex); // 공유 자원에 접근하기 때문에 lock을 얻어온 후 진행
while (count < COUNT_LIMIT) // 공유 자원인 count 값이 COUNT_LIMIT(12)보다 작은 경우
{
pthread_cond_wait(&count_threshold_cv, &count_mutex);
// condition variable에 대한 signal이 올 때까지 block된 상태로 wait
// lock을 release하기 위해 thread를 block 시키고
// signal이 오면 count 값이 12보다 큰 것을 알 수 있기 때문에 while문을 빠져나옴
printf("watch_count(): thread %d Condition signal received.\n", *my_id);
}
pthread_mutex_unlock(&count_mutex); // while문을 빠져나오면 mutex를 unlock 시킴
pthread_exit(NULL); // pthread 종료
}
int main(int argc, char *argv[])
{
int i, rc;
pthread_t threads[3];
/* 3개의 thread 생성 */
pthread_create(&threads[0], NULL, inc_count, (void *)&thread_ids[0]); // count를 증가시키는 작업 수행
pthread_create(&threads[1], NULL, inc_count, (void *)&thread_ids[1]); // count를 증가시키는 작업 수행
pthread_create(&threads[2], NULL, watch_count, (void *)&thread_ids[2]); // count 값이 12가 될 때까지 wait 하는 작업 수행
for (i = 0; i < NUM_THREADS; i++)
pthread_join(threads[i], NULL); // 3개의 thread가 종료될 때까지 기다림
printf("Main(): Waited on %d threads. Done.\n", NUM_THREADS);
pthread_exit(NULL); // thread가 다 종료되면 종료되는 프로그램
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment