本文目录一览

1,pthread如何在一个线程中终止另一个线程

如果需要只终止某个线程而不终止整个进程,可以有三种方法: 1.从线程函数return。这种方法对主线程不适用,从main函数return相当于调用exit。 2.一个线程可以调用pthread_cancel终止同一进程中的另一个线程。 3.线程可以调用pthread_exit终止自己。

pthread如何在一个线程中终止另一个线程

2,如何判断一个线程已经被pthread

还记得signal吗,大部分signal的默认动作是终止进程的运行,所以,我们才要用signal()去抓信号并加上处理函数。int pthread_kill(pthread_t thread, int sig);向指定ID的线程发送sig信号,如果线程代码内不做处理,则按照信号默认的行为影响整个进程,也就是说,如果你给一个线程发送了SIGQUIT,但线程却没有实现signal处理函数,则整个进程退出。pthread_kill(threadid, SIGKILL)也一样,杀死整个进程。如果要获得正确的行为,就需要在线程内实现signal(SIGKILL,sig_handler)了。OK,如果int sig是0呢,这是一个保留信号,一个作用是用来判断线程是不是还活着。我们来看一下pthread_kill的返回值:成功:0线程不存在:ESRCH信号不合法:EINVAL所以,pthread_kill(threadid,0)就很有用啦。int kill_rc = pthread_kill(thread_id,0);if(kill_rc == ESRCH)printf("the specified thread did not exists or already quit\n");else if(kill_rc == EINVAL)上述的代码就可以判断线程是不是还活着了。

如何判断一个线程已经被pthread

3,C语言多线程编程为什么要用pthread

3个线程使用的都是同一个info 代码 Info_t *info = (Info_t *)malloc(sizeof(Info_t));只创建了一个info pthread_create(&threads[i],NULL,calMatrix,(void *)info); 三个线程使用的是同一个 我把你的代码改了下
3个线程使用的都是同一个info 代码 info_t *info = (info_t *)malloc(sizeof(info_t));只创建了一个infopthread_create(&threads[i],null,calmatrix,(void *)info); 三个线程使用的是同一个我把你的代码改了下:#include #include #include int mtc[3] = { 0 }; // result matrix typedef struct { int prank; int *mta; int *mtb; }info_t; void* calmatrix(void* arg) { int i; info_t *info = (info_t *)arg; int prank = info->prank; fprintf(stdout,"calmatrix : prank is %d\n",prank); for(i = 0; i < 3; i++) mtc[prank] += info->mta[i] * info->mtb[i]; return null; } int main(int argc,char **argv) { int i,j,k = 0; int mta[3][3]; int mtb[3] = { 1 }; info_t *info = (info_t *)malloc(sizeof(info_t)*3); for(i = 0; i < 3; i++) for(j = 0; j < 3; j++) mta[i][j] = k++; /* 3 threads */ pthread_t *threads = (pthread_t *)malloc(sizeof(pthread_t)*3); fprintf(stdout,"\n");fflush(stdout); for(i = 0; i < 3; i++) { info[i].prank = i; info[i].mta = mta[i]; info[i].mtb = mtb; pthread_create(&threads[i],null,calmatrix,(void *)(&info[i])); } for(i = 0; i < 3; i++) pthread_join(threads[i],null); fprintf(stdout,"\n==== the matrix result ====\n\n"); fflush(stdout); for(i = 0; i < 3; i++) { fprintf(stdout,"mtc[%d] = %d\n",i,mtc[i]); fflush(stdout); } return 0; }矩阵的计算我忘记了,你运行看看结果对不对

C语言多线程编程为什么要用pthread

4,pthread函数怎么用求解

