| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 5341 人关注过本帖
标题:POSIX Threads
只看楼主 加入收藏
madfrogme
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:21
帖 子:1160
专家分:1106
注 册:2009-6-24
结帖率:98.63%
收藏
 问题点数:0 回复次数:4 
POSIX Threads
程序代码:
#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 编辑 ]
搜索更多相关主题的帖子: function include Create 
2012-09-22 11:23
madfrogme
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:21
帖 子:1160
专家分:1106
注 册:2009-6-24
收藏
得分:0 
#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 );
}

The quieter you become, the more you can hear
2012-09-22 11:36
madfrogme
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:21
帖 子:1160
专家分:1106
注 册:2009-6-24
收藏
得分:0 
用函数来对两个线程ID进行比较
int pthread_equal ( pthread_t tid1, pthread_t tid2);

线程可以通过调用pthread_self 函数获得自身的线程ID
pthread_t pthread_self(void);

The quieter you become, the more you can hear
2012-09-23 11:57
madfrogme
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:21
帖 子:1160
专家分:1106
注 册:2009-6-24
收藏
得分:0 
程序代码:
#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)),如果主线程不休眠,它就可能退出,这样在新线程有机会运行之前整个进程可能就已经终止了

这种行为特征依赖于操作系统中的线程实现和调度算法

The quieter you become, the more you can hear
2012-09-23 14:04
madfrogme
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:21
帖 子:1160
专家分:1106
注 册:2009-6-24
收藏
得分:0 
#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 编辑 ]

The quieter you become, the more you can hear
2012-09-23 21:45
快速回复:POSIX Threads
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015502 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved