본문 바로가기
C++

혼자 해보는 동시성 프로그래밍 연구

by 개발자 포비 2025. 1. 25.

동시성 프로그래밍 테스트 케이스 정리:

1. Lock-Free Queue vs Mutex Queue

// 테스트 조건
- 작업 크기: 짧은 작업(1ms) vs 긴 작업(100ms)
- 쓰레드 수: 2, 4, 8, 16
- 생산자:소비자 비율: 1:1, 1:N, N:1

2. SpinLock vs Mutex 성능

- CPU 코어수 기준 쓰레드 수 변경
- 크리티컬 섹션 길이: 10ns, 100ns, 1000ns
- 경합 상황 vs 비경합 상황

3. Read-Write Lock 활용

- 읽기:쓰기 비율: 99:1, 90:10, 50:50
- 쓰레드 수: 2~32
- 읽기/쓰기 작업 시간 변경

4. False Sharing 회피

- 캐시라인 정렬 vs 비정렬
- 쓰레드 수에 따른 영향
- 데이터 접근 패턴 변경

테스트 수도코드만 우선 작성

// 1. Lock-Free vs Mutex Queue
TestLockFreeVsMutex() {
   for(thread_counts in [2,4,8,16]) {
       for(work_size in [short_task, long_task]) {
           for(producer_consumer_ratio in [1:1, 1:N, N:1]) {
               test_duration = 60s

               // Lock-Free Test
               start_time = now()
               spawn_threads(producers, consumers)
               record_throughput()
               record_latency()

               // Mutex Test
               repeat_same_test()

               compare_and_log_results()
           }
       }
   }
}

// 2. SpinLock vs Mutex
TestSpinLockVsMutex() {
   for(critical_section_length in [10ns,100ns,1000ns]) {
       for(thread_count in [cores/2, cores, cores*2]) {
           for(contention in [high,low]) {
               // SpinLock Test
               measure_completion_time()
               measure_cpu_usage()

               // Mutex Test
               repeat_same_test()

               analyze_performance_diff()
           }
       }
   }
}

// 3. RWLock Test
TestReadWriteLock() {
   for(read_write_ratio in [99:1,90:10,50:50]) {
       for(thread_count in [2,4,8,16,32]) {
           for(operation_time in [fast,medium,slow]) {
               start_timer()
               spawn_reader_threads()
               spawn_writer_threads()
               measure_throughput()
               measure_reader_latency()
               measure_writer_latency()
           }
       }
   }
}

// 4. False Sharing
TestFalseSharing() {
   struct aligned_data[thread_count]   // 64-byte aligned
   struct unaligned_data[thread_count] // default aligned

   for(thread_count in [2,4,8,16]) {
       for(access_pattern in [sequential,random]) {
           // Test Aligned
           measure_performance()
           monitor_cache_misses()

           // Test Unaligned
           repeat_same_test()

           compare_cache_behavior()
       }
   }
}

'C++' 카테고리의 다른 글

C++ 지뢰찾기 게임 만들기  (0) 2022.11.06

댓글