Is this safe? I have a server that writes to a file and I want multiple apps to be able to call the server to write new files. If they call while the last function is still executing while this be a problem or is the state saved?
for example
void FileManager_StoreFile(const char* writeData)
{
FILE* fp = fopen(filename,“w+”);
//<— Another client calls api
//write to file etc
}
will write Data be lost if another client calls the function before it is written? if so how can I fix this?
Actually I don’t think that will work it says for threads in the same process theres not really a way for me to get a reference to the semaphore from outside of the executable?
You really only need the reference to the semaphore within the executable providing the API.
Basically, you could create a semaphore in the COMPONENT_INIT section with an initial count of 1 then use that reference in your API function(s).
For example:
COMPONENT_INIT
{
/*
* Create a semaphore with an initial count of 1
* This signals that 1 resource (a single file) is available
*/
}
void FileManager_StoreFile(const char* writeData)
{
/*
* Pend (wait) on the semaphore using either:
* le_sem_WaitWithTimeOut()
* le_sem_Wait()
* le_sem_TryWait()
* Note: the first client calling the API will `consume` the semaphore
*/
// If le_sem above returned LE_OK, it is safe to `use` the resource.
FILE* fp = fopen(filename,“w+”);
// ... do stuff with the file, etc.
/*
* *** <— Another client calls api ***
* Other clients won't be able to acquire the resource until
* the `consuming` client releases (posts) it.
* The behavior will depend on which `waiting` method you chose to implement.
*/
// Finally, Post the semaphore so other clients may `use` it.
le_sem_Post(MySemRef);
}