기본적으로 STL Container들은 Thread Safety하지 않다.
뭐 구현에 따라 다를 수 있지만..
서로 다른 Thread에서 Writing을 하거나
한 Thread에서 Writing을 하는 도중에 다른 Thread에서 Reading을 할 경우,
문제가 발생할 수 있다. (많은 경우, 프로그램이 Crash됨.)
관련 문서들은 아래를 참조..
---------------------------------------------------------------------------------
따라서, Multi-threading 환경에서 STL Container를 사용하기 위해서는 주의가 필요하다.
한 Thread에서 특정 Container object에 Writing operation을 하기 위해서는 (특히, 삽입/삭제)
다른 Thread에서는 해당 Container object에 reading이나 writing을 하지 않도록 locking을 해주어야 한다.
뭐 복잡하게 설명할 것 없이,
Intel에서는 Concurrency한 Template Library를 제공한다.
해당 Library에는
여러가지 기능이 제공되지만, 가장 관심이 가는 것 중 하나로
High concurrency & Thread-safe(인텔 측에 따르면)한 Container가 아닐까 싶다.
High concurrency라고 한 이유는, 걍 적당히 Operation 위 아래에다가 Lock/Unlock을 한게 아니라 Lock-free algorithm과 Fine-grained locking을 사용했다는 것인데 실제로 이렇게 구현하는 것은 몹시 까다로운 방법이다.
intel TBB에서는 아래와 같은 총 3개의 Container가 제공 된다.
사용 예는 기회가 되면 공유하기로 하고,
문서 중 흥미로운 사항을 몇 가지 이야기를 적어 보면..
[concurrent_vector]
- Operations on concurrent_vector are concurrency safe with respect to growing, not for
clearing or destroying a vector. Never invoke method clear() if there are other
operations in flight on the concurrent_vector.
= concurrent_vector의 경우, dynamic하게 growing하는 부분에서는 thread-safe하나 벡터의 clearing이나 destroying 부분에서는 thread-safe하지 않단다. clear()나 파괴자 등이 실행 중일 때, 다른 오퍼레이션도 일어난다면 해당 변수에 대한 Logic적인 부분도 고려 해봐야하지 않을까 싶다.
[concurrent_queue]
- Queues are widely used in parallel programs to buffer consumers from producers. Before
using an explicit queue, however, consider using parallel_while or pipeline instead. These options are often more efficient than queues for the following reasons:
• A queue is inherently a bottle neck, because it must maintain first-in first-out order.
• A thread that is popping a value may have to wait idly until the value is pushed.
• A queue is a passive data structure. If a thread pushes a value, it could take time until
it pops the value, and in the meantime the value (and whatever it references)
becomes “cold” in cache. Or worse yet, another thread pops the value, and the value
(and whatever it references) must be moved to the other processor.
= 뭐 복잡하게 설명하고 있지만, Queue의 FIFO 특성 자체가 순서가 강요되기 때문에.. 효율적이지 않는다는 부분이다. Queue를 꼭 사용해야하는지 고려해보고, 굳이 사용하지 않아도 된다면 다른 방법을 사용하길 권장하고 있다.