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.
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.
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.
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.