1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
#include <iostream>
#include <winsock.h>
using namespace std;
struct ParamsForThread
{
int num;
int num2;
// function that supposedly gets num and num2 as parameters but to make it work I did it as a sturct
// it would also work if I made the params and function outside the struct and called them in the
// main func but it felt to me that it would be less messy and easier to use if all those were in a struct
void Task2()
{
while (true)
{
cout << "my numbers are " << num << " and " << num2 << endl;
}
}
};
// regular function that doesn't get arguments, just for testing
DWORD WINAPI Task1(LPVOID lpParam)
{
while (true)
{
cout << "I'm pissed cos threading is much more complicated here than in c#" << endl;
}
}
// struct parameter created outside main() to be used in the outside task
ParamsForThread myParams;
// outside function for the struct Task2 function
// if you don't do an outside function and use myParams.Task2 there is a compilation error
// when you call CreateThread(NULL, 0, myParams.Task2, NULL, 0, 0);
// there is also a compilation error if you copy outsideTask into the struct and call
// CreateThread(NULL, 0, myParams.outsideTask, NULL, 0, 0);
DWORD WINAPI outsideTask(LPVOID lpParam)
{
myParams.Task2();
return 0;
}
int main()
{
myParams.num = 6;
myParams.num2 = 19;
CreateThread(NULL, 0, Task1, NULL, 0, 0);
CreateThread(NULL, 0, outsideTask, NULL, 0, 0);
Sleep(1000);
}
|