?
Solved

MultiThreading in W32 Applications in C++

Posted on 2003-03-18
4
Medium Priority
?
222 Views
Last Modified: 2010-04-01
Hi all,

I'm triyng to make a application using threads, some one know a simple way to do it??


ThX

NeuralC
0
Comment
Question by:neuralc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 1

Accepted Solution

by:
Dikran earned 100 total points
ID: 8159126
Hello dear neuralc.

Below is the basic pattern that you can use to resolve your problems:
1.You have to create the class(for example CMyThread):

class CMyThread
{
  HANDLE m_hMyProc; //the handel of the thread
  DWORD  m_ProcId; //ID of the thread
public:
   void Start(); //is the function that will create the thread.
   void Run(); //is the callback function that will performe your precefic user code.
}
///////////////////////////////////////////////////////////
void CMyThread::Start()
{
m_hMyProc = CreateThread( NULL, 0, MyProc, (LPVOID)this, CREATE_SUSPENDED,&m_ProcId);
     if(m_hRxFlowProc == NULL)
     {
          Error();
        }
ResumeThread(m_hMyProc);
}
void CMyThread::Run()
{
   Sleep(1000); //1 second
   printf("Hello frmo thread %d",m_ProcId);
}

2. Then you have to specify the Thread procedure:(MyProc)
DWORD WINAPI MyProc(void* p)
{
        CMyThread* pMyThread = (CMyThread*)p;
     pMyThread->Run();
     return 0;
}
 
I think that you can use this basic draft you can solve your problem.

Regards
Dikran.
0
 
LVL 2

Assisted Solution

by:MirkoLMV
MirkoLMV earned 100 total points
ID: 8177797
An important detail is that the main thread is always functional by default, and therefore is not mentioned at all. Only additional threads are declared (a second, third and so one) and operated in the manner specific to threads (started, stopped, provided with procedure.
There comes a simple Windows application for testing a thread :

#include <windows.h>
#include <stdio.h>

void fnCountThread(void) ;
volatile UINT iCount = 0 ;
CHAR szMessage[256] ;

void fnCountThread() {
 while(1) {iCount++ ; Sleep(100); }
 } // EdOf fnCountThread Function

int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
{
 char c = 5 ;
 HANDLE hthread ;
 DWORD dwThreadID ;
 hthread = CreateThread(0, 0,
  (LPTHREAD_START_ROUTINE) fnCountThread,
   0, 0, &dwThreadID);
 if (!hthread) MessageBox (NULL,
  "Failed to create the Thread", "Single-Thread Application",
  MB_OK|MB_ICONEXCLAMATION) ;
 while (c--) {
  sprintf (szMessage, "The Count is %d now.", iCount) ;
  MessageBox (NULL, szMessage, "Single-threaded",
   MB_OK|MB_ICONINFORMATION) ; }
 return 0 ;
 } // EndOf WinMain Function
// EOF
I have mentioned it for an additional example, and I hope that it's study would clarify some aspects of threads use.

Good luck !

 
0
 
LVL 9

Expert Comment

by:tinchos
ID: 9510299
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Split Points between Dikran & MirkoLMV

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0
 
LVL 2

Expert Comment

by:MirkoLMV
ID: 9558521
I am glad that I was of a help.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses

765 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