STL 线程安全

是的,所有的 STL 容器都不是线程安全的。当多个线程并发访问同一个 STL 容器时,如果有至少一个线程对容器进行了修改(插入、删除、更新等操作),则必须使用同步机制(如锁)来确保线程安全。以下是一些常见的 STL 容器以及使用互斥锁进行线程同步的示例。

示例 1:std::vector 的线程安全操作

#include <iostream>
#include <vector>
#include <thread>
#include <pthread.h>

std::vector<int> shared_vector;
pthread_mutex_t vec_mutex = PTHREAD_MUTEX_INITIALIZER;

void thread_safe_push_back(int value) {
    pthread_mutex_lock(&vec_mutex);
    shared_vector.push_back(value);
    pthread_mutex_unlock(&vec_mutex);
}

void thread_function() {
    for (int i = 0; i < 100; ++i) {
        thread_safe_push_back(i);
    }
}

int main() {
    std::thread t1(thread_function);
    std::thread t2(thread_function);

    t1.join();
    t2.join();

    std::cout << "Vector size: " << shared_vector.size() << std::endl;

    pthread_mutex_destroy(&vec_mutex);

    return 0;
}

示例 2:std::map 的线程安全操作

#include <iostream>
#include <map>
#include <thread>
#include <pthread.h>

std::map<int, int> shared_map;
pthread_mutex_t map_mutex = PTHREAD_MUTEX_INITIALIZER;

void thread_safe_insert(int key, int value) {
    pthread_mutex_lock(&map_mutex);
    shared_map[key] = value;
    pthread_mutex_unlock(&map_mutex);
}

void thread_function() {
    for (int i = 0; i < 100; ++i) {
        thread_safe_insert(i, i * 10);
    }
}

int main() {
    std::thread t1(thread_function);
    std::thread t2(thread_function);

    t1.join();
    t2.join();

    std::cout << "Map size: " << shared_map.size() << std::endl;

    pthread_mutex_destroy(&map_mutex);

    return 0;
}

示例 3:std::queue 的线程安全操作

#include <iostream>
#include <queue>
#include <thread>
#include <pthread.h>

std::queue<int> shared_queue;
pthread_mutex_t queue_mutex = PTHREAD_MUTEX_INITIALIZER;

void thread_safe_enqueue(int value) {
    pthread_mutex_lock(&queue_mutex);
    shared_queue.push(value);
    pthread_mutex_unlock(&queue_mutex);
}

void thread_function() {
    for (int i = 0; i < 100; ++i) {
        thread_safe_enqueue(i);
    }
}

int main() {
    std::thread t1(thread_function);
    std::thread t2(thread_function);

    t1.join();
    t2.join();

    std::cout << "Queue size: " << shared_queue.size() << std::endl;

    pthread_mutex_destroy(&queue_mutex);

    return 0;
}

一些重要的注意事项

  1. 加锁范围:锁的范围应该尽量小,以减少锁的持有时间,减少争用,提高并发性能。

  2. 死锁预防:确保所有加锁和解锁操作是成对出现的,避免死锁。例如,在异常处理中确保解锁。

  3. 读写锁:如果有大量的读操作和少量的写操作,可以考虑使用读写锁(如 pthread_rwlock_t)来提高性能。

总结

所有的 STL 容器在多线程环境中都需要使用同步机制来确保线程安全。使用 pthread_mutex_t 进行同步是一个常见的方法。通过仔细设计锁的使用范围和策略,可以在保证线程安全的同时,尽量减少对性能的影响。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值