Click here to Skip to main content
15,390,456 members
Articles / Desktop Programming / MFC
Posted 3 Apr 2002


18 bookmarked

Managed threads in Managed C++

Rate me:
Please Sign up or sign in to vote.
3.67/5 (12 votes)
3 Apr 2002
How to create threads in Managed C++


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. 


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

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:

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

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


#include "StdAfx.h"
#using <mscorlib.dll>

using namespace System;
using namespace System::Threading;

#include <stdio.h>
#include "mythreads.h"



void CMyThreads::MyThreadProc()
    Console::WriteLine(S"Starting Thread... ");
    Console::WriteLine(S"Finishing Thread...");

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


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!


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
Web Developer
United States United States
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


Comments and Discussions

GeneralThreads in C# Pin
Anonymous18-Jun-03 21:05
MemberAnonymous18-Jun-03 21:05 
GeneralRe: Threads in C# Pin
Albert Pascual19-Jun-03 5:36
sitebuilderAlbert Pascual19-Jun-03 5:36 
GeneralRe: Threads in C# Pin
Anonymous13-Aug-03 19:20
MemberAnonymous13-Aug-03 19:20 
GeneralRe: Threads in C# Pin
Selvam R30-Dec-03 9:59
professionalSelvam R30-Dec-03 9:59 
GeneralRe: Threads in C# Pin
Wiz_Kid4-Aug-09 2:10
MemberWiz_Kid4-Aug-09 2:10 
Generalregarding thread creation Pin
Kalagotla9-Dec-02 23:50
MemberKalagotla9-Dec-02 23:50 
GeneralRe: regarding thread creation Pin
Selvam R30-Dec-03 9:55
professionalSelvam R30-Dec-03 9:55 

In Windows contains set of library files to support multithreaded applications. Check the linker in your vc++ application project.In Multithreaded application in vc++ has support LIBCMT.LIB and MSVCRT.LIB files. It Imports from MSVCRT.DLL file.

With Regards,

GeneralWrong information!!! Pin
Nish Nishant4-Apr-02 14:01
sitebuilderNish Nishant4-Apr-02 14:01 
GeneralRe: Wrong information!!! Pin
mikanu2-Aug-05 12:48
Membermikanu2-Aug-05 12:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.