用.NET进行并行编程 [英] Parallel Programming with .NET

查看:64
本文介绍了用.NET进行并行编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我提出了我认为在任何.NET语言中编写多线程

编程更容易的好主意。我不知道在哪里发布它,

所以我会在这里试试。


..NET 2.0增加了编写匿名函数的能力,它会很好

如果有并行的话语句,可以简化编写threadprocs。

例如(理论c#)

public void DoSomeParallelStuff(){

parallel

{

{

//做一些网络的东西

//这是一个匿名的ThreadProc

}

{

//做一些IO的东西

}

{

//做一些DB的东西

}

}


//在调用者线程上做其他事情

}

编译器可能在幕后创建匿名函数,通过调用ThreadPool.QueueUserWorkItem将它们添加到ThreadPool中。在某些情况下

自动阻止调用者线程会很好,也许是姐姐

语句并行阻止

parrallel-阻止{

{

//从驱动器c读取:

StreamReader rd = new StreamReader(" C:\ test.txt" );

...

}

{

//从驱动器d读取:(并行完成) )

StreamReader rd = new StreamReader(" D:\ test.txt");

...

}

}


//线程阻塞,直到所有并行匿名函数完成

Console.WriteLine(任务完成);


我认为这样可以更容易编写多线程代码。我已经实际将这个功能添加到我正在进行的编译器中,并且它很有效。

很好。

I came up with what I think is a good idea for making multithreading
programming easier in any .NET language. I dont know where else to post it,
so I''ll try here.

..NET 2.0 adds the capability to write anonymous functions, it would be nice
if there was a "parallel" statement, that could simplify writing threadprocs.
e.g. (theoretical c#)
public void DoSomeParallelStuff() {
parallel
{
{
// do some network stuff
// this is an anonymous ThreadProc
}
{
// do some IO stuff
}
{
// do some DB stuff
}
}

// do other stuff, on caller thread
}
behind the scenes the compiler could create the anonymous functions, add
them to the ThreadPool by calling ThreadPool.QueueUserWorkItem. in some cases
it would be nice to automatically block the caller thread, maybe a sister
statement "parallel-blocked"
parrallel-blocked {
{
// read from drive c:
StreamReader rd = new StreamReader("C:\test.txt");
...
}
{
// read from drive d: (done in parallel)
StreamReader rd = new StreamReader("D:\test.txt");
...
}
}

// thread blocks until all parallel anonymous functions complete
Console.WriteLine("Tasks complete");

I think this would make it much easier to write multithreaded code. I''ve
actually added this feature to a compiler I''m working on and it works quite
nicely.

推荐答案

添加''YouKnowWhatIMean''和''HereAMiracleOccurs'',而你的'。'
" Joshua Nussbaum" < Joshua Nu******@discussions.microsoft.com >写在

消息新闻:2B ********************************** @ microsof t.com ...
Add the ''YouKnowWhatIMean'' and ''HereAMiracleOccurs'' while your''e at it.
"Joshua Nussbaum" <Joshua Nu******@discussions.microsoft.com> wrote in
message news:2B**********************************@microsof t.com...
我提出了我认为在任何.NET语言中使多线程编程更容易编程的好主意。我不知道在哪里发布
它,
所以我会在这里尝试。

.NET 2.0增加了编写匿名函数的能力,它将是
好的
如果有平行的话声明,可以简化写作
threadprocs。
例如(理论c#)

public void DoSomeParallelStuff(){
parallel
{
//做一些网络内容
//这个是一个匿名的ThreadProc
}
//做一些IO的东西
}
//做一些数据库的东西
} < //其他东西,在调用者线程上
}

幕后编译器可以创建匿名函数,添加
通过调用ThreadPool.QueueUserWorkItem将它们传递给ThreadPool。在某些情况下
自动阻止调用者线程会很好,也许是姐妹
语句并行阻塞

parrallel-blocked {
{
//从驱动器c读取:
StreamReader rd = new StreamReader(" C:\ test.txt");
...
}
{
//从驱动器读取d :(并行完成)
StreamReader rd = new StreamReader(" D:\test.txt");
...


//线程阻塞直到所有并行匿名函数完成
Console.WriteLine(任务完成);

我认为这会使编写多线程代码变得更容易。我已经将这个功能添加到我正在处理的编译器中,并且很好地工作了。
I came up with what I think is a good idea for making multithreading
programming easier in any .NET language. I dont know where else to post
it,
so I''ll try here.

.NET 2.0 adds the capability to write anonymous functions, it would be
nice
if there was a "parallel" statement, that could simplify writing
threadprocs.
e.g. (theoretical c#)
public void DoSomeParallelStuff() {
parallel
{
{
// do some network stuff
// this is an anonymous ThreadProc
}
{
// do some IO stuff
}
{
// do some DB stuff
}
}

// do other stuff, on caller thread
}
behind the scenes the compiler could create the anonymous functions, add
them to the ThreadPool by calling ThreadPool.QueueUserWorkItem. in some
cases
it would be nice to automatically block the caller thread, maybe a sister
statement "parallel-blocked"
parrallel-blocked {
{
// read from drive c:
StreamReader rd = new StreamReader("C:\test.txt");
...
}
{
// read from drive d: (done in parallel)
StreamReader rd = new StreamReader("D:\test.txt");
...
}
}

// thread blocks until all parallel anonymous functions complete
Console.WriteLine("Tasks complete");

I think this would make it much easier to write multithreaded code. I''ve
actually added this feature to a compiler I''m working on and it works
quite
nicely.


不是你要求的,但是,如果你去
http://research.microsoft.com/resear...s/default.aspx 并下载



" C#Software Transactional Memory你会找到一些帮助来创建

并行线程系统。


MSR非常酷。

-

--- Nick Malik [微软]

MCSD,CFPS,认证Scrummaster
http://blogs.msdn.com/nickmalik


免责声明:本论坛中表达的观点是我自己的,而不是

代表我的雇主。

我不代表我的雇主回答问题。我只是一个帮助程序员的
程序员。

-

" Joshua Nussbaum" < Joshua Nu******@discussions.microsoft.com >写在

消息新闻:2B ********************************** @ microsof t.com ...
Not quite what you asked for but, if you go to
http://research.microsoft.com/resear...s/default.aspx and download
the bits for
"C# Software Transactional Memory" you will find some help for creating
parallel threaded systems.

MSR is very cool.
--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I''m just a
programmer helping programmers.
--
"Joshua Nussbaum" <Joshua Nu******@discussions.microsoft.com> wrote in
message news:2B**********************************@microsof t.com...
我提出了我认为在任何.NET语言中使多线程编程更容易编程的好主意。我不知道在哪里发布
它,
所以我会在这里尝试。

.NET 2.0增加了编写匿名函数的能力,它将是
好的
如果有平行的话声明,可以简化写作
threadprocs。
例如(理论c#)

public void DoSomeParallelStuff(){
parallel
{
//做一些网络内容
//这个是一个匿名的ThreadProc
}
//做一些IO的东西
}
//做一些数据库的东西
} < //其他东西,在调用者线程上
}

幕后编译器可以创建匿名函数,添加
通过调用ThreadPool.QueueUserWorkItem将它们传递给ThreadPool。在某些情况下
自动阻止调用者线程会很好,也许是姐妹
语句并行阻塞

parrallel-blocked {
{
//从驱动器c读取:
StreamReader rd = new StreamReader(" C:\ test.txt");
...
}
{
//从驱动器读取d :(并行完成)
StreamReader rd = new StreamReader(" D:\test.txt");
...


//线程阻塞直到所有并行匿名函数完成
Console.WriteLine(任务完成);

我认为这会使编写多线程代码变得更容易。我已经将这个功能添加到我正在处理的编译器中,并且很好地工作了。
I came up with what I think is a good idea for making multithreading
programming easier in any .NET language. I dont know where else to post
it,
so I''ll try here.

.NET 2.0 adds the capability to write anonymous functions, it would be
nice
if there was a "parallel" statement, that could simplify writing
threadprocs.
e.g. (theoretical c#)
public void DoSomeParallelStuff() {
parallel
{
{
// do some network stuff
// this is an anonymous ThreadProc
}
{
// do some IO stuff
}
{
// do some DB stuff
}
}

// do other stuff, on caller thread
}
behind the scenes the compiler could create the anonymous functions, add
them to the ThreadPool by calling ThreadPool.QueueUserWorkItem. in some
cases
it would be nice to automatically block the caller thread, maybe a sister
statement "parallel-blocked"
parrallel-blocked {
{
// read from drive c:
StreamReader rd = new StreamReader("C:\test.txt");
...
}
{
// read from drive d: (done in parallel)
StreamReader rd = new StreamReader("D:\test.txt");
...
}
}

// thread blocks until all parallel anonymous functions complete
Console.WriteLine("Tasks complete");

I think this would make it much easier to write multithreaded code. I''ve
actually added this feature to a compiler I''m working on and it works
quite
nicely.


< =?Utf-8?B?Sm9zaHVhIE51c3NiYXVt?=< Joshua
Nu *** ***@discussions.microsoft.com >>写道:


< snip>
<=?Utf-8?B?Sm9zaHVhIE51c3NiYXVt?= <Joshua
Nu******@discussions.microsoft.com>> wrote:

<snip>
我认为这样可以更容易编写多线程代码。我已经将这个功能添加到我正在处理的编译器中并且它非常好用。
I think this would make it much easier to write multithreaded code. I''ve
actually added this feature to a compiler I''m working on and it works quite
nicely.




我不喜欢我知道 - 我不能想到很多情况下我已经使用过多元线程,而不是明确地做更多的事情。

。 />

我更喜欢这种语言保持良好和小巧,没有太多的东西

这是特定于线程的。 (甚至锁定本来可以更好地使用使用模式,IMO。)


-

Jon Skeet - < sk *** @ pobox.com>
http ://www.pobox.com/~skeet

如果回复小组,请不要给我发邮件



I don''t know - I can''t think of many situations where I''ve used
multithreading where it would be more readable than doing things more
explicitly.

I prefer the language to remain nice and small, not having much stuff
which is threading-specific. (Even "lock" would have been better to use
the "using" pattern, IMO.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


这篇关于用.NET进行并行编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