Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Multi-threading in C++
Index -> Programming, C++ -> C++ Tutorials
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
deville75




PostPosted: Tue Jan 23, 2007 10:17 am   Post subject: Multi-threading in C++

I don't know if you're allowed to request tutorials here, but I'm going to anyway Razz

Is anyone good enough to make a tutorial on mulithreading in C++? I think it would be very useful and much appreciated.

Thank You
Sponsor
Sponsor
Sponsor
sponsor
Martin




PostPosted: Tue Jan 23, 2007 10:53 am   Post subject: RE:Multi-threading in C++

What are you trying to do?

The first thing you should note is that threading is done through the operating system, not the programming language. libsdl (http://libsdl.org/) provides a multi-platform thread wrapper (?), which is probably a good place to start.
deville75




PostPosted: Tue Jan 23, 2007 2:29 pm   Post subject: RE:Multi-threading in C++

Well actually I just got hold of a book that is quite good, but what I was asking for is a tutorial on multithreading for the Win32 Applications. I won't be needing it I guess, but I will definitely post my own tutorial once I've learned enough Very Happy
md




PostPosted: Tue Jan 23, 2007 2:51 pm   Post subject: RE:Multi-threading in C++

Get MSDN; look up CreateThread. Learn google.

Threading has been known to break minds better then network code, and it makes it ridiculously easy to create impossibly hard to find bugs. Concurrency is to be avoided if at all possible.
deville75




PostPosted: Wed Jan 24, 2007 9:12 am   Post subject: RE:Multi-threading in C++

Get MSDN? Ya I'm looking at CreateThread, but ther is an ambiguous seciton in the book I'm reading and it's hard to tell if they're saying CreateThread() will cause Memory Leaks or not. It introduces a different method used in Visual C++, which uses _beginthreadex() rather than CreateThread(), but I still can't tell which one is safer in terms of memory leakage.
md




PostPosted: Wed Jan 24, 2007 9:49 am   Post subject: RE:Multi-threading in C++

the win32 API does not have memory leaks internal to itself; it's code you write that will leak memory.

Again, you are better off not using threads at all, since you obviously don't know much about them. Threads are a very complex subject, and have a huge number of very deep pitfalls craftily hidden all over the place. If you want to learn them then by all means; but don't start writing code until you know what you are doing.
abcdefghijklmnopqrstuvwxy




PostPosted: Thu Jan 25, 2007 2:54 am   Post subject: RE:Multi-threading in C++

Quote:

different method used in Visual C++


Visual C++ is an IDE with its own compiler. Suggesting that a method is used in Visual C++ is like saying the quote "I came, I saw, I conquered" was used in Correl Word Perfect. The two are completely seperate from each other.

Learn that functions and objects come from libraries not from IDE's or compilers and you may just be on your way to understanding a basic concept in programming which will help you with all your future programming tasks.

Also if you would like my opinion, I don't think threads is what you need. Not just because they are too mind-breaking, but because it's not even the right tool for the job. Multi-threading is needed in rare rare circumstances. I'm willing to bet your circumstance isn't the rare kind i'm speaking of.
deville75




PostPosted: Thu Jan 25, 2007 1:00 pm   Post subject: Re: Multi-threading in C++

I'm thankful for your concern, but I'm very sure I have to use Threads. My application basically reads data from a device (connected to USB) continually. When I did not use threads my program would hangup while it was reading (the buttons would not respond, because it was waiting for the program to finish reading the data). So how else do I program it such that I can continually read (and display) the data and at the same time accept Messages from buttons and such. Plus, the multithreading I'm using is not very complex at all, so It should not be a problem.
Sponsor
Sponsor
Sponsor
sponsor
md




PostPosted: Thu Jan 25, 2007 1:55 pm   Post subject: Re: Multi-threading in C++

deville75 @ Thursday 2007-01-25 1:00 pm wrote:
I'm thankful for your concern, but I'm very sure I have to use Threads. My application basically reads data from a device (connected to USB) continually. When I did not use threads my program would hangup while it was reading (the buttons would not respond, because it was waiting for the program to finish reading the data). So how else do I program it such that I can continually read (and display) the data and at the same time accept Messages from buttons and such. Plus, the multithreading I'm using is not very complex at all, so It should not be a problem.


There is always a way to not use threads. You could read a little bit of data; then check for things to do, then read more data, then check again. 'Course it helps to only read data when there is actually something there to read; otherwise you'll block your program waiting.

And based on what you say there is definitely room for lots of problems. The only time you can use threads without caring about concurrency issues is when one thread does no input or output except to files it opens on it's own and which are guaranteed not to be opened by anything else. That thread can then also not share any data with the rest of the application or read any data structures. Basically any interaction at all leads to problems.

Again, I recommend not using threads; clearly you do not know enough to use them safely.
deville75




PostPosted: Thu Jan 25, 2007 3:59 pm   Post subject: RE:Multi-threading in C++

hmmm, I c.. Are you talking about using wsprintf instead of sprintf when I use CreateThread() (avoiding C library) ?? Cuz I am using that..

Ok well until I learn more I'll try your suggestion about not using threads, but seriously, I'm interested in learning about threads and you guys seem to know about it. Is it possible for you to shed some light on where I could go to learn about threads?

By the way, the data I'm reading is from a thermocouple. It is always changing.
md




PostPosted: Thu Jan 25, 2007 5:43 pm   Post subject: RE:Multi-threading in C++

I learned threads through painful trial and error, and by reading about process management from an OS standpoint. Neither of which generally translate well to other people learning.

It's not about avoiding the C library though, it's avoiding race conditions. Google "avoiding race conditions" and I'm sure you can find many excellent examples.

oh, and your thermocouple is USB based; which means a maximum of 480Mbits/s (USB 2.0) or 12Mbits/s(USB 1.1); given that it's probably a 1.1 device you're limited to how much you can read to a rather low rate. And even if it is constantly changing you don't need to constantly read it. You only need to read it every time you want a new value, if that's anything more then 100 times/s then you're almost certainly getting more data then you could possibly use.
Display posts from previous:   
   Index -> Programming, C++ -> C++ Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 11 Posts ]
Jump to:   


Style:  
Search: