What's a thread? I'm a quasi-expert in C++ and OOP, but I'm a complete newbie about this multithreading stuff. What does it do, how do I implement it, and types of programs would I use it for? Is it the same as multitasking?
MrViggy
January 7th, 2005, 03:33 PM
From Webopedia:
In programming, a part of a program that can execute independently of other parts. Operating systems that support multithreading enable programmers to design programs whose threaded parts can execute concurrently.
Typically, a program is a sequential collection of "commands", so to speak. In other words, one line must execute after another.
A thread is a second (or third, forth, fifith) sequence of commands that executes concurrently to the first. Or, "at the same time." There's more to them than that, however (like they have their own stack; they have their own priority; etc.).
Threading allows you to do things like perform lenghty calculations in the background, and keep your GUI reponsive to user input. There are other uses for threads, it all really depends on the problem that your application is trying to solve.
Viggy
Apollyon
January 7th, 2005, 03:50 PM
I'm trying to make a program that calculates a Vigenere cipher[if you don't know what that is, see my post: Autokey Vigenere cipher in the Data Algorithms section]. It has to calculate a key using a RNG, and while it's doing that it has to encrypt the cleartext, letter by letter.
MrViggy
January 7th, 2005, 04:04 PM
Well, if this is a console app, then threads might be overkill (because once the user types in the command, there's really nothing else for them to do). If this is a GUI app, then you might consider using a thread to do the cypher calculation. That way, you can keep the GUI (and the whole system by having a lower priority on the thread) responsive. You can also do things like have a progress bar on the GUI so the user knows how about how much work is left to do.
On the other hand, if your algorithim is fast, then there may be no need to use threads. I've never used or had experience with a Vigenere cipher, so I can't really comment on whether threads will be useful to you or not. They (threads) do introduce some overhead to your app, however that is very minimal compared to the bugs that normally pop up (due to synchronization; memory access; etc.)
Viggy
Apollyon
January 7th, 2005, 04:31 PM
Did you even read my post in the data algorithms forum? Anyway, I'm mostly limited to console apps, 'cause I can't really do GUIs because I've absolutley no idea of where to start or what to do and use. Actually, I really just want to do this as a learning experience. And you've helped me figure out why WinXP and my browser keep locking up:)
MrViggy
January 7th, 2005, 04:45 PM
That'll do it.
Yes, I did read it, however it's not obvious to me how much information you're going to be pumping through this algorithim.
If you're starting with a console app, then I'd say leave it as a single thread. I believe that you can change the priority of your app by calling (one of the other gurus can correct me here):
::SetThreadPriority(::GetCurrentThread(), THREAD_PRIORITY_BELOW_NORMAL);
This should make the main thread in your app operate "below normal", which will make your desktop more responsive. Of course, this also means that your app will take a little longer to run (it's all a tradeoff!).
Viggy
Apollyon
January 7th, 2005, 05:01 PM
Exactly how long do you mean by "a little longer"? 'cause if it's one hundredth of a second I'm not really going to care. And can you help me implement the Vigenere cipher algorithm WITHOUT multithreading, because I really need to do this because I'm in a bet. Any help is greatly appreciated.
MrViggy
January 7th, 2005, 05:21 PM
Well, since it's going to be running 1 point below normal priority, it's going to get less processor time then processes that are running at normal priority. Check out the MSDN: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/scheduling_priorities.asp
I did a quick MetaCrawler search, and got a load of homework hits on this subject. However, it seems like you have a handle on what the algorithim is, and how it works. It looks to me like all you need to do is build up a character table, then do a character for character substitution. Start writing some code, and if you get stuck with it, post the code. You'll get a lot more responses to that then your original question over in the algo forum.
Viggy
Andreas Masur
January 7th, 2005, 05:28 PM
What's a thread? I'm a quasi-expert in C++ and OOP, but I'm a complete newbie about this multithreading stuff. What does it do, how do I implement it, and types of programs would I use it for? Is it the same as multitasking?
Processes and Threads (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/processes_and_threads.asp)
Apollyon
January 7th, 2005, 05:56 PM
Char arrays and copying string to array are EXTREMELY confusing. A 1D array is hard enough, but 2D is next to impossible, for me at least. All I know is that you need to create 2 strings, one 2D char array, and 3 1D char arrays, and use strncpy to convert the strings to chars. I have no idea how to find where the letter of the key intersects with the letter of the cleartext, nor how to add nulls, nor how to generate a key string using rand(), nor format it to make every word of the ciphertext five charecters. I really, really, really need your help. I'm not asking for you to do it for me, but just show me how to do the things described above, and I'll work from there. And for the record, NONE of ANYTHING I post is for homework, OK.
Apollyon
January 7th, 2005, 06:03 PM
That's a little too in depth for me. I'm one of those people that learn by analogy.
MrViggy
January 7th, 2005, 06:14 PM
One way to think about this is that a char is just an 8 bit integer (signed or unsigned, doesn't matter).
If I understand the algorithim correctly, you pick a "key" character, then use that row in the table to encypher your letters. In that case, your table can be a 2D array of characters. Assuming all capital letters here, your first index is just {key char}-'A'; then your column index is just {letter to cypher}-'A'.
A single character is represented internally with it's ASCII code. So, assuming you have your table built:
char encypheredChar;
char charToCypher = 'M';
char cypherKey = 'K';
static const char myCypherTable[26][26] = { ... }; // Initialze the table here
std::string fullString;
// You'd loop over your input string here
encypheredChar = myCypherTable[cypherKey - 'A'][charToCypher - 'A'];
fullString += encypheredChar;
I'm assuming you know how to loop over the characters in a string. Use the STL as much as possible, as it's string class make looping and concatinating very easy!
Actually, you can use std::string or std::vector for your table as well. The advantage of that would be that you could actually change the table on the fly.
Viggy
MrViggy
January 7th, 2005, 06:26 PM
What's a thread? I'm a quasi-expert in C++ and OOP
Char arrays and copying string to array are EXTREMELY confusing. A 1D array is hard enough, but 2D is next to impossible, for me at least.
What do you mean that you are a "quasi-expert in C++"? What part of copying arrays do you not understand? A "string" (in the C sence) is just an array of characters, terminated with a null character ('\0').
A "string" in the C++ sence is usually the class std::string (part of the STL). However, it behaves like an array of characters (in that you can access individual characters). And, the std::string class provides many more facilities for dealing with strings that make the C functions look ancient (no offense to any C die-hards!).
There's a few articles on the STL here on CodeGuru: http://www.codeguru.com/Cpp/Cpp/cpp_mfc/stl/ Check out the one on the vector class: http://www.codeguru.com/Cpp/Cpp/cpp_mfc/stl/article.php/c4027/
Viggy
Apollyon
January 10th, 2005, 12:05 AM
I know how to loop with a 1D array, but how do you do it with a 2D array? Also, how do you find the place where the letters intersect? could you show me how to do that with a normal char array? And could show me how to do each one seperately, then I'll just peice it together.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.