Idea for Threads

Craig Black cblack at ara.com
Fri May 11 15:57:57 PDT 2007


Correct me if I'm wrong, but the synchronize statement can be used to make a 
function, block of code, or variable atomic.  That is, only one thread at a 
time can access it. However, one very important objective of multithreading 
is to make programs faster by using todays multi-core processors.  Using 
synchronize too much would make things run slower, because it could cause a 
lot of thread contention.

I was thinking about this when I got an idea.  It would require another 
keyword that could be used to mark a function or block of code.  Perhaps 
"threaded" or "threadsafe".  This keyword would not force the code to be 
atomic.  Instead, it would cause the compiler to issue errors when the code 
does something that is not thread safe, like writing to nonsynchronized 
data.

I don't have a lot of experienct with threads so I don't know all the 
implications here.  I'm not sure if the compiler has enough knowledge to 
prohibit everything that could be the source of threading problems.  But 
even if it could enforce the most common bugs then that would be a very good 
thing.

Comments?

-Craig 





More information about the Digitalmars-d mailing list