• entries
24
21
• views
17675

# T t HR h E readi ADI n N g G

247 views

Continuing my language-shootout mission I spent what little free time I had this week looking at the issue of threading. This seems to be one area where C++ falls badly behind other languages currently in the shootout. As I understand it this is partly because the standard threading implementation for C++ under *nix, pthreads, uses kernel-space threads, as opposed to user-space threads. Kernel-space threads are also known as "heavyweight" threads, as opposed to "lightweight" user-space threads and the cost of a context switch is much higher.

In an effort to produce a much more efficient C++ concurrency implementation I investigated a couple of alternative threading libraries. Eventually however the fact that for shootout development I'm running Gentoo from a LiveCD with a RamDrive (I don't trust it sufficiently to allow it to write to my NTFS partition) and the added complexity this adds to downloading and installing a threading library together with a growing interest in the subject led me to decide to try and write my own extremely minimal threading library.

After a bit of reading around interprocessor interrupts, x86 segments and global and local descriptor tables amongst others I eventually managed to implement a very simple system which currently works under a basic test program on two compilers under windows, with or without optimisations:
#include #include "test_threads.h"void thread_function(int i){	std::cout << "thread " << i << '\n';	yield();	std::cout << "thread " << i << '\n';}int main(){	thread_t * thread1 = create_thread(thread_function, 1);	thread_t * thread2 = create_thread(thread_function, 2);	while (thread1->running && thread2->running)	{		std::cout << "main thread\n";		yield();	}	std::cout << "main thread\n";}
output:
main threadthread 1thread 2main threadthread 1thread 2main thread
I'm now working on porting the system to gcc, which is the target compiler for the language shootout. Unfortunately gcc introduces extra complications with it's alternative assembler syntax and lack of __declspec(naked) directive.

If people are sufficiently interested I may try to write up my experiences if and when I manage to develop a working language-shootout program with my own threading library. I'm sure I'm not doing things the best way possible, but it's an interesting journey and I've learnt a lot already.

?nigma

There are no comments to display.

## Create an account

Register a new account