Thread-sicheres, schlossfreies Array

Ich habe eine C ++ - Bibliothek, die einige Berechnungen für mehrere Threads durchführen soll. Ich habe unabhängigen Thread-Code erstellt (d. H. Es gibt keine gemeinsamen Variablen zwischen ihnen), mit Ausnahme eines Arrays. Das Problem ist, ich weiß nicht, wie ich es threadsicher machen soll.

Ich habe mir Mutex Lock / Unlock (QMutex(wie ich Qt benutze), aber es passt nicht zu meiner Aufgabe - während ein Thread den Mutex sperrt, warten andere Threads!

Dann habe ich darüber gelesenstd::atomic, was genau so aussah, wie ich es brauchte. Trotzdem habe ich versucht, es wie folgt zu benutzen:

std::vector<std::atomic<uint64_t>> *myVector;

Und es erzeugte Compilerfehler (Verwendung der gelöschten Funktion 'std :: atomic :: atomic (const std :: atomic &)'). Dann habe ich gefundendie Lösung - Verwenden Sie eine Spezialverpackung fürstd::atomic. Ich habe es versucht:

struct AtomicUInt64
{
    std::atomic<uint64_t> atomic;

    AtomicUInt64() : atomic() {}

    AtomicUInt64 ( std::atomic<uint64_t> a ) : atomic ( atomic.load() ) {}

    AtomicUInt64 ( AtomicUInt64 &auint64 ) : atomic ( auint64.atomic.load() ) {}

    AtomicUInt64 &operator= ( AtomicUInt64 &auint64 )
    {
                atomic.store ( auint64.atomic.load() );
    }
};

std::vector<AtomicUInt64> *myVector;

Dieses Ding wird erfolgreich kompiliert, aber wenn ich den Vektor nicht füllen kann:

myVector = new std::vector<AtomicUInt64>();

for ( int x = 0; x < 100; ++x )
{
    /* This approach produces compiler error:
     * use of deleted function 'std::atomic<long long unsigned int>::atomic(const std::atomic<long long unsigned int>&)'
     */
    AtomicUInt64 value( std::atomic<uint64_t>( 0 ) ) ;
    myVector->push_back ( value );

    /* And this one produces the same error: */
    std::atomic<uint64_t> value1 ( 0 );
    myVector->push_back ( value1 );
}

Was mache ich falsch? Ich nehme an, ich habe alles versucht (vielleicht auch nicht) und nichts hat geholfen. Gibt es andere Möglichkeiten für die threadsichere gemeinsame Nutzung von Arrays in C ++?

Ich benutze übrigens den MinGW 32bit 4.7 Compiler unter Windows.

Antworten auf die Frage(3)

Ihre Antwort auf die Frage