Click here to Skip to main content
16,004,782 members

Comments by haraldhubbes (Top 4 by date)

haraldhubbes 28-Oct-12 17:04pm View    
None of the above listed problems applies, as my processor has 12 cores, the threads can work independently and do not change any information of the class. Each thread is writing its calculated results into a separate file. The programm is not working properly in the sense that in the course of running it appears at a sudden to be stuck in one thread for a while and then again it continues the execution. Apparently there is a command missing which ensures a smooth execution accross the threads.
haraldhubbes 28-Oct-12 16:11pm View    
MAX_THREADS is global integer denoting the number of threads to be called and max_threadsn is a typo and should read MAX_THREADS.
haraldhubbes 22-Oct-12 18:03pm View    
Deleted
Can I come back to my question with regard to static member functions and threading.
My initial issue as to being not able to access class information from the (static) thread function is solved as far as I can say, by following your advise. But the situation is not fully satisfactory.
1) My thread function is supposed to write calculated results using
ofstream bond_data;
bond_data.open(FILE_NAME, ios::trunc);
// calculating some stuff
bond_data << results << EL;
bond_data.close();

this seems not to work properly. Can this be explained? Do I have to use fprinf(..) instead?

2) I would like, if possible being assured that my approach as outlined below is correct. I am afraid that some issues have not been dealt with properly such as synchronization of threads, which has been explicitly addressed in your comments to my initial question. But I am not fully aware how to do that. Please have a look:

// define the Thread Data Structure to be transferred to the (static) thread function
// --------------------------
typedef struct Thread_Data{
MC_Simulation* mc_simulation; // this solved my initial problem
long first_scenario;
long last_scenario;
char bond_data_file[_MAX_FNAME];
} THREAD_DATA, *PTHREAD_DATA;
// -----------------------------------
// member function in MC_Simulation:
Void initiate_threads(void){
// call threads
for (int index=0; index<max_threads;index++){
pthread_arg[index] = (PTHREAD_DATA) HeapAlloc(GetProcessHeap(),
HEAP_ZERO_MEMORY,sizeof(THREAD_DATA));
pthread_arg[index]->mc_simulation = this;
//...
hThread[index] = CreateThread(NULL, 0, thread_function ,
pthread_arg[index], 0, &dwThreadID[index]);
} // end for loop
// Wait until all threads have terminated.
WaitForMultipleObjects(MAX_THREADS, hThread, TRUE, INFINITE);
for(int i=0; i < MAX_THREADS; i++) {
CloseHandle(hThread[i]);
if(pthread_arg[i] != NULL){
HeapFree(GetProcessHeap(), 0, pthread_arg[i]);
pthread_arg[i] = NULL;} // Ensure address is not reused.
} // end for loop
} // end thread calling function
haraldhubbes 13-Oct-12 8:44am View    
Hello Forum Members

As the person having asked the question I have to thank you for all the answers.
I would particularly like to thank Mr Sergey Alexandrovich for his comments. In the first place I am glad that my question apparently has not been too displaced. Now I need to digest all the suggestions.

For now, I must understand that actually my class MC_Simulation will not be made a "copy" of for each thread. This would not work in a 32bit environment. With my huge amount of scenario data I approach 4gb memory.

To be more precise, in the msdn article, which I mentioned in my question, rightly allocates memory to the parameter ("pDataArray"), which is sent to the threadfunction as LPVOID arg. The memory which is consumed by the pointer to the threadfunction must not be too much.... (at least for the time being until I have progressed to 64b compiling).

I am not so familiar with the voting rules of code project. My impression is that all solutions are somehow similar. For now I will go for solution two, which gives a code example. Can the administrator advise me, whether I can grade all solutions highly or only one of them?
Harald Hubbes