Threads

Part of Standard Library Category

Warning: This code won't compile with recent versions of DMD/GDC

This example needs to be re-written to use std.file.listdir since it uses std.recls (which is no longer part of Phobos).

See also: Threads and Delegates Example

Description

Demonstrates the use of std.thread with two threads.

Example

/* You might need to adjust these two paths to fit your own computer: */
char[] path1 = "c:\\windows\\fonts"; 
char[] path2 = "c:\\windows\\temp"; 


import std.recls;
import std.thread;


int run1(void *ptr)
{
    printf("Thread 1 started...\n");
    Search  search  =   new Search(path1, "*.*", RECLS_FLAG.RECLS_F_RECURSIVE);
    foreach(Entry entry; search)
    {
        with(entry)
        { 
            if(!ShortFile())
                printf("T1: %.*s%.*s\n", DirectoryPath(), File());
            else

                printf("T1: %.*s%.*s (%.*s)\n", DirectoryPath(), File(), ShortFile());
        }
    }     
    printf("Thread 1 finished.\n"); 
    return 0;
}

int run2(void *ptr)
{
    printf("Thread 2 started...\n");    
    Search  search  =   new Search(path2, "*.*", RECLS_FLAG.RECLS_F_RECURSIVE);
    foreach(Entry entry; search)
    {
        with(entry)
        { 
            if(!ShortFile())
                printf("T2: %.*s%.*s\n", DirectoryPath(), File());
            else

                printf("T2: %.*s%.*s (%.*s)\n", DirectoryPath(), File(), ShortFile());
        }
    }     
    printf("Thread 2 finished.\n"); 
    return 0;
}

void main()
{   
    Thread t1 = new Thread(&run1, null);    
    Thread t2 = new Thread(&run2, null);    
    
    t1.start();
    t2.start();
    
    while(t1.getState() == std.thread.Thread.TS.RUNNING)
        Thread.yield ();    
}

Compile Tips

You may need to make the recls.lib as explained in ReclsExample.

Sample batch file:

@echo off
dmd threads.d recls.lib
pause
threads.exe
pause

Source

Thanks to Burton Radons for his helpful posts on this topic (D:10231 and D:10236).