请问一下boost:shared_ptr的线程安全有关问题
请教一下boost::shared_ptr的线程安全问题
请教一下boost::shared_ptr的线程安全问题
看了一下boost的官网关于shared_ptr线程安全的那一小结,
http://www.boost.org/doc/libs/1_53_0/libs/smart_ptr/shared_ptr.htm#ThreadSafety
前面提到的那几个线程安全需要注意的地方,后面则说在1.33版本之后加入了lock-free,这个意思是1.33版本以后都是线程安全的不需要注意前面的那几个例子吗?
感觉有些歧义我按他说的写了些例子,虽然有崩溃的情况但都是new的时候出现错误(new的太频繁),貌似和线程没关系,所以求教。
------解决方案--------------------
请教一下boost::shared_ptr的线程安全问题
看了一下boost的官网关于shared_ptr线程安全的那一小结,
http://www.boost.org/doc/libs/1_53_0/libs/smart_ptr/shared_ptr.htm#ThreadSafety
前面提到的那几个线程安全需要注意的地方,后面则说在1.33版本之后加入了lock-free,这个意思是1.33版本以后都是线程安全的不需要注意前面的那几个例子吗?
感觉有些歧义我按他说的写了些例子,虽然有崩溃的情况但都是new的时候出现错误(new的太频繁),貌似和线程没关系,所以求教。
#include <cstdlib>
#include <iostream>
#include <time.h>
#include <stdio.h>
#include <winsock2.h>
#include <windows.h>
#include <process.h>
#include <iostream>
#include <boost/shared_ptr.hpp>
boost::shared_ptr<int> p(new int(42));
unsigned int __stdcall threadfunA(void* arg)
{
while(1)
{
p.reset(new int(1));
Sleep(1);
}
return 0;
}
unsigned int __stdcall threadfunB(void* arg)
{
while(1)
{
p.reset(new int(1));
Sleep(1);
}
return 0;
}
int main()
{
HANDLE client;
client = (HANDLE) _beginthreadex (NULL, 0,
threadfunA, 0, 0 , NULL);
if (client == 0) {
printf ("error1 in _beginthreadex\n");
return -1;
}
HANDLE client2;
client2 = (HANDLE) _beginthreadex (NULL, 0,
threadfunB, 0, 0 , NULL);
if (client2 == 0) {
printf ("error2 in _beginthreadex\n");
return -1;
}
system("pause");
return 0;
}
shared_ptr 线程安全
------解决方案--------------------