Would you like to react to this message? Create an account in a few clicks or log in to continue.

Parallel Processing in C++

3 posters

 :: C Languages :: C++ :: Help!

Go down

Parallel Processing in C++ Empty Parallel Processing in C++

Post  David B Tue Jan 10, 2012 6:36 pm

Hello everyone!

I was wondering if parallel processing is possible in C++. If so, please explain how it is done, and please provide an example program. Thanks in advance.

For those of you who don't know what parallel processing is...
Spoiler:
David B
David B
Administrator
Administrator

Posts : 618
Join date : 2011-07-20
Location : The Twilight Zone!

https://programmingforums.forumotion.com/

Back to top Go down

Parallel Processing in C++ Empty Re: Parallel Processing in C++

Post  legolizard Tue Jan 10, 2012 7:40 pm

First off let me add a little clarafication DB.

One multitasking on a computer comes in TWO forms not one and those are,

1) Parallel processing or process based.<---No your OS does this.
2) Multithreading, or thread based.<---C++ uses this.

A process is an executing program and pparallel processing, which DB mentions is being able to run two programs concurrently or at the same time.
Multitreading is the simultaneous execution of executable code, this meaning that a single program can do two or more things at the same time, and is what DB is suggesting.

Now that I have just p0wned DB in some vocab it is time to move on...
Multi threading is very interesting in that it is not "built in" C++ like other languages like C# has however in C++ multithreading was designed to utilize the OS's capabilities for multithreading and thus give full range.

Anyway here is an exmaple using the Win32 API which I again strongly recommend learning if you are a masacit. LAWL!!!
But seriously, sarcasm aside Win32 is as cryptic as sh!t, and it is just...just...horrid and I strongly urge Boost or maybe even SFML, which I think both support multithreading. :/(I am learning SFML now so I we will see, but I know Boost contains it. . :-D)


Code:
#include <iostream>
#include <windows.h>
#include <process.h>//For _beginthread().
///@note This is a very basic multithread example, in fact there is no synchronization, but this should suffice for now. :P

//Prototypes.
void  test(void*);//It is required that the function take a void* paramater.

//Enter primary thread, main().
int main(){
    std::cout<<"Now in main().\n";

    _beginthread(test, 0 , (void*)0);//Call anotherThread in a new thread.


    anotherThread((void*)0);//Call silly from main().
}
//Implementaiotns.
void  test(void* variable){
    std::cout<<"Now in test().\n";
}


Note how while I only called test once in the main function but two things appear.
legolizard
legolizard

Posts : 137
Join date : 2011-08-01
Location : On planet Char.

Back to top Go down

Parallel Processing in C++ Empty Re: Parallel Processing in C++

Post  David B Tue Jan 10, 2012 8:46 pm

Could you please explain the syntax to me? I looked over the code, and found it very confusing.

Oh, and thanks for the grammar lesson. Very Happy
David B
David B
Administrator
Administrator

Posts : 618
Join date : 2011-07-20
Location : The Twilight Zone!

https://programmingforums.forumotion.com/

Back to top Go down

Parallel Processing in C++ Empty Re: Parallel Processing in C++

Post  legolizard Tue Jan 10, 2012 9:02 pm

First off it was not a grammar lesson. It was a programming lesson.

Second off, well okay.

Firstly, understand this is the Win32API, it is by nature cryptic and hard to understand, and thus unless you are a massacist will find it very difficult, even some pros find it difficult, haha.


The only thing I feel absolutly certain in explaining is what this is :

Code:
void*

This is a void pointer, which is a special type of pointer that litterally points to no specific value. To explain, we know that a int* points to a int, and a double* points to a double. A void* can point to any data structure, however there is one big limitation. Because void* cannot be deferenced, and thus in order to use a void* we must do the following :

Cast it as some other type of pointer and then concentrate it as that type of pointer.

The only other thing I feel comfortable in explaining is the

_beginthread(...);

Here is the sig. of this function :
*looking....*FOUND IT!!!Very Happy
Code:
void( __cdecl *start_address )( void * ),
  unsigned stack_size,
  void *arglist

Behold, the gross formatting that is known as Win32. Shocked Shocked Shocked Shocked

Basically, all I want you to understand is what the 3 params are.

1) The name of the function(also called the entry point function to begin that must return nothing and must have one void* parameter.
2)Since every thread gets its own stack we need to define how large the stack will be with this parameter, but I just set it to 0, since I was lazy. Embarassed
3)The last parameter is the void* parameter that must be within the entry point function as stated above.

I would suggest that you learn more about C++ args and fully understand them, before doing something as complex as multithreading, in fact I suggest fully understanding even OO stuff, before it, however there ya' go. : )


