Click here to Skip to main content
15,909,498 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionRe: Using IWebBrowser2 and Navigate. Need help! Thanks! Pin
David Crow8-Aug-06 8:38
David Crow8-Aug-06 8:38 
AnswerRe: Using IWebBrowser2 and Navigate. Need help! Thanks! Pin
Paul Groetzner8-Aug-06 9:33
Paul Groetzner8-Aug-06 9:33 
QuestionRe: Using IWebBrowser2 and Navigate. Need help! Thanks! Pin
David Crow8-Aug-06 9:39
David Crow8-Aug-06 9:39 
AnswerRe: Using IWebBrowser2 and Navigate. Need help! Thanks! Pin
Paul Groetzner8-Aug-06 10:09
Paul Groetzner8-Aug-06 10:09 
GeneralRe: Using IWebBrowser2 and Navigate. Need help! Thanks! Pin
David Crow8-Aug-06 10:48
David Crow8-Aug-06 10:48 
AnswerRe: Using IWebBrowser2 and Navigate. Need help! Thanks! Pin
Paul Groetzner8-Aug-06 10:12
Paul Groetzner8-Aug-06 10:12 
QuestionResizing Floating ToolBars question... Pin
Maximilien8-Aug-06 4:56
Maximilien8-Aug-06 4:56 
QuestionThread control problem, please help! Pin
Yoskie8-Aug-06 4:46
Yoskie8-Aug-06 4:46 
Hi all,
OK, this is my first multi-threaded program ever. It's more a proof of concept rather than anything really useful. The main problem is that it's not working as I intended.
Overwiev:
An m*n array (double**) is filled with random numbers from -5 to 5. After that a thread gets created for each row of the array (i.e. we know that exactly m threads will be created). That thread will add all entries in that row, write the result to a particular memory location (double *result), decremnt the threadcount and exit.
Problem:
I want main() to create m-threads and then wait until the last thread exits. Then main is supposed to print the result to the screen and exit. So far I can't get main to wait on the threads. It continues execution despite my best efforts.Confused | :confused:
The code:
See below. It compiles on Microsofts Visual C++ 6.0. I have a lot of
#ifdef Mutex2... in there 'cos I want it to port it to Unix as well. Any hints in that direction are very much appreciated.
Thanks much
Mike

/////////////////////////////////////
#include <stdlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#ifdef WIN32
#include <windows.h>
#include <process.h>
#endif
#ifdef SOLARIS
// ?????? POSIX threads maybe?
#endif
//////// definitions ///////////
#ifdef WIN32
#define MUTEX1 1
#define MUTEX2 0
#endif
#ifdef SOLARIS
#define MUTEX1 0
#define MUTEX2 1
#endif
//////// data struct ///////////

struct data{
int size;
int id;
double *row;
double *result;
};
//////// globals ///////////////

int threadcount;
#if MUTEX1
HANDLE hMutex;
HANDLE doneMutex;
#endif
#if MUTEX2

#endif
//////// functions /////////////

void sum(void*);

////////////////////////////////

int main(int argc, char *argv[])
{
if(argc != 3){
printf("useage: <exe> <# of rows > 0> <#of columns > 0>\n");
exit(0);
}
int i,j;
int row = atoi(argv[1]);
int col = atoi(argv[2]);
double **arr = NULL;
double *result;
struct data *list;
struct data *ptrlist;
#if MUTEX1
hMutex = CreateMutex(NULL, false, NULL);
doneMutex = CreateMutex(NULL, true, NULL);
#endif
#if MUTEX2
#endif
list = new data[row];
arr = new double*[row];
for(i = 0; i < row; i++){
arr[i] = new double[col];
}
result = new double[row];
for(i = 0; i < row; i++){
for(j = 0; j < col; j++){
arr[i][j] = -5 + rand() % 11;
}
}
list->size = col;
for(i = 0; i < row; i++){
result[i] = -99;
}
threadcount = row;
#if MUTEX1
WaitForSingleObject( doneMutex, INFINITE );
// I was hoping that main() would suspend execution when it hits the 2nd WaitForSingleObject( doneMutex, INFINITE )
// a few lines further down. It doesn't. Why?
#endif
#if MUTEX2
#endif
///////////////////////////
for(i = 0; i < row; i++){
list[i].size = col;
list[i].id = i;
list[i].row = arr[i];
list[i].result = result;
ptrlist = list+i;
#if MUTEX1
_beginthread(sum, 0, (void*)ptrlist);
#endif
#if MUTEX2
#endif
}
#if MUTEX1
WaitForSingleObject( doneMutex, INFINITE );
// I was hoping to lock the doneMutex and have the last thread (i.e. threadcount == 0) unlock it.
// ...that strategy doesn't do the job. main() does not wait until the last thread releases this mutex. Why?
#endif
#if MUTEX2
#endif
// This while-loop should never execute IF main() would wait for the doneMutex to be released by the last thread upon exit.
// Sadly it doesn't care.
while(threadcount > 0){
// while this appears to work, I really want main to wait until the last thread exits.

printf("threadcount = %i\n", threadcount);
}
for(i = 0; i < row; i++){
printf("%i. %f\n", i, result[i]);
}
#if MUTEX1
ReleaseMutex( doneMutex );
#endif
#if MUTEX2

#endif
for(i = 0; i < row; i++){
delete [] arr[i];
}
delete [] arr;
delete [] result;
delete [] list;
return(1);
}

