Multicores ========== Since computers with multiple cores are the norm today, let us discuss how this can be exploited with D. While concurrency and parallelism are `actually different concepts `_, we will not discuss them separately, because programmers usually encounter them at same time anyways. However, this means we only provide an overview here. Threads ------- The basic mechanism for threads managed by the operating system (PThread) is provided by ``std.concurrency``. .. literalinclude:: ../examplecode/threads.d :language: d Once you use threads, keep in mind that in D global variables are thread-local. You can declare them global to the whole process with the ``shared`` qualifier. .. code-block:: d shared int countA; // global counter int countB; // thread-local counter Tasks ----- This is about the lightweight thread variant, which is managed by the program itself. It is also called user-level threads. Since the tasks are usually executed by a thread pool, they are run in parallel as well. .. literalinclude:: ../examplecode/tasks.d :language: d Synchronization --------------- Once you have parallelism, usually concurrency arises and the need for synchronization. Of course, D provides the ususal suspects: Locks, semaphores, atomic variables. Memory Model ------------