Last edited by legolizard on Wed Jan 11, 2012 6:31 pm; edited 1 time in total
legolizard
legolizard

Posts : 137
Join date : 2011-08-01
Location : On planet Char.

Back to top Go down

Parallel Processing in C++ Empty Re: Parallel Processing in C++

Post  legolizard Wed Jan 11, 2012 6:16 pm

Also, I researched a little bit into how SFML does threads and lawl. It is so much nicer, in formatting compared to the Windows way, however I must say somethings are really easy to make in Win32 API(hehe), but if you want I can show you how to use SFML with C::B so that I can show you the SFML way of making a thread. : )

The only problem is however is that SFML is object oriented and one must have a relatively good understanding of OOP.
legolizard
legolizard

Posts : 137
Join date : 2011-08-01
Location : On planet Char.

Back to top Go down

Parallel Processing in C++ Empty Re: Parallel Processing in C++

Post  Nighthawk0973 Fri Jan 13, 2012 2:46 pm

Oh. Multithreading. Yeah. I can do threads in Java. But not really. XD

But I bet it's absolutely nessecary when making a RTS.
Nighthawk0973
Nighthawk0973
Moderator
Moderator

Posts : 307
Join date : 2011-07-20
Age : 25
Location : In Front of My Computer

Back to top Go down

Parallel Processing in C++ Empty Re: Parallel Processing in C++

Post  legolizard Fri Jan 13, 2012 5:41 pm

Yes. Generally speaking you would have a thread for the game, and all its mechanics etc, and another thread for the GUI.
One thing that is important to remember however, that you want to keep the number of threads at a minimum since too many will require too many resources and your game will most likely lag out, but I think 2 with some sort of sync, like a mutex, would be sufficient. : )

In Java multithreading is really easy, in fact it is much more similar to the SFML way, being that both are OO, but then again Java always has to be different now doesn't it, so I am sure there is some random quirk to lawl at. XD
legolizard
legolizard

Posts : 137
Join date : 2011-08-01
Location : On planet Char.

Back to top Go down

Parallel Processing in C++ Empty Re: Parallel Processing in C++

Post  Nighthawk0973 Sun Jan 15, 2012 4:04 pm

But wouldn't you have a thread for every unit when you are in game, and then use methods like thread.sleep(MiliSecs);
to control what they do?
Nighthawk0973
Nighthawk0973
Moderator
Moderator

Posts : 307
Join date : 2011-07-20
Age : 25
Location : In Front of My Computer

Back to top Go down

Parallel Processing in C++ Empty Re: Parallel Processing in C++

Post  legolizard Sun Jan 15, 2012 4:11 pm

No, if you did your computer would sh!t the bed. Each "unit" would be(logically speaking) an object of a class that would interact with other objects all of which would be on the game thread separate from the UI thread. Keep in mind that the optimal number of threads is generally equal to the number of CPU's available to you. I say optimal because, I know for a fact that the JVM can handle thousands of threads at any given time, but of course your computer is not very happy, and personally mine lagged out. :/

In Java, because it is OP, you can actually find the number of processors you have with this line of code
Note :
I have not tested it out for a while, so you may have to tweak the names or whatever, since I don't remember exactly. :/

Code:
Runtime.getRuntime().availableProcessors();
You can just put it in a print statement, that was what I did when I showed me CS teacher. Razz
To make sure it works however, what you can do is the following on a Windows OS.

Computer->Properties->Device Manager->Processors
And then count the number of processors you have and it should be equal to the number outputted by the aforementioned line of code.

EDIT : To further explain why you would not want a thread for each unit, there is another problem that the programmer must take into account. For this example, let's assume that you' re computer can handle having so many threads, which it most likely wouldn't. :p
When game that is thread heavy(which I don't know if they even exist. xD), you would really have to be careful about syncing them. Otherwise some units would be working and then at the same time, others would not. DX

Now I am not saying that multithreading is not useful. It is awesome, in that it allows the computer to do multiple things at one time, keep in mind that the majority of a programs time is WASTED sending and receiving input from the user to whatever and back. However multithreading allows you to DO something in that time, thus making your program much more efficient, but you must again keep in mind that if I have one CPU available to me and said CPU SUCKS(lol) then it is not going to be able to handle a large number of threads.
legolizard
legolizard

Posts : 137
Join date : 2011-08-01
Location : On planet Char.

Back to top Go down

Parallel Processing in C++ Empty Re: Parallel Processing in C++

Post  Sponsored content


Sponsored content


Back to top Go down

Back to top


 :: C Languages :: C++ :: Help!

 
Permissions in this forum:
You cannot reply to topics in this forum