简体   繁体   English

C ++向量/类/线程

[英]C++ Vectors / Classes /Threading

I'm Using Win7 / VS2008 (9) SDK 6/7.1 我正在使用Win7 / VS2008(9)SDK 6 / 7.1

i faced a problem in a code that i'm using 我在使用的代码中遇到问题
the mini version of the code as follows 迷你版代码如下

class CONNECTION
{
    int/std::string/bool vars; // just to make it simple
    CONNECTION ( int defaultvar );
    CONNECTION ( const CONNECTION& copycon )
    ~CONNECTION ( );
    DWORD static WINAPI staticstart( void *param )  //HACK to use createthread on classes
    { return ((CONNECTION *)param)->main); } // yea it works fine
    DWORD main();
};

this class has no default constructor with a copy and a destructor all vars are copied fine and destructor leaves no memleaks, constructor is simple as assigning the paramters to a var .. all vars and code are omitted to make it simple and because they aren't the prob. 此类没有带副本和析构函数的默认构造函数,所有变量均被良好复制,并且析构函数没有任何遗忘,构造函数很简单,因为将参数分配给var ..省略了所有var和代码以使其简单,因为它们不是概率。

class main
{
    std::vector<CONNECTION> con;
    int addcon( int defaultvarofcon )
    {
        CONNECTION temp( defaultvar );
        con.push_back( temp );
        return con.size() - 1;
    }
}

so far so good when i run a console test program that only have an include and this code 到目前为止,当我运行仅包含include和此代码的控制台测试程序时,效果很好

main mymainclass;
mymainclass.addcon( 0 );

program runs fine closes without errors 程序运行良好,关闭无错误
but when i add extra code like 但是当我添加额外的代码时

main mymainclass;
mymainclass.addcon( 0 );
mymainclass.addcon( 1 );
mymainclass.addcon( 2 );

program crashes with access violation after checking my code twice i debugged it step by step on all threads i found out the main thread can read correct values of all vector class/elements in both main thread and worker thread ONLY if i'm using one element at the vector 两次检查我的代码后,程序由于访问冲突而崩溃,我在所有线程上对其进行了逐步调试,发现主线程只能在使用一个元素的情况下才能读取主线程和工作线程中所有向量类/元素的正确值在矢量

however if i'm using more than one like the second code all data on all elements on their own threads are inaccessible ( bad pointers ) . 但是,如果我像第二个代码那样使用多个代码,则无法访问其自己线程上所有元素上的所有数据(错误的指针)。 but on main thread they still correct and showing right values 但是在主线程上它们仍然正确并显示正确的值

can any one please help me figure out what's wrong with this code ? 谁能帮我弄清楚这段代码有什么问题吗?

std::vector is not designed to be thread safe. std::vector并非设计为线程安全的。 Therefore you have to use some sort of mutex to make sure only one thread accesses it at a time. 因此,您必须使用某种互斥锁来确保一次只有一个线程访问它。

Otherwise, if any thread resizes the vector, the vector may have to allocate new storage, copy elements, then free the old storage... and clearly freeing the old storage while some other thread is using it is a very bad thing. 否则,如果任何线程调整了向量的大小,则向量可能必须分配新的存储空间,复制元素,然后释放旧的存储空间……并且在其他线程正在使用它的情况下显然释放旧的存储空间是非常不好的事情。

You need to allocate the CONNECTION objects yourself. 您需要自己分配CONNECTION对象。 Letting std::vector manage them means they are going to get moved around in memory when the vector resizes, and the pointers held by other threads are left dangling. std::vector管理它们意味着当向量调整大小时,它们将在内存中四处移动,而其他线程持有的指针则悬空了。 Resizing a vector invalidates all pointers to any of its content. 调整向量的大小会使指向其任何内容的所有指针无效。

If you need a custom destructor and copy constructor, you probably also need a custom assignment operator. 如果需要自定义析构函数和复制构造函数,则可能还需要自定义赋值运算符。

Without that you quickly end up with several instances of your class referring to the same internal pointers/..., easily leading to memory corruption and access violations. 否则,您很快会导致类的多个实例引用相同的内部指针/ ...,从而容易导致内存损坏和访问冲突。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM