Threading

Hello,
I've written a code that can make a function with parameters run in a new thread. Something that generally isn't possible with the CreateThread method. My problem is that my code is very messy and as a newbie to the c++ language I'm having a hard time creating a class to create a medium level between the main() calls and the operating functions.
Here is the code, hope you can give me any suggestions of how to improve it or create a class that deals with it.

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);
}
make a function with parameters run in a new thread

That's what std::thread (formerly boost::thread) does. It is using the same underlying machinery as std::bind, if you're interested in details.
i tried using std::thread but it doesn't exist. I'm using VS 2008. I saw some examples on the net but none worked for me and I tried downloading boost too and couldn't make it work for me either. How do I use it on my VS 2008?
You can download boost from http://boostpro.com/download
I have downloaded and installed the boost from that link. But whenever I try to include <thread> or <thread.h> or whatever <boost/thread/thread.hpp> combination I find in any of the examples in the boost folder that I installed it just doesn't work. I don't really know from what default directory the #include takes the files from but the threading files don't seem to be there and I don't know how to make it work
You probably need to set VS to look in the boost directory as well for <> includes.

Try Project→Properties→Configuration Properties→C/C++→General→Additional Include Directories

This will add that directory for the project only. There is a way to adjust it for VS itself but the method escapes me.
Thanks. I did exactly what you said and it stopped showing me the not found error. But now when I call #include <boost/thread/thread.hpp>
I get an error of:
Error 1 fatal error LNK1104: cannot open file 'libboost_thread-vc90-mt-gd-1_51.lib'

what can I do about it?
in the linker section you need to provide the path to the boost libs as well
can you please explain me how to do it in VS? I tried adding it under C/C++ option and it didn't work and if I go Configuration Properties→Linker there is no option to add anything
ty
This are nice screen shots what to do in order to use a library:

http://www.steptools.com/support/stdev_docs/help/settings_vc10.html

for boost you don't need 'Additional Dependencies'. You just have to set 'Additional Library Directories' to the boost directory
What's wrong with CRT _begintreadex() function from <process.h> ?

If you don't know multithreading concepts then neither boost or std::thread will help you.
Topic archived. No new replies allowed.