boost threads

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • whitehat1
    New Member
    • May 2012
    • 3

    boost threads

    So I'm modifying a code to be multithreaded, I have read several articles but have not found my answer, I have the Main, Class A, Class B, now I want to know if it's possible to program threads in class b so when when main calls class a this in turn calls class b and here the treads are created, not from the main from the subclasses. Thanks.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    I don't see your proble here. Obviously, an object of class A can call a method on a class B object that creates a thread. Since a thread is just a function call all the class A object needs to do is pass the arguments that the class B object will use to create the thread.

    Usually, however, there is no class B. The thread create s done by a static method on class A.

    The this pointer of the cass A bject is passed to the sttic method as an argument. After creating the thread, the static function typecasts its argument back to class A and then calls a class A method to execute the thread. Thus class A manages its own threads.

    If you use subclasses, then this logic is in the base class.

    Comment

    • whitehat1
      New Member
      • May 2012
      • 3

      #3
      Any tutorial where I can see this done to look at that, not the normal ones where is called from the main

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        I used this example in my C++ courses. It's for Windows, which may not suit you, but the idea is the same regardless of operating system.

        If you use Windows, build this code and step through it using the debugger.


        Code:
        //Demo of how to communicate with threads
        
        
        #include <iostream>
        using namespace std;
        #include <windows.h>
        
        
        class MyClass
        {
        	public:
        		MyClass();
        		~MyClass();
        		static DWORD WINAPI StartThreadOne(LPVOID in);
        		void LaunchOne();
        		void StopOne();
        		void ExecuteThreadOne();
        		int GetDataOne();
        		void SetDataOne(int in);
        
        	private:
        		HANDLE hThreadOne;
        		HANDLE hEventThreadOne;
        		int DataOne;
        		CRITICAL_SECTION cs;
        };
        
        	MyClass::MyClass() : hThreadOne(0), DataOne(0)
        {
        	InitializeCriticalSection(&cs);
        
        	hEventThreadOne = CreateEvent(
        									0,		//handle cannot be inherited
        									TRUE,   //we will do ResetEvent ouselves
        									FALSE,  //event is unsignaled
        									0       //the event object is unnamed
        								 );
        
        }
        	MyClass::~MyClass()
        {
        	DeleteCriticalSection(&cs);
        	CloseHandle(hThreadOne);
        }
        
        void MyClass::LaunchOne()
        {
        	DWORD threadid;					//to hold the returned thread id
        	HANDLE th = ::CreateThread(
        						NULL,		//use default security
        						0,			//use stack size of calling thread
        						StartThreadOne,   	//the thread
        						this,			//the thread input argument
        						0,			//run immediately
        						&threadid
        						);
        	if (!th)
        	{
        		cout << "CreateThread failed" << endl;
        	}
        
        }
        
        void MyClass::StopOne()
        {
        	::SetEvent(hEventThreadOne);
        }
        
        DWORD WINAPI MyClass::StartThreadOne(LPVOID in)
        {
        	reinterpret_cast<MyClass*> (in) ->ExecuteThreadOne();
        
        	return 0;	//thread completed
        }
        
        void MyClass::ExecuteThreadOne()
        {
        	while (1)
        	{
        		if (WaitForSingleObject(hEventThreadOne, 100) == WAIT_OBJECT_0)
        		{
        			//We have been told to shut down this thread
        			break;
        		}
        		Sleep(500);	//simulate some processing
        		EnterCriticalSection(&cs);
        		DataOne++;
        		LeaveCriticalSection(&cs);
        
        	}
        }
        
        int MyClass::GetDataOne()
        {
        	int rval;
        		EnterCriticalSection(&cs);
        		rval = DataOne;
        		LeaveCriticalSection(&cs);
        		return rval;
        
        }
        
        void MyClass::SetDataOne(int in)
        {
        		EnterCriticalSection(&cs);
        		DataOne = in;
        		LeaveCriticalSection(&cs);
        
        }
        
        
        
        int main()
        {
        	cout << "Starting main()" << endl;
        	MyClass  obj;
        			 obj.LaunchOne();
        
        	int buffer;
        	int loopctr = 0;
        	while (1)
        	{
        		loopctr++;
        		buffer = obj.GetDataOne();
        		cout << buffer << " ";
        		Sleep(500);
        		if (buffer > 20)
        		{
        			obj.SetDataOne(0);
        		}
        		if (loopctr == 9)
        		{
        			obj.StopOne();
        		}
        	}
        
        	return 0;
        }
        
        
        
        /*
        HANDLE CreateThread(
          LPSECURITY_ATTRIBUTES lpThreadAttributes, // SD
          DWORD dwStackSize,                        // initial stack size
          LPTHREAD_START_ROUTINE lpStartAddress,    // thread function
          LPVOID lpParameter,                       // thread argument
          DWORD dwCreationFlags,                    // creation option
          LPDWORD lpThreadId                        // thread identifier
        );
        */
        /*
        HANDLE CreateEvent(
          LPSECURITY_ATTRIBUTES lpEventAttributes, // SD
          BOOL bManualReset,                       // reset type
          BOOL bInitialState,                      // initial state
          LPCTSTR lpName                           // object name
        );
        */

        Comment

        • whitehat1
          New Member
          • May 2012
          • 3

          #5
          thanks this will help a lot

          Comment

          Working...