Click here to Skip to main content
15,891,033 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: uninitialized pointer Pin
Cristian Amarie25-Dec-06 7:18
Cristian Amarie25-Dec-06 7:18 
GeneralRe: uninitialized pointer Pin
rp_suman25-Dec-06 14:02
rp_suman25-Dec-06 14:02 
Questioncan we get the Floppy Driver's information? Pin
jakeyjia24-Dec-06 20:55
jakeyjia24-Dec-06 20:55 
AnswerRe: can we get the Floppy Driver's information? Pin
Cristian Amarie25-Dec-06 5:36
Cristian Amarie25-Dec-06 5:36 
GeneralRe: can we get the Floppy Driver's information? [modified] Pin
jakeyjia26-Dec-06 14:13
jakeyjia26-Dec-06 14:13 
QuestionHow to control Parallel port by programming Visual C++ Pin
BK200724-Dec-06 20:55
BK200724-Dec-06 20:55 
AnswerRe: How to control Parallel port by programming Visual C++ Pin
kasturi_haribabu25-Dec-06 22:47
kasturi_haribabu25-Dec-06 22:47 
Questionmutithreading, how to synchronize threads Pin
saania khan24-Dec-06 20:26
saania khan24-Dec-06 20:26 
Hi all.

I have two threads ... one as a sender and the other a receiver for data sent over sockets. But the sockets are being created and handled in a dll ( a VC# dll which has a class named Class1). so when I call these threads I have to pass the object of the class in the dll because the sender and receiver use the same sockets right!!.

Here is what I have in the sender and receiver threads... ( and I dont know a work around for the DoEvents thing, because I have to enforce the processor to do other tasks while the thread is in a while loop .... and I have removed the doevents completely as well and it doesnt make any difference. So the problem is not with doevents).

DWORD WINAPI SenderThread(LPVOID iValue)

{

Mutex^ mut = gcnew Mutex();

mut->WaitOne();

Class1^ senderdll = *((Class1^*)iValue);

mut->Close();

int returnval;

DoEvents();

while(true)

{

returnval = senderdll->SenderEntryPoint();

if(returnval == 1) // sockets closed

{

break;

//dynamic_cast<autoresetevent^>(stateInfo)->Set();

}

}

return 0;

}

DWORD WINAPI ReceiverThread(LPVOID iValue)

{

Mutex^ mut = gcnew Mutex();

mut->WaitOne();

Class1^ receiverdll = *((Class1^*)iValue);

mut->Close ();


int returnval;

DoEvents();

while(true)

{

returnval = receiverdll->ReceiverEntryPoint();

if(returnval == 1) // the sockets closed

break;

}

return 0;

}

and here is how I am creating these threads. (The cmdListen_Click function creates the sockets.)

Class1^ c = gcnew Class1();

c->cmdListen_Click(NewPosition);//call the dll function to create sockets




////Threads start


DWORD dwGenericThread;

hThread2 = CreateThread(NULL,0,SenderThread,&c,0,&dwGenericThread);//the sender thread

if(hThread2 == NULL)

{

DWORD dwError = GetLastError();

return 0;

}

hThread3 = CreateThread(NULL,0,ReceiverThread,&c,0,&dwGenericThread);//the receiver thread

if(hThread3 == NULL)

{

DWORD dwError = GetLastError();

return 0;

}

//////Threads end

Now the two problems I am facing are:

1) How do I synchronize the threads? The AutoResetEvent (as given in http://msdn2.microsoft.com/en-us/library/system.threading.autoresetevent.aspx), uses a threadpool to create threads. My problem is if I use the threadpool instead of using the CreateThread how do I pass the Class1 object in the threads ?

2) My second problem is the AccessViolation error, which I am getting at the point where I create the instance of the Class in the thread. ( in both sender or receiver, depending upon which thread I am creating first !!).

Class1^ receiverdll = *((Class1^*)iValue);

What am I doing wrong here? When I was getting an access violation error in VC#, I used LOCK to enforce resource allocation. How do I do this in VC++?

Thanks.
AnswerRe: mutithreading, how to synchronize threads Pin
Mark Salsbery25-Dec-06 6:51
Mark Salsbery25-Dec-06 6:51 
QuestionExpandEnvironmentStrings Pin
Waldermort24-Dec-06 20:06
Waldermort24-Dec-06 20:06 
AnswerRe: ExpandEnvironmentStrings Pin
Michael Dunn24-Dec-06 20:31
sitebuilderMichael Dunn24-Dec-06 20:31 
GeneralRe: ExpandEnvironmentStrings Pin
Waldermort24-Dec-06 21:01
Waldermort24-Dec-06 21:01 
QuestionHow to running console program on background? Pin
hanlei000000000924-Dec-06 14:19
hanlei000000000924-Dec-06 14:19 
AnswerRe: How to running console program on background? Pin
Waldermort24-Dec-06 18:20
Waldermort24-Dec-06 18:20 
Generalthanks Pin
hanlei000000000924-Dec-06 18:24
hanlei000000000924-Dec-06 18:24 
QuestionStart Button color Pin
locoone24-Dec-06 11:53
locoone24-Dec-06 11:53 
AnswerRe: Start Button color Pin
Hamid_RT24-Dec-06 18:31
Hamid_RT24-Dec-06 18:31 
Questionunable to run exe Pin
ennm424-Dec-06 11:24
ennm424-Dec-06 11:24 
AnswerRe: unable to run exe Pin
Waldermort24-Dec-06 13:54
Waldermort24-Dec-06 13:54 
GeneralRe: unable to run exe Pin
ennm424-Dec-06 15:13
ennm424-Dec-06 15:13 
GeneralRe: unable to run exe Pin
Waldermort24-Dec-06 18:04
Waldermort24-Dec-06 18:04 
GeneralRe: unable to run exe Pin
ennm424-Dec-06 18:56
ennm424-Dec-06 18:56 
GeneralRe: unable to run exe Pin
Waldermort24-Dec-06 20:24
Waldermort24-Dec-06 20:24 
GeneralRe: unable to run exe Pin
S Douglas25-Dec-06 20:05
professionalS Douglas25-Dec-06 20:05 
AnswerRe: unable to run exe Pin
Mark Salsbery25-Dec-06 6:59
Mark Salsbery25-Dec-06 6:59 

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.