Http://www.cnblogs.com/tianzhiliang/archive/2010/08/31/1813629.html
What is the difference between asynchronous and multithreading in C? Both asynchronous and multithreading can avoid calling thread blocking and improve software responsiveness. Sometimes we think that Asynchronization and multithreading are equivalent. However, there are some differences between asynchronous and multithreading. These differences lead to the difference in the time to use Asynchronous and multithreading.
Differences between asynchronous and multithreading: the nature of asynchronous operations
Institute
Some programs will eventually be executed by computer hardware. Therefore, in order to better understand the nature of asynchronous operations, we need to understand its hardware base.
Friends who are familiar with computer hardware must be familiar with the DMA term. The technical specifications of hard disks and optical drives all have clear DMA mode indicators. In fact, NICS, sound cards, and video cards also have DMA functions. DMA is straight
Memory Access means that hardware with DMA functions can exchange data with memory without consuming CPU resources. The hardware is enabled as long as the CPU sends a command during data transmission.
The hardware will trigger an interruption after the transmission is complete to notify the operation to complete. These I/O operations that do not consume CPU time are the hardware basis of asynchronous operations. So even in DOS
Such single-process (and wireless) systems can also initiate asynchronous DMA operations.
Difference between Asynchronization and multithreading: thread nature
A thread is not a computer hardware function, but a logic function provided by the operating system. A thread is essentially a piece of code that runs concurrently in a process, therefore, threads require the operating system to invest CPU resources for running and scheduling.
Differences between asynchronous and multithreading Advantages and Disadvantages of asynchronous operations
Because
For asynchronous operations, there is no additional thread burden, and callback is used for processing. Under well-designed conditions, the processing function does not need to use shared variables (even if it cannot be completely used, at least can be reduced
The number of shared variables) to reduce the possibility of deadlocks. Of course, asynchronous operations are not perfect. The programming of asynchronous operations is highly complex. The program mainly uses the callback method for processing.
It is difficult to debug.
Differences between Asynchronization and multithreading Advantages and Disadvantages of Multithreading
The advantages of multithreading are obvious. The processing program in the thread is still executed in sequence, which conforms to the thinking habits of ordinary people, So programming is simple. However, the disadvantages of multithreading are also obvious. The use (abuse) of threads will impose additional burden on context switching. In addition, shared variables between threads may cause deadlocks.
Applicability
In
After understanding the advantages and disadvantages of threads and asynchronous operations, we can discuss the rational use of threads and asynchronous operations. In my opinion, when I/O operations need to be executed, asynchronous operations are better than thread + synchronization.
I/O operations are more appropriate. I/O operations include not only direct file and network read/write operations, but also database operations, web services, httprequest, And. net
Remoting and other cross-process calls.
The applicability of threads is those that require long CPU operations, such as time-consuming graphics processing and Algorithm Execution. However
Due to the simplicity and habit of using thread programming, many friends often use threads to execute time-consuming I/O operations. In this way, when there are only a few concurrent operations
It is not appropriate to handle a large number of concurrent operations.
Case study of differences between asynchronous and Multithreading
Some brothers may have been impatient with the theory. Now let's look at several practical asynchronous operation examples.
Asynchronous and multithreading: What is the difference between the asynchronous method generated by Delegate?
As you may know, using delegate can "automatically" enable asynchronous calls to a method. Intuitively, I think the compiler or CLR uses another thread to execute the target method. Is that true? Let's use a piece of code to prove it.
View sourceprint?
01
using
System;
02
using
System.Threading;
03
namespace
AsyncDelegateDemo
04
{
05
delegate
void
AsyncFoo(
int
i);
06
class
Program
07
{
08
/// ﹤summary﹥
09
/// Output the information of the current thread
10
/// ﹤/summary﹥
11
/// <Param name = "name"> method name </param>
12
static
void
PrintCurrThreadInfo(
string
name)
13
{
14
Console.WriteLine(
"Thread Id of "
+ name +
" is: "
+ Thread.CurrentThread.ManagedThreadId +
", current thread is "
15
+ (Thread.CurrentThread.IsThreadPoolThread ?
""
:
"not "
) +
"thread pool thread."
);
16
}
17
18
19
/// ﹤summary﹥
20
/// Test method, sleep for a certain period of time
21
/// ﹤/summary﹥
22
/// <Param name = "I"> sleep time </param>
23
static
void
Foo(
int
i)
24
{
25
PrintCurrThreadInfo(
"Foo()"
);
26
Thread.Sleep(i);
27
}
28
29
30
/// ﹤summary﹥
31
/// Deliver an asynchronous call
32
/// ﹤/summary﹥
33
static
void
PostAsync()
34
{
35
AsyncFoo caller =
new
AsyncFoo(Foo);
36
caller.BeginInvoke(1000,
new
AsyncCallback(FooCallBack), caller);
37
}
38
39
40
static
void
Main(
string
[] args)
41
{
42
PrintCurrThreadInfo(
"Main()"
);
43
for
(
int
i = 0; i < 100; i++)
44
{
45
PostAsync();
46
}
47
Console.ReadLine();
48
}
49
50
51
static
void
FooCallBack(IAsyncResult ar)
52
{
53
PrintCurrThreadInfo(
"FooCallBack()"
);
54
AsyncFoo caller = (AsyncFoo)ar.AsyncState;
55
caller.EndInvoke(ar);
56
}
57
}
58
}
Differences between asynchronous and multithreading the output of the Instance code is as follows:
Thread ID of main () is: 1,
Current thread is NOT thread pool thread.
Thread ID of Foo () is: 3,
Current thread is thread pool thread.
Thread ID of foocallback () is: 3,
Current thread is thread pool thread.
Thread ID of Foo () is: 3,
Current thread is thread pool thread.
Thread ID of Foo () is: 4,
Current thread is thread pool thread.
Thread ID of Foo () is: 5,
Current thread is thread pool thread.
Thread ID of foocallback () is: 3,
Current thread is thread pool thread.
Thread ID of Foo () is: 3,
Http://www.cnblogs.com/tianzhiliang/archive/2010/08/31/1813629.html
What is the difference between asynchronous and multithreading in C? Both asynchronous and multithreading can avoid calling thread blocking and improve software responsiveness. Sometimes we think that Asynchronization and multithreading are equivalent. However, there are some differences between asynchronous and multithreading. These differences lead to the difference in the time to use Asynchronous and multithreading.
Differences between asynchronous and multithreading: the nature of asynchronous operations
Institute
Some programs will eventually be executed by computer hardware. Therefore, in order to better understand the nature of asynchronous operations, we need to understand its hardware base.
Friends who are familiar with computer hardware must be familiar with the DMA term. The technical specifications of hard disks and optical drives all have clear DMA mode indicators. In fact, NICS, sound cards, and video cards also have DMA functions. DMA is straight
Memory Access means that hardware with DMA functions can exchange data with memory without consuming CPU resources. The hardware is enabled as long as the CPU sends a command during data transmission.
The hardware will trigger an interruption after the transmission is complete to notify the operation to complete. These I/O operations that do not consume CPU time are the hardware basis of asynchronous operations. So even in DOS
Such single-process (and wireless) systems can also initiate asynchronous DMA operations.
Difference between Asynchronization and multithreading: thread nature
A thread is not a computer hardware function, but a logic function provided by the operating system. A thread is essentially a piece of code that runs concurrently in a process, therefore, threads require the operating system to invest CPU resources for running and scheduling.
Differences between asynchronous and multithreading Advantages and Disadvantages of asynchronous operations
Because
For asynchronous operations, there is no additional thread burden, and callback is used for processing. Under well-designed conditions, the processing function does not need to use shared variables (even if it cannot be completely used, at least can be reduced
The number of shared variables) to reduce the possibility of deadlocks. Of course, asynchronous operations are not perfect. The programming of asynchronous operations is highly complex. The program mainly uses the callback method for processing.
It is difficult to debug.
Differences between Asynchronization and multithreading Advantages and Disadvantages of Multithreading
The advantages of multithreading are obvious. The processing program in the thread is still executed in sequence, which conforms to the thinking habits of ordinary people, So programming is simple. However, the disadvantages of multithreading are also obvious. The use (abuse) of threads will impose additional burden on context switching. In addition, shared variables between threads may cause deadlocks.
Applicability
In
After understanding the advantages and disadvantages of threads and asynchronous operations, we can discuss the rational use of threads and asynchronous operations. In my opinion, when I/O operations need to be executed, asynchronous operations are better than thread + synchronization.
I/O operations are more appropriate. I/O operations include not only direct file and network read/write operations, but also database operations, web services, httprequest, And. net
Remoting and other cross-process calls.
The applicability of threads is those that require long CPU operations, such as time-consuming graphics processing and Algorithm Execution. However
Due to the simplicity and habit of using thread programming, many friends often use threads to execute time-consuming I/O operations. In this way, when there are only a few concurrent operations
It is not appropriate to handle a large number of concurrent operations.
Case study of differences between asynchronous and Multithreading
Some brothers may have been impatient with the theory. Now let's look at several practical asynchronous operation examples.
Asynchronous and multithreading: What is the difference between the asynchronous method generated by Delegate?
As you may know, using delegate can "automatically" enable asynchronous calls to a method. Intuitively, I think the compiler or CLR uses another thread to execute the target method. Is that true? Let's use a piece of code to prove it.
View sourceprint?
01
using
System;
02
using
System.Threading;
03
namespace
AsyncDelegateDemo
04
{
05
delegate
void
AsyncFoo(
int
i);
06
class
Program
07
{
08
/// ﹤summary﹥
09
/// Output the information of the current thread
10
/// ﹤/summary﹥
11
/// <Param name = "name"> method name </param>
12
static
void
PrintCurrThreadInfo(
string
name)
13
{
14
Console.WriteLine(
"Thread Id of "
+ name +
" is: "
+ Thread.CurrentThread.ManagedThreadId +
", current thread is "
15
+ (Thread.CurrentThread.IsThreadPoolThread ?
""
:
"not "
) +
"thread pool thread."
);
16
}
17
18
19
/// ﹤summary﹥
20
/// Test method, sleep for a certain period of time
21
/// ﹤/summary﹥
22
/// <Param name = "I"> sleep time </param>
23
static
void
Foo(
int
i)
24
{
25
PrintCurrThreadInfo(
"Foo()"
);
26
Thread.Sleep(i);
27
}
28
29
30
/// ﹤summary﹥
31
/// Deliver an asynchronous call
32
/// ﹤/summary﹥
33
static
void
PostAsync()
34
{
35
AsyncFoo caller =
new
AsyncFoo(Foo);
36
caller.BeginInvoke(1000,
new
AsyncCallback(FooCallBack), caller);
37
}
38
39
40
static
void
Main(
string
[] args)
41
{
42
PrintCurrThreadInfo(
"Main()"
);
43
for
(
int
i = 0; i < 100; i++)
44
{
45
PostAsync();
46
}
47
Console.ReadLine();
48
}
49
50
51
static
void
FooCallBack(IAsyncResult ar)
52
{
53
PrintCurrThreadInfo(
"FooCallBack()"
);
54
AsyncFoo caller = (AsyncFoo)ar.AsyncState;
55
caller.EndInvoke(ar);
56
}
57
}
58
}
Differences between asynchronous and multithreading the output of the Instance code is as follows:
Thread ID of main () is: 1,
Current thread is NOT thread pool thread.
Thread ID of Foo () is: 3,
Current thread is thread pool thread.
Thread ID of foocallback () is: 3,
Current thread is thread pool thread.
Thread ID of Foo () is: 3,
Current thread is thread pool thread.
Thread ID of Foo () is: 4,
Current thread is thread pool thread.
Thread ID of Foo () is: 5,
Current thread is thread pool thread.
Thread ID of foocallback () is: 3,
Current thread is thread pool thread.
Thread ID of Foo () is: 3,