| Niels Meijer (22) | |
|
Hi, As you might have seen in the title, I'm looking for a good multithreading method and corresponding tutorial. I've searched the web, but there is too much out there for me to choose from. I've read a tutorial about the process.h file and the Windows API, but I prefer a more standard way: winAPI threading is way to opaque for me due all its different types and renaming (I think Windows reshapes my code into a (for me) illegible, CAPS_LOCK'd monster which doesn't look like any 'normal' C++ code I've ever seen), and it is OS specific. So I considered the Boost library, but I would rather use a more standard C++ish method and I'm not a hardcore fan of external libraries. I heard that C++11 has a thread.h, but I'm using Dev-C++ (which doesn't support C++11). Perhaps I could download only the header and use it without the rest of the update, but I'm not really sure copy/pasting a header is the way to go. Does anyone know a good, simple and standard way of threading and its corresponding tutorial which fits in the standard C++ usage (not like winAPI) and which does not require me to download foreign header files? I'm not very sure about what I'm actually seeking for and not all requirements have to be met, there just there to specify my goal for a bit. Ok, that was all I had to say. (You may breathe again now) Please leave a reply and have a nice day! Kind regards, Niels Meijer | |
|
|
|
| jlb (169) | ||||
Then maybe this is a sign that you should think about getting a more updated IDE.
Definitely not the way to go. You can't just download a few header files from some other compiler and use them in your old outdated compiler.
My best advice would be to obtain a newer IDE that can compile a C++11 program. You could download the latest Code::Blocks IDE (with compiler) or the latest Visual C++ IDE. Both of these compilers should support the std::thread library. The other option, since you don't want to use the Windows API would be to download and use the Boost::thread library. If I remember correctly the std::thread library was based on this library. | ||||
|
|
||||
| EssGeEich (1007) | |
|
If you are going to keep using Dev-C++, Use the Orwell edition, It should support C++11, you can find it here: http://sourceforge.net/projects/orwelldevcpp Also redownload the compiler. Using the default download link should work well for both x86 and x64. Should we put the link in the "Why we've deprecated Dev-C++" page? Ah, let me say: C++11 does not just include some headers. It also needs editing of the compiler itself, so copy/pasting a C++11 header for use in a C++-03 compiler won't work, unless the header is C++03-compatible. | |
|
Last edited on
|
|
| Antares64 (7) | |||
Here is an example:
| |||
|
Last edited on
|
|||
| EssGeEich (1007) | |
Antares64, completely wrong and crashy test.time = &number;This won't even compile. | |
|
|
|
| Antares64 (7) | |
@EssGeEich: Sorry, that is a mistake I made, just change & to * . time = *number;
| |
|
Last edited on
|
|
| EssGeEich (1007) | |||
|
Yeah, but if you don't use mutexes or so you may be reading data at a bad time, for example while it is being written. 'time++' is not an atomic part of code. '++time' neither. So, you cannot assume you can always read 'time' when another write operation on 'time' is being executed. For small-scale tests it's all right, but be aware to this kind of things. Also using globals isn't the best of things, an better example, still not using mutexes, looks like follows:
| |||
|
Last edited on
|
|||
| DeXecipher (170) | |||
multithreading is a
In layman's terms, multithreading divides the time of the program's main process into a subroutine known as a thread. You pass data to the thread, prototypes and calling conventions of said thread functions are generally the same but can vary. Basically you gather the data you want to share with the thread and operate upon the data in that said thread. You want to avoid deadlocks and race conditions, where the data that you use influences the output of the program before it is supposed to. A deadlock completely halts the flow of the program and it is generally caused by a logic or timing error. I made a digital timer using multithreading. It logged the day, month, year, hour, second, etc. Multhreading is good if something takes so much time that it stalls your main thread greater than or equal to 1 second. | |||
|
Last edited on
|
|||