//****************************************

void sum(void *arg)
{
double sum = 0;
int i;
struct data *list = (struct data*)arg;
for(i = 0; i < list->size; i++){
sum += list->row[i];
}
list->result[list->id] = sum;
#if MUTEX1
WaitForSingleObject( hMutex, INFINITE );
#endif
#if MUTEX2
#endif
threadcount--;
printf("ID = %i sum = %f\n", list->id, sum);
if(threadcount == 0){
ReleaseMutex( doneMutex );
// Here I figured the last thread exits and releases the Mutex for main().
// Anyway, main() dosn't appear to wait on this Mutex, it continues execution regardless
}
#if MUTEX1
ReleaseMutex( hMutex );
_endthread();
#endif
#if MUTEX2
#endif
}

//****************************************
AnswerRe: Thread control problem, please help! Pin
Chris Losinger8-Aug-06 5:35
professionalChris Losinger8-Aug-06 5:35 
GeneralRe: Thread control problem, please help! Pin
Yoskie8-Aug-06 6:20
Yoskie8-Aug-06 6:20 
GeneralRe: Thread control problem, please help! Pin
Chris Losinger8-Aug-06 6:38
professionalChris Losinger8-Aug-06 6:38 
AnswerRe: Thread control problem, please help! Pin
Zac Howland8-Aug-06 9:21
Zac Howland8-Aug-06 9:21 
QuestionMessage Removed Pin
8-Aug-06 4:18
Reza Shademani8-Aug-06 4:18 
AnswerRe: MySql API for C++ [modified] Pin
bob169728-Aug-06 9:27
bob169728-Aug-06 9:27 
AnswerRe: MySql API for C++ Pin
flippydeflippydebop8-Aug-06 20:24
flippydeflippydebop8-Aug-06 20:24 
Questionhow to use _TrackMouseEvent to track mouse movement over a button Pin
Manjunath S8-Aug-06 4:00
Manjunath S8-Aug-06 4:00 
AnswerRe: how to use _TrackMouseEvent to track mouse movement over a button Pin
Viorel.8-Aug-06 4:50
Viorel.8-Aug-06 4:50 
QuestionTCP Port 80 Pin
wrjksdf8-Aug-06 3:47
wrjksdf8-Aug-06 3:47 
AnswerRe: TCP Port 80 Pin
fredsparkle8-Aug-06 4:37
fredsparkle8-Aug-06 4:37 
AnswerRe: TCP Port 80 Pin
wrjksdf8-Aug-06 5:31
wrjksdf8-Aug-06 5:31 
GeneralRe: TCP Port 80 Pin
charlieg8-Aug-06 6:45
charlieg8-Aug-06 6:45 
QuestionLOGBRUSH and CreateBrushIndirect on Win98 Pin
includeh108-Aug-06 2:21
includeh108-Aug-06 2:21 
AnswerRe: LOGBRUSH and CreateBrushIndirect on Win98 Pin
Viorel.8-Aug-06 3:07
Viorel.8-Aug-06 3:07 
QuestionDifference between C++ Structures and C++ Classes Pin
Subramaniam s.V.8-Aug-06 2:21
Subramaniam s.V.8-Aug-06 2:21 
AnswerRe: Difference between C++ Structures and C++ Classes Pin
includeh108-Aug-06 2:24
includeh108-Aug-06 2:24 

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.