Multi threading issues

New to FreeBASIC? Post your questions here.
Post Reply
kraton
Posts: 2
Joined: Apr 04, 2023 8:24

Multi threading issues

Post by kraton »

Hello, everyone. I'm very sorry. I asked through machine translation. I want to search all files through multithreading to learn about multithreading. After writing the following code, I never get the correct output. How can I fix the following code.

Code: Select all

#include once "windows.bi"

Const MAX_THREADS = 4
Dim Shared As Integer thread_handles(MAX_THREADS)
Dim Shared As Integer thread_ids(MAX_THREADS)

Dim Shared As Integer thread_idx = 0


Sub search_files(ByRef directory As String)

    Dim As WIN32_FIND_DATAA find_data
    Dim As HANDLE find_handle

    
    Dim As String * 255 search_path = directory & "*.*"

    
    find_handle = FindFirstFile(search_path, @find_data)

    
    If (find_handle = INVALID_HANDLE_VALUE) Then
        Return
    End If

    
    Do
        
        If (find_data.cFileName <> "." And find_data.cFileName <> "..") Then

            
            Print directory & find_data.cFileName

        End If
    Loop While FindNextFile(find_handle, @find_data)

    
    FindClose find_handle

End Sub


Function thread_func(directory As String Ptr) As Integer

    search_files(*directory)

    Return 0

End Function





    Dim As String * 250 directory = "c:\"
    
    
     

    
    For i As Integer = 0 To MAX_THREADS - 1
         
         If thread_idx >= MAX_THREADS Then Exit For
        thread_handles(i) = ThreadCreate(@thread_func,  @directory)

        
        thread_ids(i) = thread_idx

        
        thread_idx += 1

    Next

   
    For i As Integer = 0 To MAX_THREADS - 1

        
        ThreadWait @thread_handles(i)

    Next

    Sleep

fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Multi threading issues

Post by fxm »

What do you think is the "correct" output (you are looking to achieve) ?
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Multi threading issues

Post by fxm »

Or maybe this (4 times the file list, but interleaved):

Code: Select all

#include once "windows.bi"

Const MAX_THREADS = 4
Dim As Any Ptr thread_handles(MAX_THREADS - 1)
Dim As Integer thread_ids(MAX_THREADS - 1)
Dim As Integer thread_idx = 0

Sub search_files(Byval p As Any Ptr)
    Dim As WIN32_FIND_DATAA find_data
    Dim As HANDLE find_handle
    Dim As Zstring Ptr pdir = p

    find_handle = FindFirstFile(*pdir & "*.*", @find_data)

    If (find_handle = INVALID_HANDLE_VALUE) Then
        Return
    End If

    Do
        If (find_data.cFileName <> "." And find_data.cFileName <> "..") Then
            Print *pdir & find_data.cFileName
        End If
    Loop While FindNextFile(find_handle, @find_data)

    FindClose find_handle

End Sub


Dim As Zstring Ptr pdir = @"c:\"

For i As Integer = 0 To MAX_THREADS - 1
    thread_handles(i) = ThreadCreate(@search_files, pdir)
    thread_ids(i) = thread_idx
    thread_idx += 1
Next
   
For i As Integer = 0 To MAX_THREADS - 1
    ThreadWait thread_handles(i)
Next

Sleep
kraton
Posts: 2
Joined: Apr 04, 2023 8:24

Re: Multi threading issues

Post by kraton »

Thank you very much for your reply. The effect I want to achieve is to multithread search for all files with the specified extension in the specified directory. I found a topic on the forum, but it has expired. I have modified the code, how can I search for all sub items without recursion? And want to know how many threads are suitable for searching for files?

Code: Select all

#include once "windows.bi"

Const MAX_THREADS = 4
Dim Shared As Any Ptr thread_handles '(MAX_THREADS - 1)
Dim Shared As Integer thread_ids(MAX_THREADS - 1)

Dim Shared As Integer thread_idx = 0

Type ThreadData
  As Integer id 
  As ZString * 255 path
  As ZString  * 8 szFilter

End Type



Sub search_files(ByVal p As Any Ptr)
    Dim As WIN32_FIND_DATAA find_data
    Dim As HANDLE find_handle
    Dim As ThreadData Ptr pz = p

    Dim As ZString * 255 search_path = pz->path & "*.*"

    find_handle = FindFirstFile(search_path, @find_data)

    If (find_handle = INVALID_HANDLE_VALUE) Then
        Return
    End If

    Do
    	If (find_data.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) = 0 Then
    			If LCase(Right(find_data.cFileName, 4)) = pz->szFilter Then
    				 Print pz->path & find_data.cFileName
    				 pz->id += 1
    				 
    				 
    			End If
    		
    	Else
    		
		        If (find_data.cFileName <> "." And find_data.cFileName <> "..") Then
					'Print pz->path & find_data.cFileName & "\*.*"		
					Dim As ZString * 255 tpath = pz->path & find_data.cFileName & "\*.*"    
					?tpath
		        	'search_files(@tpath)
		        	  pz->id += 1
		        End If
        EndIf
    Loop While FindNextFile(find_handle, @find_data)

    FindClose find_handle

End Sub

Dim td As ThreadData 

Dim As ZString  * 255 pdir = "C:\"
Dim As ZString  * 255 pFilter = ".exe"

td.path =  pdir
td.szFilter =  pFilter

 thread_handles = ThreadCreate(@search_files, Cast(Any Ptr , @td))
   ThreadWait thread_handles
 
/'For i As Integer = 0 To MAX_THREADS - 1
    thread_handles(i) = ThreadCreate(@search_files, Cast(Any Ptr , @td))
    thread_ids(i) = thread_idx
    thread_idx += 1
Next
 
For i As Integer = 0 To MAX_THREADS - 1
    ThreadWait thread_handles(i)
Next
  '/
  ? td.id
Sleep
Post Reply