Adding Multi-Threading Inside Function

I'm new to multithreading, so I have a bunch of questions.

1. Is there a standard C++ way of doing multithreading, or is there only MFC for Windows? I'd like to write just one code that supports all platforms.

I have a code somewhat like this...
1
2
3
4
5
6
7
8
9
for ( int i = 0; i < a.length; i++ )
{
         //create thread
	for ( int j = 0; j < b.length; j++ )
	{
		//do stuff;
	}
         //destroy thread
}

It is reading through a file format and storing the data into lists. What I want is the main thread reads until it finds a block []. Then it creates a thead to process data up to the next block. While that is processing, I want the main thread to skip to the next block, and then create a new thread and just repeat the process.

2. How would I go about doing this?
I would probably have to lock the list while adding to it.

I'd like to add multithreading because it takes a while for each one to process the data.
If anyone knows a good place to start or read, please post a link.
There is nothing in std:: that supports cross-platform multi-threading, but you can use boost which is really easy to setup.

You would set up this:
1
2
3
#include <boost/thread.hpp>

void MyFunction( MyType& MyObject);


Then to run your function in a thread and pass the object into it you would simply do this:
boost::thread MyFunction(boost::ref(TheObject));

You're passing by reference because you need an output. If you didn't need the output, you wouldn't need boost::ref
Last edited on
I read that using the OS specific multithread libraries are faster and give greater functionality, so I am going to use them instead. I found some good references, so if I get stuck I'll let you know.
If you want to make your code not OS-specific with OS specific libraries, you'll just have to do things twice.

Use
1
2
3
4
5
6
7
8
9
10
11
#if defined(_WIN32) || defined(__WIN32__)
// Windows-specific threading code
#elif defined(linux) || defined(__linux)
// Linux-specific threading code
#elif defined(__APPLE__) || defined(MACOSX) || defined(macintosh) || defined(Macintosh)
// Mac code
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
// BSD code
#else
#error This operating system is not supported
#endif 


Make sure that the functions you use are named the same thing in the exclusive code bits so that you can use them outside of the #ifdef bleck.
Last edited on
What exactly is the start address of a thread and what is its stack size?
How do you pass arguments to the thread if it is of type void*?

Here's the link to the VC++ _beginthreadex() I'm using.
http://msdn.microsoft.com/en-us/library/kdzttdcb.aspx
I have to use its begin and end thread functions because I'm using Libcmt.lib and compiling for Windows with the Windows API.
I'm bumping because I don't see any rules on the time limit of bumping.

All the things I find on msdn, google, and some of the books I have don't explain what the stack size really is. None of them actually pass in arguments with the void* because they are just simple examples showing how to use threads.

I'd appreciate if someone could answer this, or tell me how to move the topic to the Windows Programming section of the forums where it might get more attention since it is using the Windows API.
That's one of the reasons why it's easier to use boost. The boost wrapper makes everything simple and doesn't have any OS-specific dependencies that you'll have to re-write yourself for each supported system.

Since you are using Win32, this is the way I've always done it with this (But yours looks very similar):
http://msdn.microsoft.com/en-us/library/windows/desktop/ms682453(v=vs.85).aspx

The start address of a thread is a pointer to your function use:
CreateThread(NULL, 0, MyFunction, (void*)&MyObject, 0);

This will call a function with this header:
DWORD WINAPI MyFunction(void* Something);

The contents of the function (or even the name) are up to you. To pass the function, you need to simply cast the address as a void*. Then in your function you'll need to unpack it as a specific type.

If you need two-way communication, then you will need to pass by reference, in which case you'll need to use a reference wrapper ( I think there is one in std:: but I've never used it). This would be similar to boost::ref(TheObject) that I used above.
Topic archived. No new replies allowed.