Recent

Author Topic: Function called by multiple threads?  (Read 2720 times)

dieselnutjob

  • Full Member
  • ***
  • Posts: 217
Function called by multiple threads?
« on: March 06, 2015, 08:00:14 pm »
I know that a variable cannot be touched by multiple threads, but what about a function?

The code that calls a function will treat the output like a variable, but I can't quite figure out if it is the same.

Obviously if the function accesses a variable, and is called by the multiple threads then that would be a problem but what if it only uses local variables.  Is the local variable then created independently in each thread?

What if the function is making a OS API call and converting it into a number which is the function result?

thanks, Philip

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Function called by multiple threads?
« Reply #1 on: March 06, 2015, 08:18:07 pm »
I know that a variable cannot be touched by multiple threads, but what about a function?

The code that calls a function will treat the output like a variable, but I can't quite figure out if it is the same.

Obviously if the function accesses a variable, and is called by the multiple threads then that would be a problem but what if it only uses local variables.  Is the local variable then created independently in each thread?
Yes they are independet.

What if the function is making a OS API call and converting it into a number which is the function result?

Read up on the api function is it thread safe? if yes then you have no problem if no then you have to protect its access.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

dieselnutjob

  • Full Member
  • ***
  • Posts: 217
Re: Function called by multiple threads?
« Reply #2 on: March 06, 2015, 09:01:16 pm »
Will the "now" function be threadsafe?
as in http://www.freepascal.org/docs-html/rtl/sysutils/now.html

taazz

  • Hero Member
  • *****
  • Posts: 5368
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

 

TinyPortal © 2005-2018