Jump to content
  • Advertisement
Sign in to follow this  
floatingwoods

2 threads alternating processing

This topic is 2534 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello,

What is the best way of implementing 2 threads that alternatively operate (but never at the same time), by reducing the waiting time to a maximum?

I though about using a CCriticalSection object, but can't get it working consistently (and fast!). Ideally, when one thread is done with its task, the other thread should do its task. When that second thread is done, then the first thread should do some other task, etc.
It is important that both threads do not waste time, except for waiting for the other thread.

Thanks for any insight

Share this post


Link to post
Share on other sites
Advertisement
Events would be my choice. You can wait on them and the threads signaling the event to each other

Share this post


Link to post
Share on other sites

What is the best way of implementing 2 threads that alternatively operate (but never at the same time), by reducing the waiting time to a maximum?


Have thread A run, and thread B wait.

If you've got two threads only working on something, but never at the same time, you're never running them concurrently... and shouldn't use threads.

Share this post


Link to post
Share on other sites
Thanks to both replies.


[quote name='floatingwoods' timestamp='1325783116' post='4900006']
What is the best way of implementing 2 threads that alternatively operate (but never at the same time), by reducing the waiting time to a maximum?


Have thread A run, and thread B wait.

If you've got two threads only working on something, but never at the same time, you're never running them concurrently... and shouldn't use threads.
[/quote]

I need two non-concurrently running threads for the transition. I have to rewrite a single-threaded application. In future the 2 threads will run concurrently, but until I finished preparing the individual API locks, this intermediate solution is best.
"Have thread A run and thread B wait" Yes! But how?
e.g.
[source]
CCriticalSection cs;

THREAD1:
while (true)
{
cs.Lock()
do some work
cs.Unlock()
}

THREAD2:
while (true)
{
cs.Lock()
do some work
cs.Unlock()
}

[/source]

Above solution works. But I don't have the guarantee that thread 1 will not run twice before thread 2 runs. I need an alternating pattern.

Share this post


Link to post
Share on other sites
Agreed, using threads is the wrong thing if you specifically want no concurrency.

Also, this does not make sense: "reducing the waiting time to a maximum". You can reduce something to a minimum or you can increase it to the maximum, those are your choices.

Edit: Okay then, use events.

Share this post


Link to post
Share on other sites
Hidden
Have one thread call function A, then function B, then function A... Or have this thread pull tasks from a Queue, whatever. But why bother putting them in threads if they're running in lock step?

Share this post


Link to post

I need an alternating pattern.


No you don't. You won't be guaranteed it in the concurrent version, so you shouldn't worry about it for the intermediate version.

Frankly I don't see any benefit of the intermediate version. You're not accomidating any concurrency for testing. You're not doing any new and interesting work.... I don't get it.


"Have thread A run and thread B wait" Yes! But how?
[/quote]


// Thread 1
while(true){
work();
}

// Thread 2
while(true){
// die in a fire.
}

Share this post


Link to post
Share on other sites

Thanks to both replies.

[quote name='Telastyn' timestamp='1325783627' post='4900012']
[quote name='floatingwoods' timestamp='1325783116' post='4900006']
What is the best way of implementing 2 threads that alternatively operate (but never at the same time), by reducing the waiting time to a maximum?


Have thread A run, and thread B wait.

If you've got two threads only working on something, but never at the same time, you're never running them concurrently... and shouldn't use threads.
[/quote]

I need two non-concurrently running threads for the transition. I have to rewrite a single-threaded application. In future the 2 threads will run concurrently, but until I finished preparing the individual API locks, this intermediate solution is best.
"Have thread A run and thread B wait" Yes! But how?
e.g.
[source]
CCriticalSection cs;

THREAD1:
while (true)
{
cs.Lock()
do some work
cs.Unlock()
}

THREAD2:
while (true)
{
cs.Lock()
do some work
cs.Unlock()
}

[/source]

Above solution works. But I don't have the guarantee that thread 1 will not run twice before thread 2 runs. I need an alternating pattern.
[/quote]

You need two locks. Make each one unlock the other. I'm not sure that's really a lock anymore, though. Make one lock start locked and make each thread depend on its own lock. You will have starvation issues, which is one reason this is a bad idea.

Share this post


Link to post
Share on other sites
Again thanks for the replies!

Telastyn: The application that used to run on a single thread now has to run on 2 threads (including external plugins). This requires many changes as you can imagine for a large application. Before there was no need at all to lock resources. With 2 threads there is a need to lock every single resource. Now, I can start to implement every single resource lock and test if the 2 threads work fine in one month. Or... I can have the intermediate solution where I start having my 2 threads run their respective code without having to worry about resource locking and have a running application now. And slowly implement resource locking and test gradually.
In addition to that, I need a "compatibility mode" where old plugins will still run with the new framework (old plugins (that I don't even have the source code for) still think there is only one thread running!). In the compatibility mode I will forbid the concurrent execution, and guarantee the alternating pattern

Well, the previous framework was ill-conceived, so yes, I have not an ideal solution.

I am compiling with MSVC and MFC, but can also compile with Mingw and Qt. So I was thinking about using CCriticalSection and QMutex

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!