zoukankan      html  css  js  c++  java
  • UNIX环境高级编程第11章线程

    Y)]ICD@GDFRWELG04SI5V]X

    Z~6QF84}N0LOMJAD(QK{`4G

    GWNF97[]85}){@M(S276Q}E

    9KK2WW(ENX~LZQ$LO_EGTUF

    {~P61A6}9(_O4@}3R0ZHV12

    OFPR))EAH9_BX1NEN9K@6TV

    程序清单11-1打印线程ID

    // threads/threadid.c 11-1
    #include "apue.h"
    #include <pthread.h>
    
    pthread_t ntid;
    
    void printids(const char* s)
    {
        printf("%d ", (unsigned int)ntid);
        printf("%ld ", (unsigned long)ntid);
        pid_t     pid;
        pthread_t tid;
    
        pid = getpid();
        tid = pthread_self();
        // printf("%s pid %u tid %u (0x%x)
    ", s, (unsigned int)pid, (unsigned int)tid, (unsigned int)tid);
        printf("%s pid %lu tid %lu (0x%lx)
    ", s, (unsigned long)pid, (unsigned long)tid, (unsigned long)tid);
    }
    
    void* thr_fn(void* arg)
    {
        printids("new thread: ");
        return((void *)0);
    }
    
    /*This example has two oddities, which are necessary to handle races between the main
     * thread and the new thread. (We'll learn better ways to deal with these conditions later
     * in this chapter.) The first is the need to sleep in the main thread. If it doesn't sleep, the
     * main thread might exit, thereby terminating the entire process before the new thread
     * gets a chance to run. This behavior is dependent on the operating system's threads
     * implementation and scheduling algorithms.
     * The second oddity is that the new thread obtains its thread ID by calling
     * pthread_self instead of reading it out of shared memory or receiving it as an
     * argument to its thread-start routine. Recall that pthread_create will return the
     * thread ID of the newly created thread through the first parameter (tidp). In our
     * example, the main thread stores this ID in ntid, but the new thread can¡¯t safely use it.
     * If the new thread runs before the main thread returns from calling pthread_create,
     * then the new thread will see the uninitialized contents of ntid instead of the thread ID.
     */
    int main(void)
    {
        int err;
    
        err = pthread_create(&ntid, NULL, thr_fn, NULL);
        if (0 != err)
            err_quit("can't create thread: %s
    ", strerror(err));
        printids("main thread:");
        sleep(1);
        return 0;
    }
    
     
    all: threadid
    threadid: threadid.c
    	g++ -g -Wall threadid.c ../lib/libapue.a -I ../include -lpthread -o threadid
    clean:
    	rm threadid
    
     

    ($96]3D7J`ID{YT2%ULYHBK

     

    P_OH]47DHLP5(MN~8[CU_G5

     

    这和我在虚拟机centos6.3中测试的情况不同,centos下进程ID是相同的(3607),并不是不同的。

    K18O_03W)3Y1}P}YDN~XRG4

     

     

    810de12d-73cc-45d0-928e-a7eb0766ea91

  • 相关阅读:
    Swift 泛型和闭包结合使用
    Swift中避免在多个文件中重复import相同的第三方包
    iOS AVAudioPlayer播放音频时声音太小
    python中装饰器的原理以及实现,
    python-网易云简单爬虫
    python模拟SQL语句操作文件
    python学习第二天-基本数据类型常用方法
    python学习第一天-语法学习
    iOS 出现错误reason: image not found的解决方案
    Swift as as!和as?的区别
  • 原文地址:https://www.cnblogs.com/sunyongjie1984/p/4278041.html
Copyright © 2011-2022 走看看