|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionThere 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
public __gc __sealed class Thread
That means we cannot derive from the As if that was not bad enough, my next shock was when I realized that the thread proc delegate would not take any parameters. public __gc __delegate void ThreadStart();
The API function Well so how do we get around these drawbacks. I presume that there are some
very valid reasons for the I might have missed out on something. Two likely candidates are
Anyway for now I have found the following solutions. We have our own managed
class. And we add a Program Listing#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));
}
OutputD:\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
|
||||||||||||||||||||||