I have a case where a particular API needs to be called only once to initialize. Thereafter threads can call the other methods. In vuser_init.c, I have this:
static volatile int initOnlyOnce = 0;
static volatile int initComplete = 0;
int iStatus;
if (1!=initOnlyOnce ) {
initOnlyOnce =1;
lr_output_message("Before, initComplete = %d", initComplete );
iStatus = Initialize(); // product API call
initComplete = 1;
lr_output_message("After, initComplete = %d", initComplete );
if (1 != iStatus ) {
lr_error_message("Initialize returns %d on %s.",iStatus,szLoadGenerator);
srand(time(NULL));
}
}
When I run the scenario, the first thread prints both, i.e.
Before, initComplete = 0
followed by
After, initComplete = 1
and the rest of the test is done in that thread, correctly. However, the next thread fails with "Error -- memory violation : Exception ACCESS_VIOLATION received" because it has executed a subsequent method without the Initialize being completed or initComplete was 0. The logs for each of the other threads have "Before, initComplete = 0" as the last line. Since I have defined these variables as static volatile, I expected that
initOnlyOnce = 1
right after the first thread called it and subsequently this block of code would not be entered again. However, it seems that Vugen is not the same as a thread. Each vuser_init.c is independent of the other Vugens running and so the state is not shared although variables are declared static volatile.
Is there a workaround? I essentially want a singleton precursor to the vuser_init.