注册 登录
编程论坛 Linux系统管理

POSIX Threads

madfrogme 发布于 2012-09-22 11:23, 5351 次点击
程序代码:

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

void *print_message_function( void *ptr );

int main()
{
     pthread_t thread1, thread2;
     char *message1 = "Thread 1";
     char *message2 = "Thread 2";
     int  iret1, iret2;

    /* Create independent threads each of which will execute function */

     iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1);
     iret2 = pthread_create( &thread2, NULL, print_message_function, (void*) message2);

     /* Wait till threads are complete before main continues. Unless we  */
     /* wait we run the risk of executing an exit which will terminate   */
     /* the process and all threads before the threads have completed.   */

     pthread_join( thread1, NULL);    /* wait for thread termination: int pthread_join(pthread_t thread, void **value_ptr);*/
     pthread_join( thread2, NULL);     /*NULL 表示对线程的返回值不感兴趣*/

     printf("Thread 1 returns: %d\n",iret1);
     printf("Thread 2 returns: %d\n",iret2);
     exit(0);
}

void *print_message_function( void *ptr )
{
     char *message;
     message = (char *) ptr;
     printf("%s \n", message);
}


[ 本帖最后由 madfrogme 于 2012-9-23 22:00 编辑 ]
4 回复
#2
madfrogme2012-09-22 11:36
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

void *functionC();
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
int  counter = 0;

int main()
{
   int rc1, rc2;
   pthread_t thread1, thread2;

   /* Create independent threads each of which will execute functionC */

   if( (rc1=pthread_create( &thread1, NULL, &functionC, NULL)) )
   {
      printf("Thread creation failed: %d\n", rc1);
   }

   if( (rc2=pthread_create( &thread2, NULL, &functionC, NULL)) )
   {
      printf("Thread creation failed: %d\n", rc2);
   }

   /* Wait till threads are complete before main continues. Unless we  */
   /* wait we run the risk of executing an exit which will terminate   */
   /* the process and all threads before the threads have completed.   */

   pthread_join( thread1, NULL);
   pthread_join( thread2, NULL);

   exit(0);
}

void *functionC()
{
   pthread_mutex_lock( &mutex1 );
   counter++;
   printf("Counter value: %d\n",counter);
   pthread_mutex_unlock( &mutex1 );
}
#3
madfrogme2012-09-23 11:57
用函数来对两个线程ID进行比较
int pthread_equal ( pthread_t tid1, pthread_t tid2);

线程可以通过调用pthread_self 函数获得自身的线程ID
pthread_t pthread_self(void);
#4
madfrogme2012-09-23 14:04
程序代码:
#include "utilites.h"

pthread_t ntid;

void
printtids(const char *s) {
    pid_t pid;
    pthread_t tid;
    pid = getpid();
    tid = pthread_self();
    printf("%s pid %u tid %u (0x%x)\n",s,(unsigned int)pid,(unsigned int)tid, (unsigned int)tid);
}

void *
thr_fn(void *arg) {
    printtids("new thread: ");
    return((void*)0);
}

int
main(void) {
    int err;
    err = pthread_create(&ntid, NULL, thr_fn,NULL);
    if(err != 0)
        err_quit("can't create thread: %s\n",strerror(err));
    printtids("main thread: ");
//    sleep(1);               
    exit(0);
}


主线程需要休眠(sleep(1)),如果主线程不休眠,它就可能退出,这样在新线程有机会运行之前整个进程可能就已经终止了

这种行为特征依赖于操作系统中的线程实现和调度算法
#5
madfrogme2012-09-23 21:45
#include "utilites.h"
void *
thr_fn1(void *arg) {
    printf("thread 1 returning\n");
    return ((void *)1);
}
void *
thr_fn2(void *arg) {
    printf("thread 2 exiting\n");
    return ((void *)2);
}
int
main(void) {
    int         err;
    pthread_t     tid1, tid2;
    void         *tret;
    if((err = pthread_create(&tid1, NULL, &thr_fn1, NULL)) != 0)
        err_quit("can't create thread 1: %s\n", strerror(err));
    if((err = pthread_create(&tid2, NULL, &thr_fn2, NULL)) != 0)
        err_quit("can't create thread 2: %s\n", strerror(err));
   
    if((err = pthread_join(tid1, &tret)) != 0)
        err_quit("can't join with thread 1: %s\n", strerror(err));
    printf("thread 1 exit code %d\n", (int)tret);

    if((err = pthread_join(tid2, &tret)) != 0)
        err_quit("can't join with thread 2: %s\n", strerror(err));
    printf("thread 2 exit code %d\n", (int)tret);
    exit(0);
}

当一个线程通过调用pthread_exit 退出或者简单地从启动例程中返回时,

进程中的其他线程可以通过调用pthread_join函数获得该线程的退出状态


[ 本帖最后由 madfrogme 于 2012-9-23 22:48 编辑 ]
1