Concurrency architecture for D2

Phil Deets pjdeets2 at gmail.com
Mon Dec 28 08:09:52 PST 2009


On Sun, 27 Dec 2009 15:32:52 -0500, Andrei Alexandrescu  
<SeeWebsiteForEmail at erdani.org> wrote:

> That's why I'm thinking of creating a mailing list or maybe another  
> group for this. Any ideas on what would be the best approach? I also  
> want to gauge interest from threading experts who'd like to participate.  
> Please advise: (a) whether you would like to participate to the design;  
> (b) keep discussions on the general group; (c) create a separate  
> newsgroup; (d) create a mailing list. The latter would have open  
> enrollment.

I probably wouldn't contribute much, but I'm satisfied as long as I can  
view the discussion. I have learned a lot from the D newsgroups.

I would strongly prefer a newsgroup to a mailing list. I wish every  
mailing list on the Internet would convert to a newsgroup.

The rest of this message is just a dump of my limited experience with  
threading in case its useful for you to see the perspective of someone  
without a lot of threading experience.

My only major experience with concurrency involved a program which was  
split into a GUI (using C#/WPF) and a C++ DLL with three always-on threads  
plus a thread from C# coming in occasionally using PInvoke. The three  
threads in the C++ side were in while(true) loops which checked volatile  
boolean flags for work and slept briefly when there was nothing to do.  
(See below for a general idea of what the code looked like.) I didn't use  
any synchronization except the volatile flags and global volatile sets of  
parameters. This worked for me since only one thread handled any one piece  
of functionality. So some built-in way of low overhead cross-thread  
function calling would be nice. Or if there was a better way to do it, a  
design that would nudge me in the right direction would be good.

Actually, after looking at the code below, I'm not convinced I got it  
right since TakeData could possibly be called before thread1_buffer got  
filled.

globals:
volatile bool take_data_flag = false;
volatile char* take_data_buffer = null; // constant buffer size (something  
like 32KiB)

thread1:
void TakeData(char* buffer) {
	take_data_buffer = buffer;
	take_data_flag = true;
	take_data_called = true;
}

bool take_data_called = false;
char thread1_buffer[BUFFER_SIZE];
while (true) {
	if (!take_data_called) {
		// Add data to thread1_buffer
		if (<buffer is full>) {
			TakeData(thread1_buffer);
		}
	} else {
		if (take_data_flag == false) {
			// ok to use buffer again
			take_data_called = false;
		}
	}
	sleep(50);
}

thread2:
char thread2_buffer[BUFFER_SIZE];
while (true) {
	if (take_data_flag) {
		// copy take_data_buffer to thread2_buffer
		take_data_flag = false;
		// process thread2_buffer
	}
	else
		sleep(50);
}



More information about the Digitalmars-d mailing list