Pthreads学习日记1

    xiaoxiao2026-05-07  2

    1. Pthreads例子

    POSIX线程(POSIX threads),简称Pthreads,它是线程的POSIX标准。该标准定义了创建和操纵线程的一整套API。在类Unix操作系统(Unix、Linux、Mac OS X等)中使用Pthreads作为操作系统的线程。Windows操作系统也有其移植版pthreads-win32。Pthreads定义了一套C语言的类型、函数与常量,它以pthread.h头文件和一个线程库实现。如下所示:

    #include <pthread.h> #include <iostream> using namespace std; void* tprocess1(void* args) { while(1) { cout << "tprocess1" << endl; } return NULL; } void* tprocess2(void* args){ while(1){ cout << "tprocess2" << endl; } return NULL; } int main() { pthread_t t1; pthread_t t2; pthread_create(&t1, NULL, tprocess1, NULL); pthread_create(&t2, NULL, tprocess2, NULL); pthread_join(t1, NULL); return 0; }

    说明:g++ TestPthreads.cpp -o TestPthreads -lpthread

    2. Nsight Eclipse Edition开发Pthreads应用程序

    主要是配置Cross G++ Linker中的Libraries。如下所示:

    参考文献:

    [1] POSIX Threads Programming:https://computing.llnl.gov/tutorials/pthreads/

    [2] pthreads的基本用法:http://www.ibm.com/developerworks/cn/linux/l-pthred/

    转载请注明原文地址: https://ju.6miu.com/read-1309446.html
    最新回复(0)