I have source code for USB communication ("USBThread class") written in Borland C++ and uses Visual Component Library("vcl.h").
Now my task is to port that to Visual C++, because we did not buy Borland C++.
But this "USBThread class" has inheritance from a base class in "vcl.h", called "TThread".
May I ask , in Visual C++, What kind of base class I can use to substitute "TThread' as new inheritance source?
The original code uses "WaitFor" and "Terminate" methods coming TThread,
I do not know how to implement these two menthods with Visual C++.
Thanks!
This is likely to be a difficult exerice imo but it looks like Boost.Thread using join for WaitFor and interrupt for Terminate would get you started.
I am basing this on review of the docs for VCL found here. I say this is likely to be difficult because VCL may have behaviour that is undocumented or otherwise unexpected.
Although you could translate the VCL's TThread class into C++, it would not work very well because it relies on some Delphi semantics that simply do not translate to C++ at all (in particular the TObject::AfterConstruction() method). You are best off simply re-writing USBThread to use Win32 thread functions directly, namely CreateThread() and WaitForSingleObject(). For Terminate(), you simply set a bool flag somewhere that your thread procedure can look at periodically and stop its work when set to true.
Related
I'm developing an application in C++11 which uses SDL as a library. I'm planning to add multithreaded support for my app. Is there any difference whether to use SDL_Thread or std::thread in my application?
Unless you're using a part of the SDL API that specifically requires SDL's thread handle type, you're better off using C++'s std::thread. The API is more idiomatic (e.g. the constructor allowing you to forward arguments to start function) and does not tie that code to what SDL provides.
The reason I mention the handle is if you actually need to pass an SDL_Thread *, there is no way to query for handle to the current thread. Granted std::thread does not provide this either, but since the interface is richer, it makes more sense to use.
Edit: Both interfaces seem to draw heavily from POSIX threads, but with one exception. std::thread does not have a built-in cancellation function. Normally, you don't want to kill a thread anyway without any sort of cleanup, but it is worth mentioning.
I have a program in c++ on visual studio'10. And have decided to create a simple user interface for it on VS'10
My question is, confusion raises when choosing among MFC, CLR, Win32 etc. which one should I go for implementing my UI?
Given the constraints of your choices and the fact that you've already chosen C++, then I would go with MFC. Had you chosen C, I would have told you Win32. Had you chosen C#, I probably would have recommended WPF.
Cycript is a console based application that is a blend of Objective-C and JavaScript. Cycript is very useful for dynamic analysis of iOS applications.
If you write any methods or the complete ipa with Swift is it still possible to hook the application on a jailbroken device? Or is Swift safe like "native C" Code on iOS ?
I'm not really familiar with Cycript but I have a little understanding of the Swift compiler.
Swift code will be more resistant to hooking but it should not be completely impossible. NSObject subclasses and Swift classes that are declared #objc should be as accessible as Objective-C code. Pure Swift code, especially in optimised builds would be harder to inject code into because they are often statically dispatched and in many cases will actually be inlined into the calling code.
Where code hasn't been inlined it may may be possible to patch the functions in memory themselves to jump to an alternative function but it wouldn't be as easy as just modifying function tables.
Where key functions have been inlined it may be possible to find and modify each usage if common patterns of code that could be identified and if the function is long enough it may be posible to patch in a jump to an alternate version but this would get really quite tricky.
I've found some guides on using shared memory in Ansroid OS. I've learned that shm_open is not exist in Android amymore due to memory leaks caused by forced killing processes by OS or user.
ASHMEM functions are developed instead. But I cannot find in my NDK the declaration of ashmem_create_region() and other function. Where they are?
As with so many things in Android, the answer is to use JNI. The Java class java.nio.MappedByteBuffer wraps ashmem and provides read/write methods to access it.
Unfortunately, if you're using shared memory to boost performance, multiple round trips through JNI aren't an attractive proposition. Cedric Fung proposes using reflection to retrieve the ashmem handle by name, which will work but may break in future frameworks. (This does happen, BTW. All it takes is somebody deciding that "mFD" is too vague and "mFileDescriptor" would be a better name, or some such.) If you want to play with fire, I'd suggest retrieving the descriptor by type rather than by name, since the type is very unlikely to change.
Cedric also proposes implementing a Binder in C++, but this puts you back where you started because Binder is also not included in the NDK. Instead, you'd need to pass the handle via a binder service implemented in Java.
It's a lot of work for such a simple feature, I know. It's easier to just mmap a file and use that instead, which is too bad since a basic file mapping isn't nearly as mobile-friendly as ashmem. :-(
the header is inside system/core/include/cutils/ashmem.h of the aosp.
You must not use it for a regular application as ashmem functions aren't part of the NDK:
https://groups.google.com/forum/#!topic/android-ndk/eS9QK8EY968
Is there a template that can be used to create threads when we program in
OO language ?
How to go about designing a threading package for an OO language?
Support
C++0x will support threads in the standard library.
As of now, each platform has its own way of implementing threads (Windows, POSIX) but you can use something such as boost::thread to not have to worry about platform-specific stuff.
In Java, there is a Thread class.
Methods
In general, to put a class into another thread, you will create a thread while passing that class into the thread. Then the thread will call a function in that class. Here is some pseudo-C++-code:
main()
{
Object myObject;
thread = CreateThread(threadFunction, myObject);
thread.join(); // wait for thread
}
threadFunction(Object theObject)
{
theObject.doSomething();
}
This is all simplified by the use of boost (or C++0x threads) in C++, and the Thread class in Java handles this for you.
Related Information
A large problem in threaded applications is synchronization of threads. This includes problems like race conditions and deadlocks, to name a couple.
Methods/object exist to help these problems, such as a mutex. A mutex can be locked by one thread, and any other threads that try to lock the mutex will be blocked until the original thread releases the mutex.
A semaphore is a generalized mutex.
There are other useful concepts as outlined in Eric's post.
Each framework has its own way of dealing with threads. I suggest you look up the java thread class in the the java documentation, and then perhaps look at the C++ standard headers for thread details.
Each programming language has specific ways of handling threads.
C++ relies a lot on Boost so you might wanna check that
Java
Basically everything you learn about concurrency should apply whatever the OS or language you're using. There are roughly 4 problems you must learn to avoid
Deadlocks
Livelocks
Race Conditions
Hunger
This isn't directly related to your questions but these are subjects you should learn in parallel to learning a particular syntax in a given language. Java is of course quite easy while C++ might be a little trickier, your pick
There are also a number of "well known methods" for synchronizing threads such as
Events
Locks
Monitor
Mutex
Semaphore
Barrier
...
this list goes on and on, but are basically helping "objects" or variables that will help you solve the 4 problems mentionned
In C/C++, threads are often implemented using functions. ("Start a new thread and run this function inside, destroys the thread when the function ends.")
Thread frameworks often allow to pass parameters to the function. One way of doing OO with threads is to pass the object pointer ("this") as the function parameters and then call a specific method on the object.
If you are using MFC then MFC provides some framework helper APIs to work with multi threading. Have a look at this article: Multithreading with C++ and MFC
In Java you can use the Thread class or the Executor interface, to which you can send a runnable object. Check out the java.util.concurrent package. And the concurrency trail of the Java Tutorial. Good Luck.
The main difference when it comes to OOP and multi-threading programming is that you would like your new thread to have as its start-up function a (non-static) method of an object.
Now there are a few programming models for this: implement a "Runnable" interface is the most common used one. Your class implements a standard "Runnable" interface that has a start-up method which is usually a virtual method called "run". You put your start-up code in that virtual method and when you need to spawn a thread you create a thread object (usually called Thread) and you pass to it an object of your class that implements the "Runnable" interface. Then you call the "start" method of that "Thread" object and that will take care that the start-up code you have written in your class will be run a a new thread. In this example both "Runnable" and "Thread" are provided by the chosen threading library or by the standard library/framework of the programming language of your choice. Also please note that the names of these two classes can vary.
There is a big difference between C++ and Java when it comes to multi-threading programming: C++ has not support for multi-threading as opposed to Java which has built-in support for that.
So for C++ you will need to use a specialized threading library such as Pthreads (http://en.wikipedia.org/wiki/POSIX_Threads) or more general purpose libraries such as Boost, ACE, POCO which already have support for threading. The least recommended way is to call directly the multi-threading related services of the OS.
For Java, no matter what edition you are using you will already have the support for multi-threading programming built-in.