반응형 lock1 [C언어] pthread 라이브러리를 이용한 스레드 동기화 Threads Synchornization Multi Thread에서 전역 변수, 힙 영역, malloc 등은 Data Sharing을 한다.다음 코드는 입력한 초만큼 전역 변수 cnt가 증가하는 예제이다.volatile: 컴파일러 최적화 방지. cnt 변수를 레지스터가 아닌 메모리에서 직접 읽고 쓰게 만듦volatile int cnt;void* do_loop (void *loop){ while(1) cnt++; return NULL;}int main(int argc, char *argv[]){ int status, sec; pthread_t thread_id; cnt = 0; sec = atoi(argv[1]); if (pthread_create(&threa.. 2025. 1. 28. 이전 1 다음