-
Micro Threads
Does anyone have any information on these? Basically, threading, but doing manual time slicing:
Code:
void doSomeStuff()
{
int x =0; while(true) {++x; Slice(); }
}
Slice() should switch to the next micro thread, then after giving the other micro threads some time, come back to where it left off. Anyone ever seen this, or have any references?
Z.
-
Yeah. It's called scheduling, a part of multiprocessing. A LONG time ago I wrote OS components for DEC and other platforms that did this kind of thing.
For Windows, it is usually called 'context' - changing context is changing the process in 'charge' of the cpu. But it's still messing with the scheduler. www.sysinternals.com has code and white papers that show how Windows effects scheduling. Read carefully before proceeding. You can use SetPriorityCLass, GetThreadPriority, SetThreadPriority - in other words, the folks who designed Windows knew that you'd need to do this.
The problem you have is that once you invoke slice(), your calling process is no longer in charge. You will have to elevate substantially the base priority of the parent process after creating the threads, then do sonmething to individual threads, like alter priority, to get them to run the way you want. None of this is hard to do.
Normally, the OS operates under a set of rules about how long you get the cpu, what makes you a candidate to get the cpu, and how to handle switching context. It will independently handle the threads' requests for cpu. If you try to break those rules by too big a margin with aritifical scheduling, you may see some interesting results.
-
Well, the idea is to be able have a few thousand of these micro threads running. I found a few things about lightweight threads around on google, but I thought Id ask here to see what else I could get. Thanks a bunch, I will check the link you posted.
Z.
-
I was playing around with setjmp and longjmp, to see if they would have any application here, but, since you need to longjmp before the function that called setjmp returns, this wont help. I did find "Fibers", which are basically threads with manual scheduling.
Code:
void __stdcall FibProc(void* p)
{
while(true) { cout << "Fiber" << endl; SwitchToFiber(GetFiberData()); }
}
int main()
{
void* tFibData = NULL;
ConvertThreadToFiber(tFibData);
void* Fib = CreateFiber(0, FibProc, GetCurrentFiber());
int x = 0;
while(x < 10)
{
cout << "main()" << endl;
++x;
SwitchToFiber(Fib);
}
DeleteFiber(Fib);
return 0;
}
That should print:
Code:
main()
Fiber
main()
Fiber
main()
Fiber
main()
Fiber
...
... 100 times.
Z.
-
Don't use setjmp and longjmp in C++ programs, because they don't handle constructors and destructors and as a result will totally bugger it up.
-
Im not going to =). I just wanted to play around. =).
Z.