简体   繁体   中英

CPU occupancy shows 100%

I have a scenario of reader-writers from/to a common buffer. All writer threads have highest priority than the reader thread. The reader thread activation function is in continuous while loop if there is any data in the buffer it reads all contents at shot and calls sche_yield() function.

In this scenario I expected CPU utilisation is some where 20 to 30% but it shows 100%(Linux top command) but after little more digging out I realised that the reader thread after reading the data it get added at the end of scheduling queue and there are no writers to write data, the reader again scheduled and it again yields and get added at the end of scheduling queue and this is coninously repeating and due to this CPU shows 100%.

I feel there should be an event handling meachanism rather than the reader in the while loop continously cheking data. Could you help how to write event handling(there should be an indication to reader thread when writer threads write data) which is platform and OS independent ?

Thanks

In general you should use a synchronization primitive that would allow the reader thread to wait until the work actually appears instead of doing a busy loop. This can be solved with a synchronization primitive called condition variable.

If C is the only option you have you can either use the cnd_t condition variable introduced by the C11 thread support library , but this can be not supported on older compilers. In that case you still have the pthread_cond_t with all the synchronization functions like pthread_cond_wait / pthread_cond_signal but I am not aware if the Windows feature called "Pthread Support in Microsoft Windows Services for UNIX Version 3.5" that supports the POSIX thread stuff on Windows is not considered legacy. However you should be able to make a thin wrapper around Windows HANDLE objects that would mimic the behavior of POSIX condition variables.

If you can use C++ then you could use a std::condition_variable introduced by C++11.

Notes on support of C11

Since you mentioned you needed platform independent mechanism it should be noted that C11 is not supported in MSVC 2012 (which I can at least verify myself) and links about MSVC 2015 (eg this ) mostly hint that it is not supported there as well.

What you describe (in the second paagraph) is a reasonably way to expect a typical scheduling: when a process/thread gives up CPU, it goes back to the end of the queue.

What you can do instead is to make the writer thread(s) signal the reader when the data is placed in the queue using a conditional variable rather than using an infinite loop for the reader.

Using pthread_cond_wait() the reader can wait for the data to become available and using pthread_cond_signal() the writer(s) would notify the reader than the data is ready.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM