Click here to Skip to main content
15,918,193 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: send number to speaker Pin
azonenberg17-Jun-07 7:54
azonenberg17-Jun-07 7:54 
QuestionThreads and mutexes Pin
Cyrilix16-Jun-07 21:43
Cyrilix16-Jun-07 21:43 
AnswerRe: Threads and mutexes Pin
Matthew Faithfull17-Jun-07 0:45
Matthew Faithfull17-Jun-07 0:45 
GeneralRe: Threads and mutexes Pin
Cyrilix17-Jun-07 7:02
Cyrilix17-Jun-07 7:02 
GeneralRe: Threads and mutexes Pin
Matthew Faithfull17-Jun-07 10:11
Matthew Faithfull17-Jun-07 10:11 
GeneralRe: Threads and mutexes [modified] Pin
Cyrilix17-Jun-07 11:25
Cyrilix17-Jun-07 11:25 
GeneralRe: Threads and mutexes Pin
Bram van Kampen17-Jun-07 15:00
Bram van Kampen17-Jun-07 15:00 
GeneralRe: Threads and mutexes [modified] Pin
Cyrilix17-Jun-07 16:12
Cyrilix17-Jun-07 16:12 
Well, the original question (and design I had in mind) failed miserably after looking at a few different reasons why, so I scrapped it and made one that not only has far less synchronization (+ for performance), but also zero thread polling (another + for performance), and should work.

What I wanted to do was, if I had 3 worker threads, make the 3 threads not waste much CPU cycles if there was no work left in the queue. There are many ways of doing this and I label the first two as stupid ways (brute force, poor design), the next one a smarter but not smart way, and the last one, the best method, in my opinion:

1 - Continuous polling of the work queue for work
This wastes tremendous amounts of CPU cycles when none should be used. In theory, this is fine, if the only work you do is through the work queue, but using dedicated threads to do all the work in your program, be it big or small, is a bad idea in the first place and won't happen.

2 - Continuous polling of the work queue with a sleep timer
This wastes far less CPU cycles on polling as the thread sleeps for a little while before re-polling. The major disadvantage (and a big one at that), is the latency induced by thread sleeping. In the worst-case scenario, if all of my threads have a sleep timer of half a second, and they all sleep at the same time, work that gets called in that time may wait half a second before it can be processed. On my 2.2 GHz dual core machine, that's potentially 2.2 billion clock cycles wasted. In practice, it will be less, but you can see why this is a bad idea for a high-performance design.

3 - Blocking with critical sections / mutex
In theory, this one actually looks OK. When there is no more work, all I have to do is check a boolean value and immediately enter the critical section from the first worker thread that gets to the point where it's looking for more work and there is none. All the other worker threads that reach that point will not proceed further. This, however, means that you have to handle the case of at least one thread making it through the critical section and looping forever, wasting 100/n percent of CPU cycles (theoretically, where n is the number of active threads and where all threads have equal priority).

4 - Blocking with manually set events
The current method that I use to do this -- I'm unsure of the overhead on an event, but I would guess that it's less than that of a critical section or mutex. I've seen an article where they show acquiring/releasing critical sections have an overhead of around 40-100 cycles, and the same for mutexes is around 750-2500 cycles. I will have to double check these claims, but it comes from this article. With events, the code is simple and threads do not have to poll.

Well, that's my analysis of it... feel free to correct any incorrect assumptions I've made out of lack of experience.


-- modified at 22:17 Sunday 17th June, 2007
GeneralRe: Threads and mutexes Pin
Matthew Faithfull17-Jun-07 22:23
Matthew Faithfull17-Jun-07 22:23 
GeneralRe: Threads and mutexes Pin
Bram van Kampen18-Jun-07 13:30
Bram van Kampen18-Jun-07 13:30 
NewsAboubt Barcode printing, VC++ master-hand Come in and help me. Pin
h_boy200816-Jun-07 20:46
h_boy200816-Jun-07 20:46 
AnswerRe: Aboubt Barcode printing, VC++ master-hand Come in and help me. Pin
Matthew Faithfull17-Jun-07 0:38
Matthew Faithfull17-Jun-07 0:38 
GeneralRe: Aboubt Barcode printing, VC++ master-hand Come in and help me. Pin
Matthew Faithfull18-Jun-07 1:44
Matthew Faithfull18-Jun-07 1:44 
Questionmsi setup project Pin
deeps_cute16-Jun-07 20:25
deeps_cute16-Jun-07 20:25 
QuestionFirst-chance exception ... access violation ... pointer issue Pin
moonraker92816-Jun-07 20:18
moonraker92816-Jun-07 20:18 
AnswerRe: First-chance exception ... access violation ... pointer issue Pin
Hans Dietrich16-Jun-07 20:33
mentorHans Dietrich16-Jun-07 20:33 
GeneralRe: First-chance exception ... access violation ... pointer issue Pin
moonraker92816-Jun-07 21:48
moonraker92816-Jun-07 21:48 
GeneralRe: First-chance exception ... access violation ... pointer issue Pin
Bram van Kampen18-Jun-07 13:38
Bram van Kampen18-Jun-07 13:38 
AnswerRe: First-chance exception ... access violation ... pointer issue Pin
Stephen Hewitt17-Jun-07 12:51
Stephen Hewitt17-Jun-07 12:51 
GeneralRe: First-chance exception ... access violation ... pointer issue Pin
Bram van Kampen18-Jun-07 13:49
Bram van Kampen18-Jun-07 13:49 
Questiondrawing 2D in C++ Pin
moonraker92816-Jun-07 19:33
moonraker92816-Jun-07 19:33 
AnswerRe: drawing 2D in C++ Pin
Cyrilix16-Jun-07 21:41
Cyrilix16-Jun-07 21:41 
GeneralRe: drawing 2D in C++ Pin
moonraker92816-Jun-07 22:01
moonraker92816-Jun-07 22:01 
GeneralRe: drawing 2D in C++ Pin
moonraker92816-Jun-07 22:51
moonraker92816-Jun-07 22:51 
GeneralRe: drawing 2D in C++ Pin
Cyrilix17-Jun-07 0:17
Cyrilix17-Jun-07 0:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.