Threads in C++

Pages: 12
Dec 5, 2010 at 4:24am
Hello!

I tried to find some informaton on the internet about threads in c++, but unfortunatelly I couldnt. I found about threads in C#,Java etc. Please help me! I need to know ho to make a thread in c++, how to run it, how to stop it, how to run two threads simultaneously, and how to give priority to a thread. I hope you can give me some information.

Thank you in advance!
Dec 5, 2010 at 4:42am
Threading is not in the standard library yet. You'll need a third-party library like Boost to do it.
Dec 5, 2010 at 4:57am
Okay, thank you for the reply. But can you give me some tutorial or link with examples for Boost. And one stupid question: threading is not in the standart library of c too, right?
Dec 7, 2010 at 10:01am
If you're using Linux, I strongly recommend pthread. By default, everything should have been set up for you.
The link below has almost everything you want.
http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html

Although I'm quite sure it also works with windows, I've never tried that myself.
Last edited on Dec 7, 2010 at 10:02am
Dec 7, 2010 at 12:37pm
The semi-official Boost library provides a portable thread class for that purpose.
Have a look at it:
http://www.boost.org/doc/libs/1_43_0/doc/html/thread.html
Cheers
Dec 7, 2010 at 8:35pm
Dec 7, 2010 at 8:42pm
pthreads are not portable. Threading will be a part of the upcoming C++ standard.
Till then, use portable Boost, but be aware that the compiler does not know anything about threads and some nasty optimisation-related problems may sometimes arise in corner cases, especially on multiprocessor machines. Compilers tend to reorder instructions sometimes, breaking otherwise correct thread synchronisation.
Last edited on Dec 7, 2010 at 8:45pm
Dec 7, 2010 at 11:25pm
I did not know where to put this code to get an answer, I hope it is ok here...

please, can someone do this for me?! I would be very grateful! I use visual studio...

template<class T1, class T2, int max>
class Kolekcija{
T1 * _member1[max];
T2 * _member2[max];
int * _currentlyMember;
public:
//1. construktor/destruktor
//2. AddElement
//3. RemoveElement
//4. operator-=
};
class Date{
public:
int _day, _month, _year;
Date(int d,int m,int g){
_day = d; _month= m; _year = y;
}
};
class PassedItem{
string _name;
Date _dateOfPassed;
public:
//1. construktor and destruktor function
//2. Info
};
class Student{
const int _numberOfIndex;
char * _FirstName;
Kolekcija<PassedItem *, int, 50> _passedItem;
public:
//1. construktor/destruktor
//2. AddPassedItem;
//3. RemovePassedItem
//4. operator <<
};
class Test{
string _NameOfTest;
string _NameOfItem;
vector<Student *> _registeredStudents;
public:
//1. construktor/destruktor
//2. AddStudent
//3. RemoveStudent
//4. Info
};
void main(){
}
Last edited on Dec 7, 2010 at 11:32pm
Dec 7, 2010 at 11:33pm
@hakspek: I hate to say this but that's not where you post new stuff at all. You should put it in the Begginers section but not on someone else's thread. Also, please put code in "code tags" like so. One last thing, when you want an answer, ask a question. "Please do this for me" is not a question.
As for the question the OP posted, I would either use CreateThread (look it up, it's in windows.h) or SDL_CreateThread (it's the same thing but it's not operating system dependent)
Dec 8, 2010 at 2:24am
Sometimes I wonder if the Standard C++ committee members are doing work or not. They approve new APIs too SLOWLY!!!!!!

I wish for below to be part of Standard C++ coming soon.

- Networking/Sockets API (includes email, ftp, scp, goher, http etc)
- 2D/3D Imaging API
- Audio/Video API
- Serialization API
- Threading API
- XML/JSON Parsing API
- Database Connectivity API
- LDAP API
- DNS API
- Security API
...

You can see I want more business-centric API. Anyone can add in to the above lis t:P
Last edited on Dec 8, 2010 at 2:27am
Dec 8, 2010 at 3:07am
Not going to happen.
C++ is designed to run on many different kinds of systems and it would be unreasonable to have all of that as part of the language specification. There are frameworks like Qt that provide the kind of functionality that is needed for desktop GUI applications.
It might be nice to have some more general-purpose, hardware-independent, header-only aids in the standard library, but since boost is quasi-standard, there is no practical need for that either.
Dec 8, 2010 at 3:21am
I believe Boost.org has threading, serialization correct ? So it just may happen cuz a lot of Boost.org API get approved to be in Standard C++ eventually. The complaint now is the Standard C++ committee is taking too long a time to get them approved!

I wonder what is so hard to stamp a chop, certify this API part of Standard C++ ? Afterall Boost.org already provide implementations already. Unless there are some non-technical issues like say political and geographical reasons behind the approval process? !

In comparison, Java-now Oracle APIs, Google APIs move at very fast pace. Maybe the Standard C++ committee members need some re-shuffling to bring in more enthusiastic members to get approval done faster :)
Dec 8, 2010 at 3:53am
Adding something to the standard library is a momentous and pretty much irreversible decision.
So before you add something you'd want to make sure that it is as "perfect" as reasonably possible, integrates well with the rest of the standard library etc., because it's not really feasible to change the API later (let alone remove it) when you realize that the solution wasn't all that good after all.
Libraries like Boost.Serialization are in much better hands when they're with boost: its API may change with every new version. Which is fine, because developers can choose an API by choosing a certain boost version for their project that they stick with.
People can also choose a different serialization library that they like better - they could still do that even if the standard library had its own serialization library, but it leaves a bit of a bad aftertaste to use an external library for something that the standard library already provides.
Dec 8, 2010 at 4:02am
because it's not really feasible to change the API later (let alone remove it) when you realize that the solution wasn't all that good after all.


