Solved

How to restart a thread in mfc

Posted on 2007-11-27
5
731 Views
Last Modified: 2013-11-20
All,
Background:
I have an application that collects data from sensors on several com ports. Once all the data is collected I "do something" with it. Each sensor is interogated sequentially, based on its positon in a database. I have been tasked to collect data using multiple threads (1 for each com port that is active). My experience with threads is limited to a few worker threads here and there.

What I have done:
Created a class called "CComDataThread" that has the thread function
On start of program:
for(int i = 0; i < numberofComPortsInUse; i++)
{
    create a new comdatathread object;
    do some class init stuff
    set the thread autodelete to false
    create the thread suspended

    add the thread object to an object array for use later on when I need to collect data
   - a "poor mans" thread pool. (I don't know if this is the correct way to go about this yet...)
}

At some time later to collect data:
//for(int t = 0; t < threadpool.GetCount(); t++)  - just test one thread now
for(int t = 0; t < 1; t++)
{
   // get the thread object out of the array
   CComDataThread* pThread = (CComDataThread*)ThreadPool->GetAt(t);
    pThread->ResumeThread();
   
    DWORD wait =   WaitForSingleObject(pThread->m_hThread, INFINITE);
    if(wait == WAIT_OBJECT_0)
        break;   // thread has exited go do something with the data
}

This actually works - the thread launches and data collection occurs. The problem is when I need to colect data again - the thread won't run again.

I have a lot to learn about this stuff and I'm sure I'll be posting a lot more questions re multithreading, but for now all I want to do is figure out how to get my thread(s) to run and rerun...

(I can attach real code if needed)

Thanks
John


   


0
Comment
Question by:jhorstkamp
  • 2
  • 2
5 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20361061
this won't work because thread has ended. Just work when you Suspend a thread.
You have to create a new thread.
0
 

Author Comment

by:jhorstkamp
ID: 20362027
Really?
Then what's the use of having a thread pool where all the threads are created suspended and then each thread is let run whenever needed? (Or do I completely misunderstand this?)
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 200 total points
ID: 20362102
Have a look to this article: http://en.wikipedia.org/wiki/Thread_pool
The first paragraph says:
In the thread pool pattern in programming, a number of threads are created to perform a number of tasks, usually organized in a queue. Typically, there are many more tasks than threads. As soon as a thread completes its task, it will request the next task from the queue until all tasks have been completed. The thread can then terminate, OR SLEEP UNTIL THERE ARE NEW TASKS AVAILABLE.

so, you don't terminate a thread and launch again, just make it sleep with Suspend(), or better, the own thread sleeps (with Sleep() or other function) until it detects more data to process.
The goal of the pool is to keep track of the threads, create and destroy in a oportune moment.
0
 

Author Closing Comment

by:jhorstkamp
ID: 31411286
Thanks. I got laid off from the job that had that particular problem and lost interest and forgot about it...Good info for future use though.
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Excel Use VBA to get user's Mac Address for their computer 5 182
fix34  challenge 9 123
Sed question 2 87
Change to event 1 115
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question