Multithreading - calling thread methods from another thread


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

class A : Thread

  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.
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,
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.

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.

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).


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.
evilrixSenior Software Engineer (Avast)Commented:
Example using Boost threads below.

You can download precompiled Windows libraries from here:

There is much more to Boost than just threads!
About Boost:
#include <iostream>
#include <string>
#include <csignal>
#include <cstdlib>
#include <boost/thread.hpp>
// Thread functor
class thread_func
	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
			// Your work goes here
		// Commit transaction here
		std::cout << "Ending thread" << std::endl;
		m_sigQuit = 0; // Reset signal
	// 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!
	std::sig_atomic_t sigQuit = 0;
	thread_func tfunc(sigQuit);
	std::cout << "Starting thread" << std::endl;
	boost::thread bt(tfunc);
	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;
	return 0;

Open in new window

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

From novice to tech pro — start learning today.