.关于编译时出现 对pthread_create未定义的引用 之类的错误的解决:由于pthread库不是Linux系统默认的库,连接时需要使用库libpthread.a,所以在使用pthread_create创建线程时,在编译中要加-lpthread参数: gcc -o pthread -lpthread pthread.c 特别的,如果这样还没解决的话: 按照上面编译了一下,还是一样的提示. 后面man gcc 才知道Usage: gcc [options] file... 因此需要将库链接放在末尾。 xs@vm:~/Desktop$ gcc -o pthread pthread.c -lpthread 2.关于pthread里的一些函数. pthread_join函数: 函数pthread_join用来等待一个线程的结束。 函数定义: int pthread_join(pthread_t thread, void **retval); 描述 : pthread_join()函数,以阻塞的方式等待thread指定的线程结束。当函数返回时,被等待线程的资源被收回。如果进程已经结束,那么该函数会立即返回。并且thread指定的线程必须是joinable的。 参数 : thread: 线程标识符,即线程ID,标识唯一线程。 retval: 用户定义的指针,用来存储被等待线程的返回值。 返回值 : 0代表成功。 失败,返回的则是错误号。 看下面一段程序:[cpp] view plain copy print?#include <pthread.h> #include <unistd.h> #include <stdio.h> void *thread(void *str) int i; for (i = 0; i < 10; ++i) sleep(2); printf( "This in the thread : %d\n" , i ); } return NULL; } int main() pthread_t pth; int i; int ret = pthread_create(&pth, NULL, thread, (void *)(i)); pthread_join(pth, NULL); for (i = 0; i < 10; ++i) sleep(1); printf( "This in the main : %d\n" , i ); } return 0; } 也就是说:子线程还没有执行完毕,main函数已经退出,那么子线程也就退出了! 如果我们不注释掉那一行,那么运行结果如下: 这说明:pthread_join函数的调用者在等待子线程退出后才继续执行! pthread_create函数: 声明: int pthread_create(pthread_t *thread, const pthread_attr_t *restrict_attr, void*(*start_rtn)(void*), void *restrict arg); 参数: 第一个参数*thread为指向线程标识符的指针。 第二个参数*restrict_attr用来设置线程属性,上面也可以用NULL,表示使用默认的属性。 第三个参数是线程运行函数的起始地址。 最后一个参数是运行函数的参数,NULL表示无参数。 另外,在编译时注意加上-lpthread参数,以调用链接库。因为pthread并非Linux系统的默认库,而是posix线程库,在Linux中将其作为一个库来使用,因此加上 -lpthread(或-pthread)以显示的链接该库。函数在执行错误时的错误信息将作为返回值返回,并不修改系统全局变量errno,当然也无法使用perror()打印错误信息。 pthread_t:pthread_t用于声明线程ID! 类型定义: typedef unsigned long int pthread_t; //come from /usr/include/bits/pthread.h sizeof (pthread_t) =4; pthread_attr_init函数: 声明:int pthread_attr_init(pthread_attr_t*attr); 返回值:返回0,表示函数初始化对象成功。失败时返回一个错误代码。 参数:指向一个线程属性的指针。 下面一个程序是书上的:[cpp] view plain copy print?/*小小的一个程序,折腾人个半死*/ #include <pthread.h> #include <unistd.h> #include <stdio.h> int sum; void *runner (void *param); int main(int argc, char *argv[]) pthread_t tid;/*线程标示符*/ pthread_attr_t attr; if (argc != 2)/*如果参数不为2个*/ fprintf (stderr, "usage:a.out<integer value>\n");/*报错*/ return -1; } if (atoi(argv[1] ) < 0) fprintf (stderr, "%d must be <= 0\n", atoi(argv[1])); return -1; } pthread_attr_init(&attr); /*初始化,得到默认的属性值*/ pthread_create(&tid, &attr, runner, argv[1]);/*创建一个线程*/ pthread_join(tid, NULL);/*等待子线程执行完毕*/ printf ("sum = %d\n", sum); return 0; } void *runner(void *param)/*子线程将会执行这个函数*/ int i, upper = atoi(param); sum = 0; for (i = 1; i <= upper; i++) sum += i; } pthread_exit(0); }
你的理解不对。 首先,pthread_setcancelstate()函数只是改变本线程(注意是本线程)的cancel state。所以t1进入fun()函数,执行到pthread_setcancelstate()函数时,只是改变了t1本身的cancel state,并不能改变t2的cancel state。 第二,线程执行到pthread_testcancel()函数时,并不一定会马上取消(退出)。 先描述一下取消一个线程的过程:1) 其他线程通过调用pthread_cancel()函数,向目标线程发送取消请求(cancellation request)。2) 取消请求发出后,根据目标线程的cancel state来决定取消请求是否会到达目标线程:a. 如果目标线程的cancel state是pthread_cancel_enable(默认),取消请求会到达目标线程。b. 如果目标线程的cancel state是pthread_cancel_disable,取消请求会被放入队列。直到目标线程的cancel state变为pthread_cancel_enable,取消请求才会从队列里取出,发到目标线程。3) 取消请求到达目标线程后,根据目标线程的cancel type来决定线程何时取消:a. 如果目标线程的cancel type是pthread_cancel_deferred(默认),目标线程并不会马上取消,而是在执行下一条cancellation point的时候才会取消。有很多系统函数都是cancellation point,详细的列表可以在linux上用man 7 pthreads查看。除了列出来的cancellation point,pthread_testcancel()也是一个cancellation point。就是说目标线程执行到pthread_testcancel()函数的时候,如果该线程收到过取消请求,而且它的cancel type是pthread_cancel_deferred,那么这个线程就会在这个函数里取消(退出),这个函数就不再返回了,目标线程也没有了。b. 如果目标线程的cancel type是pthread_cancel_asynchronous,目标线程会立即取消(这里的“立即”只是说目标线程不用等执行到属于cancellation point的函数的时候才会取消,它会在获得调度之后立即取消,因为内核调度会有延时,所以并不能保证时间上的“立即”)。 举个例子,说明一下这些与线程取消相关的函数的用法:void thread_function(void *arg)/*** 线程准备执行一些关键工作,在这个过程中不希望被取消。* 所以先通过pthread_setcancelstate()将本线程的cancel state* 设为disabled。*/pthread_setcancelstate(pthread_cancel_disable, null);/* 执行关键工作 */.../*** 关键工作执行完成,可以被取消。* 通过pthread_setcancelstate()将本线程的cancel state* 设为enabled。*/pthread_setcancelstate(pthread_cancel_enable, null);/*** 调用pthread_testcancel()函数,检查一下在cancel state* 为disabled状态的时候,是否有取消请求发送给本线程。* 如果有的话就取消(退出)。*/pthread_testcancel();/*** pthread_testcancel()返回了,表明之前没有取消请求发送给本线程,* 继续其余的工作。* 这时候如果有取消请求发送给本线程,会在下一次执行到* cancellation point的时候(例如sleep(), read(), write(), ...)时取消。*/.../*** 从这里开始,函数里不再包含cancellation point了。* 如果收到取消请求,将无法取消。所以先把本线程的cancel type* 设为asynchronous,收到取消请求将立即取消。*/pthread_setcanceltype(pthread_cancel_asynchronous, null);/* 不包含cancellation point的代码 */...}

文章TAG:Pthread  pthread如何在一个线程中终止另一个线程  
下一篇