Click here to Skip to main content
15,914,111 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Global vs local variables Pin
Naveen5-Sep-07 2:13
Naveen5-Sep-07 2:13 
AnswerRe: Global vs local variables Pin
David Crow5-Sep-07 4:53
David Crow5-Sep-07 4:53 
QuestionReader Writer lock Pin
Waldermort5-Sep-07 0:50
Waldermort5-Sep-07 0:50 
AnswerRe: Reader Writer lock Pin
chandu0045-Sep-07 1:42
chandu0045-Sep-07 1:42 
AnswerRe: Reader Writer lock Pin
Matthew Faithfull5-Sep-07 1:52
Matthew Faithfull5-Sep-07 1:52 
GeneralRe: Reader Writer lock Pin
Waldermort5-Sep-07 2:44
Waldermort5-Sep-07 2:44 
GeneralRe: Reader Writer lock Pin
El Corazon5-Sep-07 5:02
El Corazon5-Sep-07 5:02 
GeneralRe: Reader Writer lock Pin
El Corazon5-Sep-07 5:34
El Corazon5-Sep-07 5:34 
WalderMort wrote:
I'm implementing a console stub which will divert a second consoles output to my gui app in real time. The main loop constantly scans the screen buffer, reads the text and clears it. Due to sluggish behaviour I'm using a thread to do the actual writing to the pipe ( since the text gathered from the buffer needs manipulating ).


It suddenly occured to me after answering you... though the answer is still a possibility, it may not be necessary. We're both thinking too "direct" to get an efficient answer.

Problem: you need to add in one thread, read in another, you don't want constant memory allocation of a queue, you want vector speed and efficiency.

Answer: preallocate the vector, and use it as a ring-buffer queue. A ring buffer queue is different than a standard queue in that it is just a section of preallocated memory (in the past an array, recently a vector) in which you keep two indexes: head, tail. You start with head and tail pointing to the start of the vector, head==tail is an empty buffer. Thread 1 adds an item to the vector, write first, then increment the tail, if the tail exceeds the buffer size, set it to 0. if incrementing tail ever makes tail== head you have a full buffer, expand it. to pop something off the queue, read first, then increment the head similarly as you did the tail.

You can protect only the subportions of the algorithm, reading and writing the head/tail indexes and fully lock during expansion. This is a prime example of an implicitely shared container. as long as adding to the tail never reaches the head (fills the buffer) you can run almost free-running, as near as wait-free as you can imagine.





_________________________
Asu no koto o ieba, tenjo de nezumi ga warau.
Talk about things of tomorrow and the mice in the ceiling laugh. (Japanese Proverb)

GeneralRe: Reader Writer lock Pin
Waldermort5-Sep-07 6:17
Waldermort5-Sep-07 6:17 
GeneralRe: Reader Writer lock Pin
El Corazon5-Sep-07 7:38
El Corazon5-Sep-07 7:38 
GeneralRe: Reader Writer lock Pin
Matthew Faithfull5-Sep-07 5:49
Matthew Faithfull5-Sep-07 5:49 
GeneralRe: Reader Writer lock Pin
Waldermort5-Sep-07 6:15
Waldermort5-Sep-07 6:15 
AnswerRe: Reader Writer lock Pin
Matthew Faithfull5-Sep-07 6:28
Matthew Faithfull5-Sep-07 6:28 
GeneralRe: Reader Writer lock Pin
El Corazon5-Sep-07 7:42
El Corazon5-Sep-07 7:42 
GeneralRe: Reader Writer lock Pin
Matthew Faithfull5-Sep-07 9:30
Matthew Faithfull5-Sep-07 9:30 
GeneralRe: Reader Writer lock Pin
El Corazon5-Sep-07 9:52
El Corazon5-Sep-07 9:52 
QuestionA very basic question Pin
HakunaMatada5-Sep-07 0:24
HakunaMatada5-Sep-07 0:24 
AnswerRe: A very basic question Pin
Matthew Faithfull5-Sep-07 0:47
Matthew Faithfull5-Sep-07 0:47 
AnswerRe: A very basic question Pin
Waldermort5-Sep-07 0:58
Waldermort5-Sep-07 0:58 
AnswerRe: A very basic question Pin
Hamid_RT5-Sep-07 1:02
Hamid_RT5-Sep-07 1:02 
QuestionIE question Pin
Mohit19825-Sep-07 0:18
Mohit19825-Sep-07 0:18 
AnswerRe: IE question Pin
Hamid_RT5-Sep-07 1:05
Hamid_RT5-Sep-07 1:05 
QuestionHow to retrive the handle of other window. Pin
GauranG Shah4-Sep-07 23:31
GauranG Shah4-Sep-07 23:31 
AnswerRe: How to retrive the handle of other window. Pin
Waldermort4-Sep-07 23:56
Waldermort4-Sep-07 23:56 
QuestionMFC Multiple Inheritance Pin
Krishnatv4-Sep-07 22:50
Krishnatv4-Sep-07 22:50 

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.