Why does Windows have no DeleteConditionVariable() function to go together with InitializeConditionVariable()? - winapi

I'm trying out Windows support for Condition Variables today (as provided by Microsoft for Windows Vista and later). To initialize a condition variable, I call InitializeConditionVariable(), which is straightforward enough, but I don't see any way provided to destroy the condition variable when I'm done using it. Why is there no DeleteConditionVariable() function?
(I'd expect the API to be analogous to the existing CreateCriticalSection() / DestroyCriticalSection() API)

A conditional variable is a very light-weight object that is internally based on a single global kernel keyed event object that is always available through every process's entire lifetime. The conditional variable simply contains a pointer to that object. So there is nothing that needs to be freed explicitly, thus no delete function is needed.

Related

WinDbg session variables - Aliases vs. User-Defined Pseudo-Registers

I need to hold a specific value (a string, to be exact) throughout the lifetime of a single WinDbg session.
The reason for that, is that I need to address this variable in a few places throughout the lifetime of the WinDbg session (through .if statements, to be exact), and I want it to be defined at the startup of the session - using an argument that'll be passed. It can't be undefined or disposed - I must be able to address it in any point in the debugging session, and of course I don't want to risk that it might be redefined.
I was thinking of using a file for that purpose, or a shared memory, but I much prefer to solve this using WinDbg scripting.
If it's possible, it's obviously a much more elegant solution.
I've done some reading online on this matter and the issue is that I couldn't find a reference where the differences between Aliases (defined by an aS command) and User-Defined Pseudo-Registers (the registers in the range $t0..$t19, which are accessed by using the r command) were described. I couldn't find really understand the use cases for each.
The seemed to me Aliases is the better option, due to the fact that they can be named, in contrast to User-Defined Pseudo-Registers which have set names (wasn't sure regarding how to pick the "right" registered, to minimize possible collisions with other scripts which might use it, or is there any difference at all).
Am I missing something here?
Which should I use in this case, or are they both unsuitable for this situation?

Should I explicitly delete object with custom format from clipboard?

I have following question - should I delete explicitly object placed in windows clipboard? What does EmptyClipboard function in such case? Global object was created via GlobalAlloc function, and has custom format (which was registered via RegisterClipboardFormat function). I did not find section which says about such aspect
No, once you place the object on the clipboard, the clipboard is responsible for destroying it. You allocated the memory with GlobalAlloc, as required, and the system is therefore able to deallocate the memory using GlobalFree. Which is does when it needs to.

Go lang global variables without goroutines overwriting

I'm writing a CMS in Go and have a session type (user id, page contents to render, etc). Ideally I'd like that type to be a global variable so I'm not having to propagate it through all the nested functions, however having a global variable like that would obviously mean that each new session would overwrite it's predecessor, which, needlessly to say, would be an epic fail.
Some languages to offer a way of having globals within threads that are preserved within that thread (ie the value of that global is sandboxed within that thread). While I'm aware that Goroutines are not threading, I just wondered if there was a similar method at my disposal or if I'd have to pass a local pointer of my session type down through the varies nested routines.
I'm guessing channels wouldn't do this? From what I can gather (and please correct me if I'm wrong here), but they're basically just a safe way of sharing global variables?
edit: I'd forgotten about this question! Anyhow, an update for anyone who is curious. This question was written back when I was new to Go and the CMS was basically my first project. I was coming from a C background with familiarity with POSIX thread but I quickly realised a better approach was to write the code in a mode functional design with session objects passed down as pointers in function parameters. This gave me both the context-sensitive local scope I was after while also minimizing the amount to data I was copying about. However being a 7 year old project and one that was at the start of my transition to Go, it's fair to say the project could do with a major rewrite anyway as there are a lot of mistakes made. That's a concern for another day though - currently it works and I have enough other projects on the go at.
You'll want to use something like a Context:
http://blog.golang.org/context
Basically, the pattern is to create a Context for each unique thing you want to do. (A web request in your case.) Use context.WithValue to embed multiple variables in the context. Then always pass it as the first parameter to other methods that are doing further work in other goroutines.
Getting the variable you need out of the context is a matter of calling context.Value from within any goroutine. From the above link:
A Context is safe for simultaneous use by multiple goroutines. Code can pass a single Context to any number of goroutines and cancel that Context to signal all of them.
I had an implementation where I was explicitly sending variables as method parameters, and I discovered that embedding these variables using contexts significantly cleaned up my code.
Using a Context also helps because it provides ways to end long-running tasks by using channels, select, and a concept called a "done channel." See this article for a great basic review and implementation:
http://blog.golang.org/pipelines
I'd recommend reading the pipelines article first for a good flavor of how to manage communication among goroutines, then the context article for a better idea of how to level-up and start embedding variables to pass around.
Good luck!
Don't use global variables. Use Go goroutine-local variables.
go-routine Id..
There are already goroutine-local variables: they are called function
arguments, function return values, and local variables.
Russ
If you have more than one user, then wouldn't you need that info for each connection? So I would think that you'd have a struct per connected user. It would be idiomatic Go to pass a pointer to that struct when setting up the worker goroutine, or passing the pointer over a channel.

MediaHistory NowPlaying vs WriteRecentPlay

Is it necessary to call WriteRecentPlay in addition to assigning a NowPlaying value or does the latter imply the former as far as the OS handles it?
If it is required, can WriteRecentPlay be called at the same time as the NowPlaying assignment or should it be called when the audio completes?
Considering that the certifications call for separate image dimensions for now playing vs recent play, it follows that two separate calls need to be made.

Windows API and Passing Values

I know that we can pass parameters to API functions in three types of Passing by Value, Passing Indirectly and Passing by Reference.
My question is about Indirect mode; can we change address of allocated memory space on demand or that's done by windows in some restricted area which is owned by windows core?
In other words can we tell windows upon structure creation time to make and store needed structure in a memory area that we specified?
How about passing by Reference? if we call the API function which is accept parameters by reference, does windows places and keeps structures in the same memory area on each call or not?

Resources