子线程01_简单例子

Qt5.3.2代码:

 

1、pro

TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt

SOURCES += main.cpp

include(deployment.pri)
qtcAddDeployment()

QMAKE_CXXFLAGS += -pthread  #这里加入gcc编译参数。这是for cpp的,for c 使用 QMAKE_CFLAGS
LIBS += -pthread       #上面和这里,使用 "-lpthread" 也可以,它们的区别 还不清楚...

 

2、CPP

//#include <iostream>
//using namespace std;

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

void *ThreadFunc(void* /*_args*/)
{
    printf("In function ThreadFunc() - A\n");
    sleep(2);
    printf("In function ThreadFunc() - B\n");
    char* pcThreadReturn = "Thank you for your CPU time .";
    pthread_exit((void*)pcThreadReturn);
    printf("In function ThreadFunc() - C\n"); // ZC: 这一句不会被执行
    //return NULL; // ZC: 这里不需要 return?
}

int main()
{
    pthread_t pthread = 0;
    int iRtn = pthread_create(&pthread, NULL, ThreadFunc, NULL);
    if (iRtn)
    {
        perror("pthread_create");
        return 0;
    }

    printf("Wait for subthread to finish .\n");
    void* threadreturn;
    iRtn = pthread_join(pthread, &threadreturn);
    if (iRtn)
    {
        perror("pthread_join");
        return 0;
    }

    printf("Thread joined, it returned \" %s \"\n", (char*)threadreturn);
    sleep(1);

    printf("Main thread out\n");
//    cout << "Hello World!" << endl;
    return 0;
}

 

 

 

3、

 

posted @ 2016-08-10 14:43  LinuxCode  阅读(125)  评论(0编辑  收藏  举报