Click here to Skip to main content
6,596,602 members and growing! (23,283 online)
Email Password   helpLost your password?
General Programming » Threads, Processes & IPC » Threads     Intermediate

Managed threads in Managed C++

By Albert Pascual

How to create threads in Managed C++
C++/CLI, VC6, VC7, .NET, Win2K, WinXP, Visual Studio, MFC, Dev
Posted:3 Apr 2002
Views:71,150
Bookmarked:13 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
61 votes for this article.
Popularity: 6.42 Rating: 3.60 out of 5
3 votes, 25.0%
1
1 vote, 8.3%
2

3
1 vote, 8.3%
4
7 votes, 58.3%
5

Introduction

Using Managed C++ to create threads is not as easy as C#. If you already tried to compile a Managed C++ program with threads, you know what I am talking about. 

Details

First, do not try to add the thread in the same class, it won't compile. The easiest way I personally found is to create a new managed class ( remember to add __gc in the beginning of the class declaration ). In these instance we'll call it CMyThreads. Why not? Every program should have a CMySomething as a class.

#pragma once

__gc class CMyThreads
{
public:
CMyThreads(void);
~CMyThreads(void);

void MyThreadProc();
void AddArguments(void* pArg1, void* pArg2)
void * m_FirstArgument ;
void * m_SecondArgument ;
};

One problem in managed C++ threads is the arguments. You must create a function to call before starting the thread if you want arguments. (See AddArguments above)

Calling the thread from another class:

foo()
{
    CMyThreads * pMyThread;
    pMyThread = new CMyThreads;
    pMyThread->AddArguments(Argument1, Argument2);
    ThreadStart * pThread = new ThreadStart(pMyThread, &CMyThreads::MyThreadProc);
    Thread *oThread = new Thread(pThread);
    oThread->Start();
}

Before we create ThreadStart you must call AddArguments if you want arguments on this thread. The thread will not begin until you call the member function Start()

#include "StdAfx.h"

#using <mscorlib.dll>

using namespace System;
using namespace System::Threading;

#include <stdio.h>

#include "mythreads.h"



CMyThreads::CMyThreads(void)
{
}

CMyThreads::~CMyThreads(void)
{
}

void CMyThreads::MyThreadProc()
{
    Console::WriteLine(S"Starting Thread... ");
    Thread::Sleep(5);
    pClass->ExternalFunction(/*Arguments*/);
    Console::WriteLine(S"Finishing Thread...");
}

void CMyThreads::AddArguments(void* pArg1, void* pArg2)
{
    m_FirstArgument = pArg1;
    m_SecondArgument = pArg2;
}

Conclusion

Remember to Sleep to allow the main process to continue. Also you put anything you like in MyThreadProc() you can also call a function in another class. I hope you have fun!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Albert Pascual


Member
Al is just another Software Engineer working in C++, ASp.NET and C#. Enjoys snowboarding in Big Bear, and wait patiently for his daughters to be old enough to write code and snowboard.

Al is a Microsoft ASP.NET MVP

Blog
Occupation: Web Developer
Location: United States United States

Other popular Threads, Processes & IPC articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 9 of 9 (Total in Forum: 9) (Refresh)FirstPrevNext
GeneralThreads in C# PinsussAnonymous22:05 18 Jun '03  
GeneralRe: Threads in C# PinmemberAlbert Pascual6:36 19 Jun '03  
GeneralRe: Threads in C# PinsussAnonymous20:20 13 Aug '03  
GeneralRe: Threads in C# PinmemberR.selvam10:59 30 Dec '03  
GeneralRe: Threads in C# PinmemberHaroon Alvi3:10 4 Aug '09  
Generalregarding thread creation PinsussKalagotla0:50 10 Dec '02  
GeneralRe: regarding thread creation PinmemberR.selvam10:55 30 Dec '03  
GeneralWrong information!!! PinmemberNish [BusterBoy]15:01 4 Apr '02  
GeneralRe: Wrong information!!! Pinmembermikanu13:48 2 Aug '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 3 Apr 2002
Editor: Nishant Sivakumar
Copyright 2002 by Albert Pascual
Everything else Copyright © CodeProject, 1999-2009
Web17 | Advertise on the Code Project