I believe above problem has been faced by Java and even M$ on API changes. It cannot be avoided. The best solution is just to release a suffix 2 to indicate new API. Discontinue support of earlier version and persuade ppl to switch over to 2

E.g Windows used to be <winsock.h> then we have <winsock2.h>, the same applies to Google API, they have V1, V2, V3 etc...

The point I am trying to say is API will change. This problem cannot be avoided. Even if you spend a lot of time to come out with a "perfect" API, there is no guarantee it will NOT change with the passage of time.

Rather than incubate and release a "perfect" API, it should be better to faster release a stable version and increment them along as needed.

I believe my thinking deviates from Standard C++ committee members which is why I advocate re-shuffling of members :P
Dec 8, 2010 at 9:21am
I wish for below to be part of Standard C++ coming soon...

As Athar said, C++ is a general purpose language and so won't have all this business related stuff, just the tools to build them. It's also vendor neutral, so no one vendor can impose it's view on the world.

However, I do think that C++ looses out to Java and C# becuase it doesn't have a defacto standard library at this level. As a result, C++ remains a fragmented community with very little reuse, with no equivalent to Java's SE or EE API in terms of scale, cost and common use, and is relegated to building parts of systems where nothing else will do, a modern day assembler.


I believe my thinking deviates from Standard C++ committee members which is why I advocate re-shuffling of members

That's a bit extreme. And who would you replace them with, a team from Oracle or Microsoft?
Last edited on Dec 8, 2010 at 9:23am
Dec 9, 2010 at 8:58am
That's a bit extreme. And who would you replace them with, a team from Oracle or Microsoft?


I would prefer a team from Google instead. They do it fast and next year I got Chrome OS to play with. Nowadays all new "inventions" are coming out pretty fast from Google isn't it ?

Btw I am addicted to Google Maps, Google App Engine, Google Android, Google Chrome browser, Google Closure Compiler etc etc :)
Dec 9, 2010 at 9:38am
closed account (z05DSL3A)
Google do things fast because they only have to satisfy Google to make decisions. Microsoft can make changes to .net fast because they only have be satisfy themselves with the changes. The same goes for Java, as long as Oracle keep their eyes on the ball and don't cock it up. They do seem to be a bit distracted with chasing patents at the moment.

C++ standardisation has a lot of stakeholders and to satisfy them all takes a lot of work. I lot of work takes a lot of time.



Dec 9, 2010 at 11:32am

The same goes for Java, as long as Oracle keep their eyes on the ball and don't cock it up.


Actually with Java it is more like with C++ nowadays. There are lots of companies involved in the standarization process and Oracle can't do whatever it wishes to. And Java is also moving forward too slowly. Maybe not that slowly, but major releases every 2-3 years is plain too long.

Bjarne Stroustrup said something recently, that the C++ committee works in their "spare time" and they don't get any profits for that work. So this may explain that slowness. Another reason is that it is extremely hard to add something to C++, without breaking it. It is already very hard to learn, and making it even more complex would not help its popularity (as more and more universities get away from C++ as the obligatory language, there will be less and less programmers knowing it = less new projects started in this language).
Last edited on Dec 9, 2010 at 11:35am
Dec 9, 2010 at 9:49pm
I would prefer a team from Google instead.

Reeeeeeeally?
Dec 10, 2010 at 4:49pm
Haha then you can expect advertisements on all your applications. XD
Pages: 12