Click here to Skip to main content
15,886,724 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionNeed help on finishing this basic C program [modified] Pin
biggiant2200029-Apr-09 19:35
biggiant2200029-Apr-09 19:35 
AnswerRe: Need help on finishing this basic C program Pin
_AnsHUMAN_ 29-Apr-09 19:57
_AnsHUMAN_ 29-Apr-09 19:57 
GeneralRe: Need help on finishing this basic C program Pin
biggiant220001-May-09 6:29
biggiant220001-May-09 6:29 
AnswerRe: Need help on finishing this basic C program Pin
Michael Schubert29-Apr-09 22:12
Michael Schubert29-Apr-09 22:12 
QuestionRe: Need help on finishing this basic C program Pin
David Crow30-Apr-09 4:26
David Crow30-Apr-09 4:26 
AnswerRe: Need help on finishing this basic C program Pin
biggiant220001-May-09 6:07
biggiant220001-May-09 6:07 
QuestionRe: Need help on finishing this basic C program Pin
David Crow1-May-09 6:10
David Crow1-May-09 6:10 
QuestionMultithreading help in C++ Pin
Kiran Satish29-Apr-09 16:59
Kiran Satish29-Apr-09 16:59 
Hi all,

I am having a c++ file which is used to create mexfunction DLL that can be used to call a function from Matlab through dll. This is used to compute some mathematical calculations on a big matirx and for this I do it in two parallel threads each doing half matrix. At the end I combine both to generate full resultant matrix. For this, I create two different threads as follows-
HANDLE thd_handle[2];
DWORD WINAPI GenerateThread0(LPVOID param);
DWORD WINAPI GenerateThread1(LPVOID param);
void CreateResult0();
void CreateResult1();

void mexFunction(...mex function params....)
{
// do necessary variable declarations
// create two threads
thd_handle[0] = CreateThread(NULL,GenerateThread0, NULL, 0, NULL);
thd_handle[1] = CreateThread(NULL,GenerateThread1, NULL, 0, NULL);
WaitForMultipleObjects(2,thd_handle,true,INFINITE);
// prepare result
// return result to matlab
}
DWORD WINAPI GenerateThread0(LPVOID param)
{
CreateResult0();
return 0;
}
DWORD WINAPI GenerateThread1(LPVOID param)
{
CreateResult1();
return 0;
}
void CreateResult0()
{
//do some computations
}
void CreateResult1()
{
//do some computations
}

Everything is fine and good while doing as above and I used this for a long time without any problems. Now I am planning to make this more efficient by making it multithreading adaptable to the processor on which it runs. I have couple of systems which are like 4core/8core and would like to create threads dynamically. I came up with the following idea and implemented it and guess what it gives fatal errors while running and I highly suspect the way I am creating multiple threads.
HANDLE *thd_handle;
DWORD WINAPI GenerateThread(LPVOID param);
void CreateResult(unsigned int idx);

void mexFunction(...mexfunctionparams...)
{
//read in input params
//find (#processors*#logicalprocs*#coresperproc)=m_nThreads;
thd_handle = new HANDLE[m_nThreads-1];
//CreateThreads
for (unsigned int i=0; i < m_nThreads-1; i++ )
thd_handle[i] = CreateThread(NULL, GenerateThread, &i, 0, NULL);
WaitForMultipleObjects(m_nThreads-1,thd_handle, true,infinite);
//prepare result
//return result to matlab
}
DWORD WINAPI GenerateThread(LPVOID param)
{
DWORD iter = *(DWORD*)param;
CreateResult(iter);
retrun 0;
}
void CreateResult(unsigned int idx)
{
//compute result for this matrix size depending on idx
}

if I use the dll generated from this code, the program crashes... am I doing something fundamentally wrong here?? is there any other better way to achieve this??

thanks for any help,

PKNT

AnswerRe: Multithreading help in C++ Pin
Stuart Dootson29-Apr-09 20:48
professionalStuart Dootson29-Apr-09 20:48 
GeneralRe: Multithreading help in C++ Pin
Kiran Satish30-Apr-09 5:15
Kiran Satish30-Apr-09 5:15 
GeneralRe: Multithreading help in C++ Pin
Stuart Dootson30-Apr-09 6:16
professionalStuart Dootson30-Apr-09 6:16 
GeneralRe: Multithreading help in C++ Pin
Kiran Satish30-Apr-09 6:50
Kiran Satish30-Apr-09 6:50 
GeneralRe: Multithreading help in C++ Pin
Stuart Dootson30-Apr-09 6:55
professionalStuart Dootson30-Apr-09 6:55 
AnswerRe: Multithreading help in C++ Pin
KarstenK29-Apr-09 21:17
mveKarstenK29-Apr-09 21:17 
GeneralRe: Multithreading help in C++ Pin
Kiran Satish30-Apr-09 3:27
Kiran Satish30-Apr-09 3:27 
GeneralRe: Multithreading help in C++ Pin
KarstenK30-Apr-09 3:43
mveKarstenK30-Apr-09 3:43 
GeneralRe: Multithreading help in C++ Pin
Kiran Satish30-Apr-09 5:03
Kiran Satish30-Apr-09 5:03 
AnswerRe: Multithreading help in C++ Pin
Joe Woodbury30-Apr-09 19:35
professionalJoe Woodbury30-Apr-09 19:35 
QuestionHow can I make a combobox without using MFC Pin
CodingLover29-Apr-09 16:24
CodingLover29-Apr-09 16:24 
AnswerRe: How can I make a combobox without using MFC Pin
Naveen29-Apr-09 17:56
Naveen29-Apr-09 17:56 
QuestionCTreeCtrl - Get the number of the selected item Pin
Gagnon Claude29-Apr-09 8:42
Gagnon Claude29-Apr-09 8:42 
QuestionRe: CTreeCtrl - Get the number of the selected item Pin
David Crow29-Apr-09 9:52
David Crow29-Apr-09 9:52 
AnswerRe: CTreeCtrl - Get the number of the selected item Pin
Stuart Dootson29-Apr-09 10:51
professionalStuart Dootson29-Apr-09 10:51 
GeneralRe: CTreeCtrl - Get the number of the selected item Pin
led mike29-Apr-09 11:10
led mike29-Apr-09 11:10 
GeneralRe: CTreeCtrl - Get the number of the selected item Pin
Stuart Dootson29-Apr-09 11:48
professionalStuart Dootson29-Apr-09 11:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.