创建一个脱离的线程时遇到问题。 这是我写的代码:
void* testFunction() { pthread_attr_t attr; int chk,rc; pthread_attr_init(&attr); printf("thread_attr_init: %dn",rc); pthread_attr_getdetachstate(&attr, &chk); printf("thread_attr_getdetachedstate: %dn",rc); if(chk == PTHREAD_CREATE_DETACHED ) printf("Detachedn"); else if (chk == PTHREAD_CREATE_JOINABLE) printf("Joinablen"); return NULL; } int main (int argc, const char * argv[]) { pthread_t thread1; pthread_attr_t attr; int rc; rc = pthread_attr_init(&attr); printf("attr_init: %dn",rc); rc = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); printf("attr_setdetachedstate: %dn",rc); rc = pthread_create(&thread1, &attr, testFunction, NULL); printf("attr_create: %dn",rc); sleep(4); pthread_cancel(thread1); return 0; }
问题是testFunction()
总是打印“Joinable”。 谁能告诉我哪里出错了?
您的testFunction
不会检查有关当前线程的任何内容,而只是检查刚刚创建的全新属性对象的初始分离标志。 而且,在POSIX线程API中,完全不可能恢复创建线程的属性或确定线程是否已分离。 您只需要相信实现的行为符合要求,就像您必须相信的那样,如果malloc(100)
返回非空指针,它指向一个可以存储至少100个字节的位置。 这是C的本质。
尝试运行以下代码。 这应该清除混乱。
/*------------------------------- join_01.c --------------------------------* On Linux, compile with: cc -std=c99 -pthread join_01.c -o join_01 gcc join_01.c -o join_01 -std=c99 -lpthread (Ubuntu) Check your system documentation how to enable C99 and POSIX threads on other Un*x systems. Copyright Loic Domaigne. Licensed under the Apache License, Version 2.0. *--------------------------------------------------------------------------*/ #include // sleep() #include #include #include // EXIT_SUCCESS #include // strerror() #include /***************************************************************************/ /* our macro for errors checking */ /***************************************************************************/ #define COND_CHECK(func, cond, retv, errv) if ( (cond) ) { fprintf(stderr, "n[CHECK FAILED at %s:%d]n| %s(...)=%d (%s)nn", __FILE__,__LINE__,func,retv,strerror(errv)); exit(EXIT_FAILURE); } #define ErrnoCheck(func,cond,retv) COND_CHECK(func, cond, retv, errno) #define PthreadCheck(func,rc) COND_CHECK(func,(rc!=0), rc, rc) /*****************************************************************************/ /* thread- dummy thread */ /*****************************************************************************/ void* thread(void* ignore) { sleep(1); return NULL; } /*****************************************************************************/ /* detach_state. Print detachstate of a thread. */ /*****************************************************************************/ /* * We find out indirectly if a thread is detached using pthread_join(). * If a thread is detached, then pthread_join() fails with EINVAL. * Otherwise the thread is joined, and hence was joinable. * */ void detach_state( pthread_t tid, // thread to check detach status const char *tname // thread name ) { int rc; // return code rc = pthread_join(tid, NULL); if ( rc==EINVAL ) { printf("%s is detachedn", tname); } else if ( rc==0 ) { printf("%s was joinablen", tname); } else { printf("%s: pthread_join() = %d (%s)n", tname, rc, strerror(rc) ); } } /*****************************************************************************/ /* main- main thread */ /*****************************************************************************/ int main() { pthread_t tid1, tid2, tid3; // thread 1,2 and 3. pthread_attr_t attr; // thread's attribute int rc; // return code /*--------------------------------------------------------*/ /* 1st test: normal thread creation */ /*--------------------------------------------------------*/ rc = pthread_create(&tid1, NULL, thread, NULL); PthreadCheck("pthread_create", rc); detach_state(tid1, "thread1"); // expect: joinable /*--------------------------------------------------------*/ /* 2nd test: detach thread from main thread */ /*--------------------------------------------------------*/ rc = pthread_create(&tid2, NULL, thread, NULL); PthreadCheck("pthread_create", rc); rc = pthread_detach(tid2); PthreadCheck("pthread_detach", rc); detach_state(tid2, "thread2"); // expect: detached /*--------------------------------------------------------*/ /* 3rd test: create detached thread */ /*--------------------------------------------------------*/ // set detachstate attribute to DETACHED // rc=pthread_attr_init(&attr); PthreadCheck("pthread_attr_init", rc); rc=pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); PthreadCheck("pthread_attr_setdetachstate", rc); // create thread now // rc = pthread_create(&tid3, &attr, thread, NULL); PthreadCheck("pthread_create", rc); detach_state(tid3, "thread3"); /*--------------------------------------------------------*/ /* that's all folks! */ /*--------------------------------------------------------*/ return EXIT_SUCCESS; }
输出应该是
$ ./join_01 thread1 was joinable thread2 is detached thread3 is detached
你的testFunction没有读取当前线程的属性。 如果系统中存在pthread_attr_init()
, pthread_getattr_np()
可能会帮助您,而不是调用pthread_attr_init()
,但是您需要将相关线程的pthread_t
传递到查询的任何位置。
需要了解更多c/c++开发分享Pthread创建为独立,也可以关注C/ C++技术分享栏目—计算机技术网(www.ctvol.com)!
以上就是c/c++开发分享Pthread创建为独立相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/c-cdevelopment/979460.html