create thread of C++ on linux

This is a simple project on C++(ubuntu16.04), try to create a thread and define a condition_variable to notify thread to do something. I compile this project by cmake. So 2 files is needed: main.cpp & CMakeLists.txt

//-------------- STEP 1:  create main.cpp -----------------

#include <iostream>
#include <mutex>
#include <thread>
#include <condition_variable>
#include <unistd.h>

using namespace std;

int number = 0;
mutex mtx;
condition_variable cond;
void test_thread();

int main(int argc, char *argv[]){
    thread new_thread(test_thread);
    sleep(3);
    while(1){
        unique_lock<mutex> locker(mtx);
        number++;
        cout << "set num: " << number << endl;
        cond.notify_all();
        locker.unlock();
        sleep(3);
    }
    new_thread.join();
    return 0;
}

void test_thread(){
    cout << "I'm get in this new thread" << endl;
    while(true){
        unique_lock<mutex> locker(mtx);
        cond.wait(locker);
        cout << "--- get num: " << number << endl;
        locker.unlock();
    }
}

//-------------- STEP 2: create CMakeLists.txt -----------------

cmake_minimum_required(VERSION 3.15.0)
project(test_hello)
set(CMAKE_CXX_FLAGS "-std=c++11 -g -O0")

find_package(Threads)

message(STATUS "This is BINARY dir" ${HELLO_BINARY_DIR})
message(STATUS "This is SOURCE dir" ${HELLO_SOURCE_DIR})

add_executable(hello main.cpp)
target_link_libraries(hello ${CMAKE_THREAD_LIBS_INIT})

//-------------- STEP 3: compile and test this project -----------------

$ mkdir build

$ cd build

$ cmake ..

$ make

$ ./hello

finally, you should find some printing lines looks like:

I'm get in this new thread
set num: 1
--- get num: 1
set num: 2
--- get num: 2
set num: 3
--- get num: 3
set num: 4
--- get num: 4
set num: 5
--- get num: 5
set num: 6

......

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#中,你可以使用Microsoft.Office.Interop.PowerPoint来实现PPT文档PDF文档。具体实现步骤如下: 1. 首先,你需要安装Microsoft PowerPoint软件,并且在Visual Studio中添加对Microsoft.Office.Interop.PowerPoint的引用。 2. 在代码中创建一个PowerPoint.Application对象,并打开PPT文档。 ``` PowerPoint.Application pptApplication = new PowerPoint.Application(); PowerPoint.Presentation pptPresentation = pptApplication.Presentations.Open(@"C:\path\to\ppt\file.pptx"); ``` 3. 使用SaveAs方法将PPT文档转换为PDF文档。 ``` pptPresentation.SaveAs(@"C:\path\to\pdf\file.pdf", PowerPoint.PpSaveAsFileType.ppSaveAsPDF); ``` 4. 关闭PPT文档和PowerPoint.Application对象。 ``` pptPresentation.Close(); pptApplication.Quit(); ``` 完整的代码示例: ``` using System; using PowerPoint = Microsoft.Office.Interop.PowerPoint; namespace PPTtoPDF { class Program { static void Main(string[] args) { // 创建PowerPoint.Application对象 PowerPoint.Application pptApplication = new PowerPoint.Application(); // 打开PPT文档 PowerPoint.Presentation pptPresentation = pptApplication.Presentations.Open(@"C:\path\to\ppt\file.pptx"); // 将PPT文档转换为PDF文档 pptPresentation.SaveAs(@"C:\path\to\pdf\file.pdf", PowerPoint.PpSaveAsFileType.ppSaveAsPDF); // 关闭PPT文档和PowerPoint.Application对象 pptPresentation.Close(); pptApplication.Quit(); } } } ``` 注意:在运行代码之前,请确保Microsoft PowerPoint已经安装在你的计算机上,并且在Visual Studio中添加了对Microsoft.Office.Interop.PowerPoint的引用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值