I think issue is that you cant enter so match parameters. I propose that you use the Multithreading of C++11
MyThread.h
#ifndef MyTHREAD_HPP
#define MyTHREAD_HPP
#include <thread>
#include <mutex>
class MyThread {
public:
MyThread();
void PrintOutMessage(string msg);
void PrintOut(int i, int j, int k);
void PrintOut(int i, int j);
~MyThread();
};
#endif
MyThread.cpp
#include "MyThread.hpp"
mutex _mu, _mu2;
MyThread::MyThread()
{
}
MyThread::~MyThread()
{
}
void MyThread::PrintOutMessage(string msg)
{
for (int l = 0; l < 10; l++)
{
lock(_mu, _mu2);
lock_guard<mutex>locker(_mu2, adopt_lock);
lock_guard<mutex>locker2(_mu, adopt_lock);
cout<< "message:" << msg << endl;
Sleep(200);
}
}
void MyThread::PrintOut(int i,int j,int k)
{
for (int l = 0; l < 10; l++)
{
lock(_mu, _mu2);
//lock_guard<mutex>locker(_mu2, adopt_lock);
//lock_guard<mutex>locker2(_mu, adopt_lock);
unique_lock<mutex>locker(_mu2, adopt_lock);
unique_lock<mutex>locker2(_mu, adopt_lock);
cout << "message:" << i << ", " << j << ", " << k << endl;
Sleep(200);
}
}
void MyThread::PrintOut(int i, int j)
{
for (int l = 0; l < 10; l++)
{
lock(_mu, _mu2);
lock_guard<mutex>locker(_mu2, adopt_lock);
lock_guard<mutex>locker2(_mu, adopt_lock);
cout << "message:" << i << ", " << j << endl;
Sleep(200);
}
}
main.cpp
#include "MyThread.hpp"
void (MyThread::*memfunc)(int,int) = &MyThread::PrintOut;
void (MyThread::*memfunc2)(int, int,int) = &MyThread::PrintOut;
void PrintOutmainfunc(int i, int j, int k)
{
for (int l = 0; l < 10; l++)
{
cout << "message:" << i << ", " << j << ", " << k << endl;
Sleep(200);
}
}
int main (){
string msg = " hello world";
int i = 10, j = 20, k = 30;
// string str = lexical_cast<string>(i);
//cout << "str: " << str << endl;
MyThread mythread;
thread t1(memfunc, mythread, i, j);
thread t2(memfunc2, mythread, i, j, k);
thread t3(&MyThread::PrintOutMessage, mythread, msg);
//thread t4(PrintOutmainfunc, i, j, k);
//thread t3(bind(&MyThread::PrintOutMessage, mythread, msg));
t1.join();
t2.join();
t3.join();
//t4.join();
system("PAUSE");
return 0;
}
in this example I putted all possible options for Multithreading