|
|
Home » Community » Coffee corner » Thoughts about alternative approach to multithreading
Re: Thoughts about alternative approach to multithreading [message #19847 is a reply to message #19205] |
Tue, 27 January 2009 00:06   |
Mindtraveller
Messages: 917 Registered: August 2007 Location: Russia, Moscow rgn.
|
Experienced Contributor |

|
|
Finally I`ve finished some controller programming work and resumed developing of "alternative MT-approach" class.
Let`s summarize the idea. To avoid synchronization objects headache and possible synchronization issues it is proposed to make all the threads isolated from each other. It means that
* threads do not "see" each other`s variables (including global ones, the ones with atomic access and shared synchronization objects) - it fully conforms classic OOP approach.
* threads do not call other thread`s member functions directly
* the only possible threads interaction is made through sending some message into thread`s internal queue.
Generally this would make threads interactions far more predictable and debuggable.
After some thinking I`ve made a decision not to send messages but instead send requests to run thread public member function (callback). It was decided because "message" is actually a signal to do some job. So it is no need to create any artificial message identifiers while you directly request to execute these callbacks.
It looks like this:
class JobThread1 : public CallbackThread
{
public: //these members may be added into thread queue and must not be called directly
void Job11();
void Job12(const String &);
private: //all the realization details are private
//...
};
class class JobThread2 : public CallbackThread
{
public: //these members may be added into thread queue and must not be called directly
void Job21(const String &);
private: //all the realization details are private
//...
};
CONSOLE_APP_MAIN
{
JobThread1 jobs1;
JobThread2 jobs2;
for (int i=0; i<10; ++i)
{
jobs1.Add(&JobThread1::Job11);
jobs1.Add(&JobThread1::Job12, FormatInt(i));
jobs2.Add(&JobThread2::Job21, FormatIntHex(i));
}
};
You may even treat main thread as the same alt-MT thread. To do this you may have CallbackQueue variable and request it`s tasks. These tasks are processed with CallbackQueue::DoTasks().
Below is ready-to-use class with a simple test code.
If you are interested you may download and test it, or even use it in your apps. I hope more advanced versions of class will come soon (a number of optimizations is yet to be made).
"Alternative" MT requires a bit of reengineering threads processing functions. To make alt-MT program, you should think differently. Now you can`t share variables, now you don`t have a number of routines which are called in unpredictable sequence.
Instead you have messaging queues which mustn`t hold big number of messages (it is possible though not recommended). This means that thread must be logically solid as much as possible. This would keep all "tiny" interactions inside one thread.
Of course there are situations where "classic" MT with sync objects fits better. Situations include threads exchange with a huge number of tiny messages (inc/dec some variable). So if you cannot divide threads interaction into a (relatively small) number of (relatively medium) jobs - use "classic" MT.
I mean if you use more than 100`000 of messages per second - just use sync objects instead. But now you at least may choose to think "new way" on reorganizing threads member functions to make program stable or continue interacting with shared global variables to make it possibly quicker and less memory consuming but harder to debug and potentially less stable.
[Updated on: Tue, 27 January 2009 09:11] Report message to a moderator
|
|
|
 |
|
Thoughts about alternative approach to multithreading
|
 |
|
Re: Thoughts about alternative approach to multithreading
By: mr_ped on Tue, 14 October 2008 08:27
|
 |
|
Re: Thoughts about alternative approach to multithreading
|
 |
|
Re: Thoughts about alternative approach to multithreading
By: mirek on Wed, 15 October 2008 11:59
|
 |
|
Re: Thoughts about alternative approach to multithreading
By: zsolt on Wed, 15 October 2008 21:49
|
 |
|
Re: Thoughts about alternative approach to multithreading
|
 |
|
Re: Thoughts about alternative approach to multithreading
By: zsolt on Thu, 16 October 2008 11:38
|
 |
|
Re: Thoughts about alternative approach to multithreading
|
 |
|
Re: Thoughts about alternative approach to multithreading
By: mirek on Thu, 16 October 2008 15:04
|
 |
|
Re: Thoughts about alternative approach to multithreading
|
 |
|
Re: Thoughts about alternative approach to multithreading
By: mirek on Fri, 14 November 2008 10:15
|
 |
|
Re: Thoughts about alternative approach to multithreading
|
 |
|
Re: Thoughts about alternative approach to multithreading
|
 |
|
Re: Thoughts about alternative approach to multithreading
By: mirek on Tue, 18 November 2008 20:05
|
 |
|
Re: Thoughts about alternative approach to multithreading
|
 |
|
Re: Thoughts about alternative approach to multithreading
By: mirek on Wed, 19 November 2008 10:39
|
 |
|
Re: Thoughts about alternative approach to multithreading
|
 |
|
Re: Thoughts about alternative approach to multithreading
By: mirek on Wed, 19 November 2008 19:38
|
 |
|
Re: Thoughts about alternative approach to multithreading
|
 |
|
Re: Thoughts about alternative approach to multithreading
By: mirek on Thu, 20 November 2008 10:42
|
 |
|
Re: Thoughts about alternative approach to multithreading
|
 |
|
Re: Thoughts about alternative approach to multithreading
|
 |
|
Re: Thoughts about alternative approach to multithreading
By: mirek on Fri, 03 July 2009 18:49
|
 |
|
Re: Thoughts about alternative approach to multithreading
|
 |
|
Re: Thoughts about alternative approach to multithreading
By: mirek on Sun, 05 July 2009 22:08
|
 |
|
Re: Thoughts about alternative approach to multithreading
|
 |
|
Re: Thoughts about alternative approach to multithreading
By: mirek on Sun, 12 July 2009 08:37
|
 |
|
Re: Thoughts about alternative approach to multithreading
By: mirek on Sun, 05 July 2009 22:10
|
 |
|
Re: Thoughts about alternative approach to multithreading
By: mr_ped on Tue, 18 November 2008 09:05
|
Goto Forum:
Current Time: Fri Sep 05 12:43:29 CEST 2025
Total time taken to generate the page: 0.07285 seconds
|
|
|