Original Post
Hello once again, kind people, as I intrude upon you once more to seek answers to questions that bang around in my skull! Avail me, and you shall have my undying gratitude. For the next few days, anyway... OK, so, here's the deal. I've been working and designing (well, trying to) some multi-threaded junk lately. That's all well and good. However, I have come to a problem/point of curiousity which I'd like an answer for. Let's imagine that I have a spate of atomic variable functions, utilizing inlined assembly (CMPXCHG). Now, with those as a basis, I then implement some useful classes such as a local resource lock, or a mutex on steriods, or something else. Now, here is where my curiosity comes in. I know Windows has a lot of built in waiting functions which can be used to wait on the good 'ol mutexes (mutices?) semaphores, events, etc., but if I had my own mutex class, I would need a suite of waiting functions for it. And to have a waiting function, there are a couple of important things that you need to have in them. Firstly, and probably most importantly, you have to be able to tell the operating system, in my case Windows (only), that the thread is waiting on a locked resource, and should switch to another thread. There is the ubiquitous Sleep() function, but I have heard more then it's fair share of bad things about it, such as there being no real way to tell how long it will be before the thread gets priority again, etc. So I kind of need a way to tell the Windows thread schedular that it can and indeed immediately should switch threads. Without a way to do that, I'm either doomed to using Sleep or simply trashing this idea, and going with the one outlined below. So that's one way of doing things. Alternately, I could reinvent the wheel in a really big way, and run everything in 2 threads, the main thread, and an extra thread. The main thread would then split into many fibers, which would be scheduled by the extra thread. And these fibers would function as 'threads' for my purposes, as well as allowing me more control over them. But then again, of course, this involved a whole lot of reinventing that I'd rather avoid if possible. So, insults, opinions, clever insights, and possibly, just maybe, an answer, would be greatly appreciated! Thanks again!