Skip to main content
added 445 characters in body
Source Link
Philipp
  • 123.2k
  • 28
  • 264
  • 345

Having a thread busy-spin like that is not a good idea. It causes unnecessary CPU load. This drains the battery of mobile users device, causes unnecessary heat and power consumption for desktops and servers and steals CPU time from background processes.

A better idea could be to make the thread pause itself when it completed processing its message queue. Then have the system which handles the message queue resume the thread as soon as it enqueues a new message. How to do that depends on what threading library you are using. I haven't got any experience with multithreading in C in particular, but a cursory research tells me that pthread is a popular library choice, which further points me to pthread_cond_wait and to this stackoverflow question.

Having a thread busy-spin like that is not a good idea. It causes unnecessary CPU load. This drains the battery of mobile users device, causes unnecessary heat and power consumption for desktops and servers and steals CPU time from background processes.

A better idea could be to make the thread pause itself when it completed processing its message queue. Then have the system which handles the message queue resume the thread as soon as it enqueues a new message. How to do that depends on what threading library you are using.

Having a thread busy-spin like that is not a good idea. It causes unnecessary CPU load. This drains the battery of mobile users device, causes unnecessary heat and power consumption for desktops and servers and steals CPU time from background processes.

A better idea could be to make the thread pause itself when it completed processing its message queue. Then have the system which handles the message queue resume the thread as soon as it enqueues a new message. How to do that depends on what threading library you are using. I haven't got any experience with multithreading in C in particular, but a cursory research tells me that pthread is a popular library choice, which further points me to pthread_cond_wait and to this stackoverflow question.

Source Link
Philipp
  • 123.2k
  • 28
  • 264
  • 345

Having a thread busy-spin like that is not a good idea. It causes unnecessary CPU load. This drains the battery of mobile users device, causes unnecessary heat and power consumption for desktops and servers and steals CPU time from background processes.

A better idea could be to make the thread pause itself when it completed processing its message queue. Then have the system which handles the message queue resume the thread as soon as it enqueues a new message. How to do that depends on what threading library you are using.