当前位置: 技术问答>linux和unix
线程编程的问题
来源: 互联网 发布时间:2016-04-23
本文导语: /* * thrdcancel.c - Illustrate thread cancellation */ #include #include #include #include void task1(int *counter); void task2(int *counter); void cleanup(int counter1, int counter2); int g1 = 0; int g2 = 0; int main(int argc, char *argv[]) { pthread_...
/*
* thrdcancel.c - Illustrate thread cancellation
*/
#include
#include
#include
#include
void task1(int *counter);
void task2(int *counter);
void cleanup(int counter1, int counter2);
int g1 = 0;
int g2 = 0;
int main(int argc, char *argv[])
{
pthread_t thrd1, thrd2;
int ret;
/* Create the first thread */
ret = pthread_create(&thrd1, NULL, (void *)task1, (void *)&g1);//问题1:pthread_create创建线程后,task1函数是否立即开始执行?
if(ret) { //问题2:pthread_create中线程属性为NULL意味这什么?查man没找到NULL的情况。
perror("pthread_create: task1");
exit(EXIT_FAILURE);
}
/* Create the second thread */
ret = pthread_create(&thrd2, NULL, (void *)task2, (void *)&g2);
if(ret) {
perror("pthread_create: task2");
exit(EXIT_FAILURE);
}
pthread_join(thrd2, NULL); //问题3: pthread_join中参数NULL意味着什么,这个也没查到?
pthread_cancel(thrd1); /* Cancel the first thread */ //这个cancel为何没起作用?虽然task1()中设置了DISABLE,但后来又ENABLE了呀?
pthread_join(thrd1, NULL);
cleanup(g1, g2);
exit(EXIT_SUCCESS);
}
void task1(int *counter)
{
/* Disable thread cancellation */
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
while(*counter