• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 870
  • Last Modified:

Multithreading - calling thread methods from another thread

Hi,

Hypathetically, I have one main application thread that spawns another thread object something along the lines of:

class A : Thread
{
public:
  A();

public:
  void start();
  void run();
  void stop();

  void compute();
};

Now after the thread has been created in my main application thread, I decide somewhere I want to call the second threads compute method which takes a very long time to finish, would calling a.compute() from my main application context hang my main application? or is it better that I implement a while(1) in run() which checks a variable that the main application sets?

Many thanks.
0
Brent-Campbell
Asked:
Brent-Campbell
  • 3
2 Solutions
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi Brent-Campbell,

> would calling a.compute() from my main application...

Calling a.compute() from the main application WOULD hang the main application.

But that's not how a thread should be designed.  The second thread should be designed to run the compute() method.  The main thread then sets up the second thread and issues a Resume() to assign the CPU to the thread.

You can use any signalling mechanism you like (perhaps setting a variable) to detect that the thread has completed.


Good Luck,
Kent
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> perhaps setting a variable
Be careful with just using a variable, there are two things to consider...
a) Will changes in one thread be seen in another?
Um, probably not unless you declare the variable volatile.
http://www.devx.com/tips/Tip/15424

b) Can the variable be written to and read in one instruction (atomic read/write)
Maybe, it depends on the type. To be sure always use sig_atomic_t, which is a type defined by the standard to always have these semantics.
http://www.cplusplus.com/reference/clibrary/csignal/sig_atomic_t.html

What you are talking about here is a worker thread. The idea is your main thread should spin up a worker thread and give it a function to execute, which will be the work it should do -- in this case the function would be compute() (although not a class member function as the function would need to have an address that doesn't need to be bound to a this pointer).

-Rx.
0
 
evilrixSenior Software Engineer (Avast)Commented:
NB. By far the simplest (x-platform) way to implement threads (IMO) is to use Boost threads.
http://www.boost.org/doc/html/thread.html
0
 
evilrixSenior Software Engineer (Avast)Commented:
Example using Boost threads below.

You can download precompiled Windows libraries from here: http://www.boost-consulting.com/products/free

There is much more to Boost than just threads!
About Boost: http://www.boost-consulting.com/products/free
#include <iostream>
#include <string>
#include <csignal>
#include <cstdlib>
#include <boost/thread.hpp>
 
// Thread functor
class thread_func
{
public:
	thread_func(std::sig_atomic_t & sigQuit): m_sigQuit(sigQuit){}
 
	void operator()()
	{
		m_sigQuit = 0; // Reset signal
 
		std::cout << "Running thread, doing some work" << std::endl;
 
		// Start transaction here
 
		while(!m_sigQuit)
		{
			// Your work goes here
			boost::thread::yield();
		}
 
		// Commit transaction here
 
		std::cout << "Ending thread" << std::endl;
 
		m_sigQuit = 0; // Reset signal
	}
 
private:
	// Used to signal thread to quit -- must be a reference!!!
	std::sig_atomic_t & m_sigQuit;
};
 
int main()
{
	// The type sig_atomic_t is thread safe!
	// http://www.cplusplus.com/reference/clibrary/csignal/sig_atomic_t.html
	std::sig_atomic_t sigQuit = 0;
	thread_func tfunc(sigQuit);
 
	std::cout << "Starting thread" << std::endl;
	boost::thread bt(tfunc);
 
	system("pause");
 
	std::cout << "Terminating thread" << std::endl;
	
	sigQuit = 1; // Signal thread to stop
	bt.join(); // Wait for thread to re-join
 
	std::cout << "Terminating process" << std::endl;
 
	system("pause");
 
	return 0;
}

Open in new window

0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now