Standard practice for implementing streams in C ++?

I am looking forward to interviews in C ++ in the coming weeks. (yay) So I was retraining C ++ and learning. Unfortunately, I realized that I never implemented threads in C ++, and is a bit concerned about the concurrency quiz.

As far as I can tell, C ++ uses pthreads on Linux and some other device on Windows. It's right? Is there another industry standard, more C ++ stream handling OO capabilities that I should expect? And are there any good web resources that you can point me to practice and learn about streams in C ++?

Thank!

+2


source to share


7 replies


Currently C ++ is completely unaware that streams exist. Various OS provide streaming libraries to make them available. The next version of C ++, so called C ++ 0x, is about to create a standard threading library. If I started a multi-threaded application today, I would go with boost streams or streams that were part of any package I could use, i.e. QT or WxWidgets.



+5


source


There is a boost threads library which is probably the closest to the standard.



Typically streams are supplied by the operating system, so you get what the OS provides. Also, people new to threading often find themselves in the GUI to allow background computation to not block the GUI, and so people tend to use threading functionality provided by a specific GUI framework (MFC / Qt, etc.).

+9


source


Ok, until C ++ 0x gets here, there is no standard way to do streams in C ++. You can use any tools provided by your operating system. So yes, if you are on a UNIX-like operating system, you can use pthreads. On Windows, you can use the Windows API.

There are third party tools out there that try to provide a uniform and portable streaming API, for example. increase threads and QT.

It is also not difficult to write your own portable abstraction layer. We did this because the API accelerator didn't have everything we needed a few years ago (no way to set priority, for example).

+2


source


On Windows, the only way to create threads is using the win32 API. Every library that can be built to create threads on Windows ends up using win32CreateThread()

QT Contains a nice C ++ wrapper around the thread, which is cross platform. It is usually good practice to have a class MyThread

that contains all the details about setting up a stream, checking for error codes, getting an exit code, etc. The class MyThread

would have a pure virtual function called run()

, which is designed to actually do what you want the thread to do. It is assumed that the user of the MyThread class inherits and implements it run()

so you can isolate the user of the class from the details for actually creating the thread.

MyThead

must also have a method start()

that initiates the thread. The thread will start at some entry point inside the class (usually it should be a static method), and then this results in a call to the run()

user method .

+1


source


Besides Qt, wxWidgets, Boost, and native OS-provided threads, you can just google for threading libraries for C ++. They're probably more portable and lightweight (if all you're looking for are streams). However, if you have a need for additional tools, and the above libraries provide them, then use them and use them. Boost is especially good, it has other features as well, but admittedly this is a streaming library, as Brian Neal said, is limited in some ways.

+1


source


For your interview, neither boost nor qt are helpful at all. you could just use them as high level libraries and interfaces and no one asked you how to use boost or qt in an interview like this. For understanding threads and mutexes, etc. See the doc http://code.google.com/p/effoaddon/downloads/list titled EffoAddons.pdf.

Raw source 
http://effoaddon.googlecode.com/svn/trunk/devel/effo/codebase/addons/thrd/src/thrd/thrd.cpp,
 high level abstract interface 
http://effoaddon.googlecode.com/svn/trunk/devel/effo/codebase/addons/thrd/include/thrd_i.h,
 and code support wait and signal 
http://effoaddon.googlecode.com/svn/trunk/devel/effo/codebase/addons/queue/include/iqb_ops_i.h.

      

you are better off learning something basic and not just high level interfaces, although write C ++.

+1


source


I have seen several real world streaming implementations using C ++, and they were all implemented by someone who wrote a class Thread

to wrap a basic O / S API

For example, on Windows there is an API CreateThread

: your Thread

class will pass a value this

to an void* lpParameter

API parameter CreateThread

; and yours LPTHREAD_START_ROUTINE

, which you implement as a private static method of the class Thread

, then needs to be cast void*

back to the Thread*' in order to get the

Thread instance .

0


source







All Articles