Thread --- Create Thread Function
Create Thread Function
#include "stdafx.h"
#include <stdlib.h>
#include <windows.h>
#include <set>
#include <conio.h>
#include <iostream>
using namespace std;
class Thread
{
public:
void fun1()
{
Sleep(0);
for(int i = 0; i<10;i++)
{
cout<< "FUnction 1 "<<i<<"\n"<<endl;
Sleep(0);
}
}
void fun2()
{
Sleep(0);
for(int i = 0; i<10;i++)
{
cout<< "FUnction 2 "<<i<<"\n"<<endl;
Sleep(0);
}
}
static DWORD_PTR WINAPI Threadfunction1(LPVOID parm)
{
Thread *tempTr = static_cast<Thread*>(parm);
tempTr->fun1();
return 0;
}
static DWORD_PTR WINAPI Threadfunction2(LPVOID param)
{
Thread *TmpPtr = static_cast<Thread*>(param);
TmpPtr->fun2();
return 0;
}
void thrreadcall()
{
HANDLE *hPtr;
hPtr = new HANDLE[2];
LPTHREAD_START_ROUTINE Rout[2] = { Threadfunction1 , Threadfunction2 };
//DWORD_PTR i = 0;
DWORD_PTR id = 0;
DWORD_PTR Mask;
for(DWORD_PTR i =0; i <2 ; i++)
{
hPtr[i] = CreateThread( NULL,0,Rout[i], NULL , 0 , &id);
Mask = 1 << i;
SetThreadAffinityMask(hPtr[0] ,Mask);
wprintf(L"Creating Thread %d (0x%08x) Assigning to CPU 0x%08x\r\n", i, (LONG_PTR)hPtr[i], Mask);
}
for(int i = 0; i < 2; ++i)
{
ResumeThread(hPtr[i]);
}
WaitForMultipleObjects(2, hPtr, TRUE, INFINITE);
for(int i = 0; i < 2; ++i)
{
CloseHandle(hPtr[i]);
}
/*i = 1;
id = 0;
Mask = 1 << i;
hPtr[i] = CreateThread(NULL,0, (LPTHREAD_START_ROUTINE)Threadfunction2() , 0 , NULL , &id);
SetThreadAffinityMask(hPtr[i] , Mask);
wprintf(L"Creating thread Id %d Assigning to CPU 0x%08\r\n" , id , Mask);*/
}
};
int main()
{
Thread obj;
obj.thrreadcall();
return 0;
}
Passing Parameters to Thread Function
#include "stdafx.h"
#include <stdlib.h>
#include <windows.h>
#include <set>
#include <conio.h>
#include <iostream>
using namespace std;
struct st
{
int afn;
int afp;
};
class Thread
{
int a;
public:
void fun1(int j)
{
Sleep(0);
for(int i = 0; i<10;i++)
{
cout<< "FUnction 1 "<<i<<" " << j <<"\n" <<endl;
Sleep(0);
}
}
void fun2()
{
Sleep(0);
for(int i = 0; i<10;i++)
{
cout<< "FUnction 2 "<<i << " " <<"\n<<endl;
Sleep(0);
}
}
static DWORD_PTR WINAPI Threadfunction1(LPVOID parm)
{
//Thread *tempTr = static_cast<Thread*>(parm);
st* tempTr=static_cast<st*>(parm);
Thread *tempTr_i =new Thread;
if(tempTr->afn == 0)
{
tempTr_i->fun1(tempTr->afp);
}
else
{
tempTr_i->fun2();
}
return 0;
}
void thrreadcall()
{
HANDLE *hPtr;
hPtr = new HANDLE[2];
LPTHREAD_START_ROUTINE Rout[2] = { Threadfunction1 , Threadfunction1 };
//DWORD_PTR i = 0;
DWORD_PTR id = 0;
DWORD_PTR Mask;
st obj;
std::cout<<"Eneter some value ";
std::cin>> obj.afp;
for(DWORD_PTR i =0; i <2 ; i++)
{
//Thread *TmpPtr = new Thread();
obj.afn = i;
hPtr[i] = CreateThread( NULL,0,Rout[i], &obj ,0 , &id);
Mask = 1 << i;
SetThreadAffinityMask(hPtr[0] ,Mask);
wprintf(L"Creating Thread %d (0x%08x) Assigning to CPU 0x%08x\r\n", i, (LONG_PTR)hPtr[i], Mask);
}
for(int i = 0; i < 2; ++i)
{
ResumeThread(hPtr[i]);
}
WaitForMultipleObjects(2, hPtr, TRUE, INFINITE);
for(int i = 0; i < 2; ++i)
{
CloseHandle(hPtr[i]);
}
/*i = 1;
id = 0;
Mask = 1 << i;
hPtr[i] = CreateThread(NULL,0, (LPTHREAD_START_ROUTINE)Threadfunction2() , 0 , NULL , &id);
SetThreadAffinityMask(hPtr[i] , Mask);
wprintf(L"Creating thread Id %d Assigning to CPU 0x%08\r\n" , id , Mask);*/
}
};
int main()
{
Thread obj;
obj.thrreadcall();
return 0;
}
1) You can't use regular member functions as a ThreadProc. If you have to cast it to get it to compile it's probably wrong. The ThreadProc functions need to be free or static. They also had the wrong signature as a ThreadProc takes a single void* parameter.
2) There are several places where you use DWORD_PTR when you really want DWORD such as the return value from the ThreadProc,
c , i , etc.
3) From the
CreateProcess docs:A thread in an executable that calls the C run-time library (CRT) should use the _beginthreadex and _endthreadex functions for thread management rather than CreateThread and ExitThread; this requires the use of the multithreaded version of the CRT. If a thread created using CreateThread calls the CRT, the CRT may terminate the process in low-memory conditions.
Chances are writing to
cout eventually hits the CRT. It may not, and even if it does you may not have issues, but if you do that's a good place to look.
4) I/O isn't guaranteed to be interleaved at all, so writing to
cout is not a good way to decide if the threads are running simultaneously or not. I've added some Sleep calls to the threads and also created them suspended at first so I could start them as close together as possible to make it seem like the I/O is interleaved, but that may just be coincidence. Once thing I do see that you may as well is that right when the threads are started the string that's printed and the endl are not attached to each other, that is I see both strings followed by two line ends. After that is it somewhat interleaved.
5) You always want to wait for the threads to exit before you delete the class out from under them. You also generally want to close their handles once they are done.
I eliminated the constructor/destructor since they were empty and other fluff just to keep this as short as possible
|
#include "stdafx.h"
#include <stdlib.h>
#include <windows.h>
#include <set>
#include <conio.h>
#include <iostream>
using namespace std;
class Thread
{
public:
void fun1()
{
Sleep(0);
for(int i = 0; i<10;i++)
{
cout<< "FUnction 1 "<<i<<"\n"<<endl;
Sleep(0);
}
}
void fun2()
{
Sleep(0);
for(int i = 0; i<10;i++)
{
cout<< "FUnction 2 "<<i<<"\n"<<endl;
Sleep(0);
}
}
static DWORD_PTR WINAPI Threadfunction1(LPVOID parm)
{
Thread *tempTr = static_cast<Thread*>(parm);
tempTr->fun1();
return 0;
}
static DWORD_PTR WINAPI Threadfunction2(LPVOID param)
{
Thread *TmpPtr = static_cast<Thread*>(param);
TmpPtr->fun2();
return 0;
}
void thrreadcall()
{
HANDLE *hPtr;
hPtr = new HANDLE[2];
LPTHREAD_START_ROUTINE Rout[2] = { Threadfunction1 , Threadfunction2 };
//DWORD_PTR i = 0;
DWORD_PTR id = 0;
DWORD_PTR Mask;
for(DWORD_PTR i =0; i <2 ; i++)
{
hPtr[i] = CreateThread( NULL,0,Rout[i], NULL , 0 , &id);
Mask = 1 << i;
SetThreadAffinityMask(hPtr[0] ,Mask);
wprintf(L"Creating Thread %d (0x%08x) Assigning to CPU 0x%08x\r\n", i, (LONG_PTR)hPtr[i], Mask);
}
for(int i = 0; i < 2; ++i)
{
ResumeThread(hPtr[i]);
}
WaitForMultipleObjects(2, hPtr, TRUE, INFINITE);
for(int i = 0; i < 2; ++i)
{
CloseHandle(hPtr[i]);
}
/*i = 1;
id = 0;
Mask = 1 << i;
hPtr[i] = CreateThread(NULL,0, (LPTHREAD_START_ROUTINE)Threadfunction2() , 0 , NULL , &id);
SetThreadAffinityMask(hPtr[i] , Mask);
wprintf(L"Creating thread Id %d Assigning to CPU 0x%08\r\n" , id , Mask);*/
}
};
int main()
{
Thread obj;
obj.thrreadcall();
return 0;
}
Passing Parameters to Thread Function
#include "stdafx.h"
#include <stdlib.h>
#include <windows.h>
#include <set>
#include <conio.h>
#include <iostream>
using namespace std;
struct st
{
int afn;
int afp;
};
class Thread
{
int a;
public:
void fun1(int j)
{
Sleep(0);
for(int i = 0; i<10;i++)
{
cout<< "FUnction 1 "<<i<<" " << j <<"\n" <<endl;
Sleep(0);
}
}
void fun2()
{
Sleep(0);
for(int i = 0; i<10;i++)
{
cout<< "FUnction 2 "<<i << " " <<"\n<<endl;
Sleep(0);
}
}
static DWORD_PTR WINAPI Threadfunction1(LPVOID parm)
{
//Thread *tempTr = static_cast<Thread*>(parm);
st* tempTr=static_cast<st*>(parm);
Thread *tempTr_i =new Thread;
if(tempTr->afn == 0)
{
tempTr_i->fun1(tempTr->afp);
}
else
{
tempTr_i->fun2();
}
return 0;
}
void thrreadcall()
{
HANDLE *hPtr;
hPtr = new HANDLE[2];
LPTHREAD_START_ROUTINE Rout[2] = { Threadfunction1 , Threadfunction1 };
//DWORD_PTR i = 0;
DWORD_PTR id = 0;
DWORD_PTR Mask;
st obj;
std::cout<<"Eneter some value ";
std::cin>> obj.afp;
for(DWORD_PTR i =0; i <2 ; i++)
{
//Thread *TmpPtr = new Thread();
obj.afn = i;
hPtr[i] = CreateThread( NULL,0,Rout[i], &obj ,0 , &id);
Mask = 1 << i;
SetThreadAffinityMask(hPtr[0] ,Mask);
wprintf(L"Creating Thread %d (0x%08x) Assigning to CPU 0x%08x\r\n", i, (LONG_PTR)hPtr[i], Mask);
}
for(int i = 0; i < 2; ++i)
{
ResumeThread(hPtr[i]);
}
WaitForMultipleObjects(2, hPtr, TRUE, INFINITE);
for(int i = 0; i < 2; ++i)
{
CloseHandle(hPtr[i]);
}
/*i = 1;
id = 0;
Mask = 1 << i;
hPtr[i] = CreateThread(NULL,0, (LPTHREAD_START_ROUTINE)Threadfunction2() , 0 , NULL , &id);
SetThreadAffinityMask(hPtr[i] , Mask);
wprintf(L"Creating thread Id %d Assigning to CPU 0x%08\r\n" , id , Mask);*/
}
};
int main()
{
Thread obj;
obj.thrreadcall();
return 0;
}
Example of Waiting for Another Thread Completion.
#include "stdafx.h"
#include <iostream>
#include <windows.h>
using namespace std;
bool threadFinished = false;
class Thread
{
int a;
int b;
int c;
public:
int Add_ab(int x,int y)
{
a=x;
b=y;
Sleep(10);
c= x+y;
threadFinished = true;
return c;
}
int Square_C(int z)
{
c = z*z;
return c;
}
static DWORD_PTR WINAPI Threa_Add(LPVOID parm)
{
Thread *ptr = static_cast<Thread*> (parm);
ptr->Add_ab(ptr->a , ptr->b);
return 0;
}
static DWORD_PTR WINAPI Thread_SQU(LPVOID param)
{
Thread *ptr = static_cast<Thread*> (param);
ptr->Square_C(ptr->c);
return 0;
}
void Threadcall()
{
HANDLE *hPtr = new HANDLE[2];
DWORD_PTR Id;
Thread *obj_ptr = new Thread;
cout<< "Enter 1st Value A:"<<endl;
cin>> obj_ptr->a;
cout<< "Enter 2st Value B:"<<endl;
cin>> obj_ptr->b;
hPtr[0] = CreateThread(NULL , 0 , LPTHREAD_START_ROUTINE(Threa_Add) , obj_ptr , NULL , &Id);
hPtr[1] = CreateThread(NULL ,0 , LPTHREAD_START_ROUTINE(Thread_SQU) , obj_ptr , NULL , NULL);
while(!threadFinished)
{
Sleep(10);
}
WaitForMultipleObjects(2 , hPtr , TRUE , INFINITE);
cout<< "C Value "<< obj_ptr->c;
for(int i = 0; i<2; i++)
{
CloseHandle(hPtr[i]);
}
}
};
int _tmain(int argc, _TCHAR* argv[])
{
Thread obj;
obj.Threadcall();
return 0;
}
Comments
Post a Comment