gogoWebsite

C++ Multithreading Practice Android

Updated to 3 hours ago

Table of contents

Android jni calls multithreading

Multi-threading that requires results

Windows Multithreading


Android jni calls multithreading

#include <>
 #include <thread>
 #include <android/>

 #define LOG_TAG "FaceRecognitionThread"
 #define ALOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)

 void faceRecognitionTask() {
     ALOGD("Face recognition thread started");

     while (true) {
         ALOGD("Running face recognition");

         std::this_thread::sleep_for(std::chrono::seconds(1));

         if (/* termination condition */) {
             break;
         }
     }

     ALOGD("Face recognition thread exiting");
 }

 extern "C" JNIEXPORT void JNICALL
 Java_com_example_myapp_MainActivity_startFaceRecognition(JNIEnv* env, jobject thiz) {
     std::thread t(faceRecognitionTask);
     (); // Let the thread run in the background and end it by itself
 }

Multi-threading that requires results

Not tested

#include <thread>
 #include <iostream>

 void backgroundTask() {
     while (true) {
         // Execute some background tasks, such as data processing
         std::cout << "Processing data in background..." << std::endl;

         // Check whether the termination signal is received. If so, the loop is exited
         if (shouldTerminate()) break;

         // Simulation processing time
         std::this_thread::sleep_for(std::chrono::seconds(1));
     }
 }

 int main() {
     std::thread worker(backgroundTask);
     ();

     std::cout << "Main thread continues..." << std::endl;

     // Execute other tasks of the main thread
     std::this_thread::sleep_for(std::chrono::seconds(10));

     // Send a termination signal to the background thread
     sendTerminateSignal();

     std::cout << "Main thread ends." << std::endl;
     return 0;
 }

In this example, the main thread continues to execute after creating and decoupling the worker thread and will not be blocked by the long-running tasks of the worker thread. At the same time, the worker thread regularly checks whether the termination signal has been received so that the execution can be terminated in time. This design ensures smooth execution of the main thread while providing appropriate control over background tasks.

Windows Multithreading

std thread is easier to use, but the socket with the system cannot call recv

The createthread function does not report an error in the mfc interface, and it reports an error in the interim:

BOOL CMFCApplication1App::InitInstance()
{

HANDLE h_thread= CreateThread(NULL, 0, Fun, NULL, 0, NULL);//Create multithreading

        CloseHandle(h_thread);

}

User interface threads often overload this function, and worker threads generally do not use it.InitInstance()

Calling this under the main thread under Python will result in an error, but if called under qthread, it will run normally.

E:\c++\MFCApplication1\MFCApplication1\

"CWinThread::CreateThread": The function does not accept 6 parameters MFCApplication1

Because the interface is used, the interface is originally an interface

c:\Program Files (x86)\Windows Kits\8.1\Include\um\

Solution: It might be fine if you use it

AfxBeginThread function used by mfc