Click here to Skip to main content
15,879,326 members
Articles / Programming Languages / C++/CLI
Article

How to pass data to worker threads

Rate me:
Please Sign up or sign in to vote.
5.00/5 (18 votes)
30 Oct 2001CPOL1 min read 174.5K   32   16
Shows how you can pass data to your worker threads

Introduction

There are some things about the .NET threading classes that I have not appreciated much. The first shock I got was when I realized that the System::Threading::Thread class was a sealed class.

MC++
public __gc __sealed class Thread

That means we cannot derive from the Thread class. If Thread was not sealed, then things would have been really easy. We derive a class from Thread, say MyThread. Then we add a struct member to MyThread so that we can populate our struct before starting the thread. This would have been a clean way to pass data to our thread.

As if that was not bad enough, my next shock was when I realized that the thread proc delegate would not take any parameters.

MC++
public __gc __delegate void ThreadStart();

The API function CreateThread has an LPVOID paramater using which we can pass a struct or class pointer to pass data to our threads. The CRT functions _beginthread and  _beginthreadex have a void* argument which we can use similarly.

Well so how do we get around these drawbacks. I presume that there are some very valid reasons for the Thread class and the ThreadStart delegate having the above mentioned definitions. And if someone knows why they are so, I'd be glad if you can enlighten me.

I might have missed out on something. Two likely candidates are AllocateDataSlot and AllocateNamedDataSlot. I haven't really understood their exact purpose. But I guess I'll have to search deeper into the documentation.

Anyway for now I have found the following solutions. We have our own managed class. And we add a Thread member to our class. And  in our constructor we start our thread using the Thread member. I am not sure whether this is an ideal solution. The following program listing will try and make things clear.

Program Listing

MC++
#include "stdafx.h"

#using <mscorlib.dll>

using namespace System;
using namespace System::Threading;

__gc class CalcThread
{
public:
    CalcThread(int num,String *s);
private:
    void ThreadFunc();
    Thread *t;
    int x;
    String *s1;
};

int wmain(void)
{   
    CalcThread *c1,*c2,*c3;
    Console::WriteLine("Starting thread c1");
    c1=new CalcThread(77,"c1");
    Console::WriteLine("Starting thread c2");
    c2=new CalcThread(66,"c2");
    Console::WriteLine("Starting thread c3");
    c3=new CalcThread(99,"c3");
    return 0;
}

CalcThread::CalcThread(int num, String *s)
{
    x=num;
    s1=s;
    t=new Thread(new ThreadStart(this,&CalcThread::ThreadFunc));	
    t->Start();
}

void CalcThread::ThreadFunc()
{
    Console::WriteLine("Thread {0} has initialized",s1);
    for(int i=0;i<5;i++)
    {		
        x^=i;
        Console::WriteLine("Thread {0} Intermediate Result : {1}",
            s1,__box(x));
        Thread::Sleep(500);
    }
    Console::WriteLine("Thread {0} finished with result {1}",
        s1,__box(x));
}

Output

D:\MyProjs\mcppthreads01\Debug>mcppthreads01.exe
Starting thread c1
Starting thread c2
Starting thread c3
Thread c1 has initialized
Thread c1 Intermediate Result : 77
Thread c2 has initialized
Thread c2 Intermediate Result : 66
Thread c3 has initialized
Thread c3 Intermediate Result : 99
Thread c1 Intermediate Result : 76
Thread c2 Intermediate Result : 67
Thread c3 Intermediate Result : 98
Thread c1 Intermediate Result : 78
Thread c2 Intermediate Result : 65
Thread c3 Intermediate Result : 96
Thread c1 Intermediate Result : 77
Thread c2 Intermediate Result : 66
Thread c3 Intermediate Result : 99
Thread c1 Intermediate Result : 73
Thread c2 Intermediate Result : 70
Thread c3 Intermediate Result : 103
Thread c1 finished with result 73
Thread c2 finished with result 70
Thread c3 finished with result 103

D:\MyProjs\mcppthreads01\Debug>

Thank You

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
Nish Nishant is a technology enthusiast from Columbus, Ohio. He has over 20 years of software industry experience in various roles including Chief Technology Officer, Senior Solution Architect, Lead Software Architect, Principal Software Engineer, and Engineering/Architecture Team Leader. Nish is a 14-time recipient of the Microsoft Visual C++ MVP Award.

Nish authored C++/CLI in Action for Manning Publications in 2005, and co-authored Extending MFC Applications with the .NET Framework for Addison Wesley in 2003. In addition, he has over 140 published technology articles on CodeProject.com and another 250+ blog articles on his WordPress blog. Nish is experienced in technology leadership, solution architecture, software architecture, cloud development (AWS and Azure), REST services, software engineering best practices, CI/CD, mentoring, and directing all stages of software development.

Nish's Technology Blog : voidnish.wordpress.com

Comments and Discussions

 
AnswerSystem::Threading::Thread class is a sealed class Pin
hariseos20-May-12 23:27
hariseos20-May-12 23:27 
GeneralThread waiting Pin
oleshii20-Jan-04 0:47
oleshii20-Jan-04 0:47 
QuestionPassing Data? Pin
William E. Kempf31-Oct-01 12:21
William E. Kempf31-Oct-01 12:21 
AnswerRe: Passing Data? Pin
Nish Nishant31-Oct-01 13:08
sitebuilderNish Nishant31-Oct-01 13:08 
GeneralDeriving from Thread class Pin
Daniel Turini30-Oct-01 23:36
Daniel Turini30-Oct-01 23:36 
GeneralRe: Deriving from Thread class Pin
Nish Nishant30-Oct-01 23:50
sitebuilderNish Nishant30-Oct-01 23:50 
GeneralRe: Deriving from Thread class Pin
Daniel Turini31-Oct-01 0:16
Daniel Turini31-Oct-01 0:16 
GeneralRe: Deriving from Thread class Pin
Nish Nishant31-Oct-01 0:40
sitebuilderNish Nishant31-Oct-01 0:40 
GeneralRe: Deriving from Thread class Pin
Jörgen Sigvardsson30-Oct-01 23:51
Jörgen Sigvardsson30-Oct-01 23:51 
GeneralRe: Deriving from Thread class Pin
Nish Nishant31-Oct-01 0:42
sitebuilderNish Nishant31-Oct-01 0:42 
GeneralRe: Deriving from Thread class Pin
Jörgen Sigvardsson31-Oct-01 1:10
Jörgen Sigvardsson31-Oct-01 1:10 
GeneralRe: Deriving from Thread class Pin
Nish Nishant31-Oct-01 6:24
sitebuilderNish Nishant31-Oct-01 6:24 
GeneralRe: Deriving from Thread class Pin
31-Oct-01 9:05
suss31-Oct-01 9:05 
GeneralRe: Deriving from Thread class Pin
Nish Nishant31-Oct-01 13:08
sitebuilderNish Nishant31-Oct-01 13:08 
GeneralThis time... Pin
Nish Nishant30-Oct-01 20:49
sitebuilderNish Nishant30-Oct-01 20:49 

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.