Vector线程同步问题

多线程读写vector时出现core dump情况,调查发现当vector数量增大时会重新分配内存

方法一:定义vector后马上预留足够空间heads.reserve(2000);

方法二:加锁:

1头文件#include <pthread.h>

2全局定义pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; 

3使用时:

pthread_mutex_lock(&mutex);

...

pthread_mutex_unlock(&mutex); 

4使用完毕pthread_mutex_destroy(&mutex);

猜你喜欢

转载自blog.csdn.net/goalcn/article/details/80999000