Announcement

Collapse
No announcement yet.

someting to whish for

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Fred Oxenby
    replied
    Lance,
    I was not really thinking of a "threadsafe FreeFile" function,
    more a tread-safe "File Open " function witch is returning a filehandle
    that I can use in other file-related functions like SEEK,GET,GET$ etc.
    ------
    But that is perhaps as hard to implement.
    The only way out I can see here is to implement FreeFile in a dll
    as a function and use critical section to create and maintain "filenumbers"
    Code:
    In a DLL
    Global MyFreeFile As CRITICAL_SECTION
      
    Function GetFileHandle()as long
    Static MyFileHandle&
     EnterCriticalSection MyFreeFile 
      Incr MyFileHandle&
      If MyFileHandle& > 2,000,000,000 then MyFileHandle& = 1
      Function = MyFileHandle& 
     LeaveCriticalSection MyFreeFile
    End Function
    --
    Anywhere in your code
     FilNr& = GetFileHandle
     Open MyFile$ for binary ... as #FilNr&
     Get FilNr&,....
     Close FilNr&
    ------------------
    Fred
    mailto:[email protected][email protected]</A>
    http://www.oxenby.se



    [This message has been edited by Fred Oxenby (edited January 30, 2001).]

    Leave a comment:


  • John Kovacich
    replied
    Which is why I suggested both the FREEFILE and OPEN befor calling
    the thread. This would ensure that you pass a different file number
    to each thread (at the expense of opening a file for EVERY thread,
    whether it needed one or not).

    The thread would have to SHARE the file with the main program.


    ------------------
    Thanks,

    John Kovacich

    Leave a comment:


  • Eric Pearson
    replied
    John --

    Yes.

    The only issue is that FREEFILE is not currently thread-safe. If FREEFILE is called by thread A, and then thread B calls it before thread A "claims" the file number by using OPEN, then thread B will get the same "free" file number as thread A. Whichever thread uses OPEN first will work, and when the other thread tries to OPEN it will fail, and ERR will be set. So always doing FREEFILE in your main thread should be completely safe.

    -- Eric


    ------------------
    Perfect Sync: Perfect Sync Development Tools
    Email: mailto:[email protected][email protected]</A>

    [This message has been edited by Eric Pearson (edited January 30, 2001).]

    Leave a comment:


  • John Kovacich
    replied
    Can you issue FREEFILE and OPEN before calling the thread, and pass the
    file number? Would that then be safe?

    ------------------
    Thanks,

    John Kovacich

    Leave a comment:


  • Peter Manders
    replied
    Lance,

    I think it can only be done if you create a function to release filehandles again:

    fHandle = GetFileHandle
    Open filename for Output as fHandle
    ...
    ...
    CloseAndRelease fHandle

    I could make a threadsafe function to replace FREEFILE and OPEN but it would be convenient to have a Basic function for this.


    Peter


    ------------------
    [email protected]

    Leave a comment:


  • Lance Edmonds
    replied
    FREEFILE itself does not allocate any file numbers, so this gets complicated when multiple threads are involved.

    The most common solution is to simply restrict each thread to use a specific range of file number values based on the thread's given ID. Since a file nunber can be in the range of 1 to 2147483647, there is a big range of values to choose from!

    Another approach is to use a Critical Section in a function that issues and tracks file numbers for the applications threads. However, this can be a lot of work compared to a restricted-range approach.

    Regardless, I'll certainly pass your suggestion along. It would be interesting to see how R&D would solve such a problem, given that there is no way to predict how many threads an app may spawn.

    ------------------
    Lance
    PowerBASIC Support
    mailto:[email protected][email protected]</A>

    Leave a comment:


  • Fred Oxenby
    started a topic someting to whish for

    someting to whish for

    I would like PB to supply a threadsafe freefile/filenumber.
    Like this perhaps:
    FileOpen FileName$ for binary access read lock write TO Filenumber&
    -------


    ------------------
    Fred
    mailto:[email protected][email protected]</A>
    http://www.oxenby.se



    [This message has been edited by Fred Oxenby (edited January 29, 2001).]
Working...
X