2016-04-07 15 views
1

こんにちは、私はC++ 11とスレッドを試しています。私は解決する方法を理解できないいくつかの問題に直面しています。私は別のスレッドでクラスのものをやりたいし、いくつかの関数とクラスでmutexを使う必要があります。 はい私はstd :: mutexはコピー可能ではありませんが、私の問題の解決方法はわかりません。私が持っているもの共有mutexとクラスインスタンスを持つC++ 11スレッド

は、これまで main.cppにある:

#include <iostream> 
#include <thread> 
#include <mutex> 
#include "ThreadClass.h" 

void thread_without_class(std::mutex &mutex); 
void thread_without_class2(int number, std::mutex &mutex); 

int main(int argc, char** argv){  
std::mutex mutex; 

ThreadClass *threadClass = new ThreadClass(); 

std::thread t1(thread_without_class, &mutex); 
std::thread t2(thread_without_class2, 10, &mutex); 
std::thread t3(thread_without_class2, 11, &mutex); 
std::thread t4(threadClass); 

threadClass->mutex = mutex; 
threadClass->numberOfOutputsI = 5; 
threadClass->ThreadOutput(); 

t1.join(); 
t2.join(); 
t3.join(); 
t4.join(); 

delete threadClass; 
return 0; 
} 


void thread_without_class(std::mutex &mutex){ 
std::lock_guard<std::mutex> lock(mutex); 
std::cout << "c++11 thread without anything" << std::endl; 
} 

void thread_without_class2(int number, std::mutex &mutex){ 
for (auto i=0; i<number; i++){ 
    std::lock_guard<std::mutex> lock(mutex); 
    std::cout << "c++11 thread with function parameter: " << number << std::endl; 
} 
} 

ThreadClass.h:

#pragma once 
#include <mutex> 

class ThreadClass{ 
public: 
ThreadClass(void); 
~ThreadClass(void); 
void ThreadOutput(); 

std::mutex mutex; 
int numberOfOutputsI; 
}; 

ThreadClass.cpp

#include "ThreadClass.h" 
#include <iostream> 

ThreadClass::ThreadClass(void){ 
numberOfOutputsI = 0; 
} 

ThreadClass::~ThreadClass(void){ 
} 

void ThreadClass::ThreadOutput(){ 
for (auto i=0; i<numberOfOutputsI; i++){ 
    std::lock_guard<std::mutex> lock(mutex); 
    std::cout << "I am a class called as a thread" << std::endl; 
}} 
+2

あなたは何の問題もありませんあなたが問題を策定していない場合は、 –

+0

申し訳ありませんが、私の問題はスレッドとしてクラスの新しいインスタンスを開始する方法ですか? 私の2番目の問題は、クラスと関数でmutexを共有する方法でした。 –

答えて

3

あなたはコピーをしたくありません、参照/ポインタ:

class ThreadClass{ 
    //.. 
    std::mutex* mutex = nullptr; 
}; 

そしてmain中:

threadClass->mutex = &mutex; 
+0

共有ポインタが良いでしょうか? – user2807083

+0

@ user2807083:mutexの存続期間の保証に依存します。 – Jarod42

1

あなただけのstdとスレッドへの参照を渡すことができ:: refを():

#include <iostream> 
#include <thread> 
#include <mutex> 

class ThreadClass{ 
public: 
    ThreadClass(std::mutex& m) : mutex(m) {}; 
    void ThreadOutput(); 

    std::mutex& mutex; 
    int numberOfOutputsI = 0; 
}; 

void ThreadClass::ThreadOutput(){ 
    for (auto i=0; i<numberOfOutputsI; i++){ 
     std::lock_guard<std::mutex> lock(mutex); 
     std::cout << "I am a class called as a thread" << std::endl; 
    } 
} 


void thread_without_class(std::mutex &mutex); 
void thread_without_class2(int number, std::mutex &mutex); 

int main(int argc, char** argv){ 
    std::mutex mutex; 

    auto threadClass = std::make_unique<ThreadClass>(std::ref(mutex)); 

    std::thread t1(thread_without_class, std::ref(mutex)); 
    std::thread t2(thread_without_class2, 10, std::ref(mutex)); 
    std::thread t3(thread_without_class2, 11, std::ref(mutex)); 
    std::thread t4([tc = std::move(threadClass)]() { 
     tc->numberOfOutputsI = 5; 
     tc->ThreadOutput(); 
    }); 


    t1.join(); 
    t2.join(); 
    t3.join(); 
    t4.join(); 

    return 0; 
} 


void thread_without_class(std::mutex &mutex){ 
    std::lock_guard<std::mutex> lock(mutex); 
    std::cout << "c++11 thread without anything" << std::endl; 
} 

void thread_without_class2(int number, std::mutex &mutex){ 
    for (auto i=0; i<number; i++){ 
     std::lock_guard<std::mutex> lock(mutex); 
     std::cout << "c++11 thread with function parameter: " << number << std::endl; 
    } 
} 

出力例:

c++11 thread with function parameter: 10 
c++11 thread without anything 
c++11 thread with function parameter: 11 
I am a class called as a thread 
c++11 thread with function parameter: 10 
c++11 thread with function parameter: 11 
I am a class called as a thread 
c++11 thread with function parameter: 10 
c++11 thread with function parameter: 11 
I am a class called as a thread 
c++11 thread with function parameter: 10 
c++11 thread with function parameter: 11 
I am a class called as a thread 
c++11 thread with function parameter: 10 
c++11 thread with function parameter: 11 
I am a class called as a thread 
c++11 thread with function parameter: 10 
c++11 thread with function parameter: 11 
c++11 thread with function parameter: 10 
c++11 thread with function parameter: 11 
c++11 thread with function parameter: 10 
c++11 thread with function parameter: 11 
c++11 thread with function parameter: 10 
c++11 thread with function parameter: 11 
c++11 thread with function parameter: 10 
c++11 thread with function parameter: 11 
c++11 thread with function parameter: 11 
+0

ありがとうstd :: refはmutexの問題を解決しましたが、make_uniqueはC++ 14です。 –

+0

@akirahinoshiroあなたは 'std :: unique_ptr (std :: ref(m));を' std :: unique_ptr 'と置き換えることができます。 –

+0

両方の解決法はVC2015のVC2015で正常に動作します。ラムダは何か問題を起こす。 しかし、動作しているのはstd :: thread t4(&ThreadClass :: ThreadOutput、threadClass、10); ThreadOutput(int numberOfOutputsI)に対して少し変更されたThreadOutput()を使用して、 –