When the thread is blocked, will the thread make up the CPU?

Source: Internet
Author: User

Depending on what you mean by blocking, if it is an endless loop, of course it occupies the CPU, if it is waiting for a synchronization event, it will let out the CPU

Blue Water and blue sky [grade: ◆ (Elementary)] (credit: 100)Reply: 14:34:56Top

If the thread function structure is as follows:
DWORD threadfun (lpvoid para)
{
While (1)
{
Blocking statement; // will the thread give up the CPU?
}
}

Alan [grade: ◆ (intermediate)] (credit: 105)Reply: 15:33:51Top

If it is like you, it won't let out the CPU.

Enumeration [grade: ◆ (intermediate)] (credit: 100)Reply: 17:30:14Top

If the thread function structure is as follows:
DWORD threadfun (lpvoid para)
{
While (1)
{
Blocking statement; // will the thread give up the CPU?
}
}
========================================================== ==============
Yes, this blocking should not be caused by Loop Blocking, which will give up the CPU.
Blocking functions, such as waitforsingleobject, Recv, and accept, give way to the CPU.

Say "face": [grade: ◆ (Elementary)] (credit: 96)Reply: 18:46:43Top

Of course

Study hard [grade: ◆ (Elementary)] (credit: 100)Reply: 19:48:45Top

If the thread function structure is as follows:
DWORD threadfun (lpvoid para)
{
While (1)
{
Blocking statement; // will the thread give up the CPU?
}
}
--------------------------
Yes, because when you call a blocked function, your thread enters the core State and no longer executes user State code, that is
After your thread executes the core code, you can transfer the control to the scheduling program of the OS, and the scheduling program can call other threads.
This should be the case.

HR [grade: ◆ (Elementary)] (credit: 100)Reply: 19:55:20Top

Yes, of course. Because the OS process scheduling (or thread management) program will freeze your thread and give the CPU to other thread code for execution.
Of course, if you use the Buy-loop method to wait for other threads, do not hand over the cup, because the OS process scheduling (or thread management) program does not think (or know) your thread needs to be frozen. Understand?

Good luck.

Good children [grade: ◆ (Elementary)] (credit: 100)Reply: 20:10:28Top

Practice has proved that

Blue Water and blue sky [grade: ◆ (Elementary)] (credit: 100)Reply: 10:50:44Top

I understand that the conclusion should be true;
As long as the thread is blocked or suspended, the scheduler will deprive it of the remaining CPU time slice and execute other threads.
Thank you!

Blue Water and blue sky [grade: ◆ (Elementary)] (credit: 100)Reply: 10:55:04Top

I want to ask another question: will such a thread function always occupy the CPU? But other threads never get the CPU?
DWORD threadfun (lpvoid para)
{
While (1)
{
}
}

I am not Wang Rong [grade: ◆ (Elementary)] (credit: 100)Reply: 10:55:23Top

Ladies and gentlemen: How can I display the coordinates of two points and then measure the distance between two points? Question for beginners. How can I ask questions?
Thank you!

Kai [grade: ◆ (Elementary)] (credit: 100)Reply: 11:39:44Top

Even an endless loop. The system will also force the CPU to be transferred to other threads. But other code in the current thread cannot be executed.

Blue Water and blue sky [grade: ◆ (Elementary)] (credit: 100)Reply: 12:38:58Top

In my experiment, I found that if there is an endless loop, it occupies a very high CPU resource, up to 100% 99. If there is an endless loop in the thread, its priority will become very high, the system gives priority to CPU time slice allocation.

See you [grade: ◆ (Elementary)] (credit: 100)Reply: 14:44:05Top

CPU utilization is the ratio between the system and the mentality and the user State. There is no priority problem,

Flysec [grade: ◆ (Elementary)] (credit: 100)Reply: 14:58:00Top

Blocking will definitely let out the CPU, or why should we design the blocking function?
Only when the loop does not call the blocking function and the function is returned immediately
Will not let the CPU out, and the operating system will schedule it by itself

I held my tickets for love and waited quietly ...... [Level:★(Intermediate)] (credit: 100)Reply: 15:03:50Top

Of course, it will let out the CPU!
Pay attention to the concept of time slice. Windows switches a thread every 8 ms.

Anonymous [grade: ◆ (Elementary)] (credit: 100)Reply: 15:12:42Top

There are many statuses of the process. Look for this OS.

A Liang in thinking... [level: ◆ (Elementary)] (credit value: 100)Reply: 0:32:20Top

Yes,

Real blocking means hard interruption at the hardware level, soft interruption at the software level, or waiting for messages.

The current Code Execution will certainly give up the CPU.

A bucket of rivers and lakes (http://adong2008.512j.com) [level:★★(Intermediate)] (credit: 99)Reply: 12:07:26Top

Depends on your code.

Blue Water and blue sky [grade: ◆ (Elementary)] (credit: 100)Reply: 16:57:55Top

Thank you!

Jiang Sheng. msmvp2004jan [grade:★★★★★(Advanced)] (credit score: 262)Reply: 3:53:27Top

Some operating systems will suspend blocked threads, but some operating systems will not -- it depends entirely on whether the operating system is multi-task and its task scheduling algorithm.

Kong Qi [grade:★★(Intermediate)] (credit: 115)Reply: 8:07:57Top

The real blocking will give way to the CPU. The implementation in the OS is to put the thread-related struct pointer into a waiting queue, because it will not be recovered from the waiting queue to the ready or execution queue before the event is generated, of course, it is to let out the CPU (the system did not switch to the code of this thread again by restoring the site)

Kong Qi [grade:★★(Intermediate)] (credit: 115)Reply: 8:12:11Top

An endless loop is not blocked, as long as the code is not blocked by time-sharing scheduling.
The dead loop consumes a lot of CPU because other programs have some blocking, but it is not blocked. In contrast, it has much chance of getting the CPU.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.