带有共享互斥锁和类实例的C ++ 11线程

时间:2016-04-07 09:26:56

标签: c++ multithreading c++11

您好我正在尝试使用C ++ 11和线程。我面临一些问题,我无法弄清楚如何解决。我想在另一个线程中做一个类的东西,需要在几个函数和类中使用互斥。 是的我知道std :: mutex不可复制,但我不知道如何解决我的问题。

到目前为止我所拥有的是什么 main.cpp中:

duration += speech.duration;

ThreadClass.h:

#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.cpp

#pragma once
#include <mutex>

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

std::mutex mutex;
int numberOfOutputsI;
};

2 个答案:

答案 0 :(得分:3)

您不想复制,而是引用/指针:

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

main

threadClass->mutex = &mutex;

答案 1 :(得分: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