[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 238
  • Last Modified:

MultiThreading in W32 Applications in C++

Hi all,

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


ThX

NeuralC
0
neuralc
Asked:
neuralc
  • 2
2 Solutions
 
DikranCommented:
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
 
MirkoLMVCommented:
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
 
tinchosCommented:
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
 
MirkoLMVCommented:
I am glad that I was of a help.
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now