Linux 新建线程 简单使用

PS:在 Android 下运行正常。

#include "MainWindow.h"
#include "ui_MainWindow.h"

#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

int main_z();

void MainWindow::on_pushButton_clicked()
{
    main_z();
}


// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***

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

void* thread1(void*);

int main_z()
{
    qDebug() << "main_z in";
    pthread_t tid1;
    int rc1=0;

    rc1 = pthread_create(&tid1, NULL, thread1, &tid1);
    if(rc1 != 0)
    {
        QString str1 = __func__;
        QString str2 = strerror(rc1);
        qDebug() << str1 + str2;
    }
    qDebug() << "main_z out";
    //exit(0);
}

void* thread1(void* arg)
{
    qDebug() << "thread1 in";
    ushort us = (unsigned int)pthread_self();
    qDebug() << "this is thread1, thread id is " +QString::number(us);
    qDebug() << "thread1 out";
    pthread_exit(0);
}

  

原文地址:https://www.cnblogs.com/codeskilla/p/4933619.html