1.

How Can A C++11 Thread Be Created With A Member Function?

Answer»

#include "stdafx.h"

#include <thread&GT;

#include <iostream>

using NAMESPACE std;

class Torpedo

{

PUBLIC:

void LaunchTorpedo()

{

cout << " Launching Torpedo" << endl;

}

};

int MAIN()

{

//Execute the LaunchTorpedo() method for a SPECIFIC Torpedo object on a seperate thread

Torpedo torpedo;

thread t1(&Torpedo::LaunchTorpedo, &torpedo);

t1.join();

return 0;

}

Note that here you're executing the LaunchTorpedo() method for a specific Torpedo object on a seperate thread. If other threads are accessing the same "torpedo" object, you'll need to protect the shared resources of that object with a mutex.

#include "stdafx.h"

#include <thread>

#include <iostream>

using namespace std;

class Torpedo

{

public:

void LaunchTorpedo()

{

cout << " Launching Torpedo" << endl;

}

};

int main()

{

//Execute the LaunchTorpedo() method for a specific Torpedo object on a seperate thread

Torpedo torpedo;

thread t1(&Torpedo::LaunchTorpedo, &torpedo);

t1.join();

return 0;

}

Note that here you're executing the LaunchTorpedo() method for a specific Torpedo object on a seperate thread. If other threads are accessing the same "torpedo" object, you'll need to protect the shared resources of that object with a mutex.



Discussion

No Comment Found