Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. mutithreading, how to synchronize threads

mutithreading, how to synchronize threads

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestioncsharpcomtutorial
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    saania khan
    wrote on last edited by
    #1

    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(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 b

    M 1 Reply Last reply
    0
    • S saania khan

      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(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 b

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      Try posting your C++/CLI questions here[^] :)

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups