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.
Brent-CampbellAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.