
你可以写一个继承了runnable接口的类,在这个类中添加传递句柄参数的方法,然后用
new
thread(runnable
run);这个方法创建线程,就可以把句柄传递过去了。如:
class
a
implements
runnable{
frame
fram
线程创建
函数原型:intpthread_create(pthread_trestrict tidp,const pthread_attr_t restrict attr,void (start_rtn)(void),void restrict arg);
返回值:若是成功建立线程返回0,否则返回错误的编号。
形式参数:pthread_trestrict tidp要创建的线程的线程id指针;const pthread_attr_t restrict attr创建线程时的线程属性;void (start_rtn)(void)返回值是void类型的指针函数;void restrict arg start_rtn的形参。
线程挂起:该函数的作用使得当前线程挂起,等待另一个线程返回才继续执行。也就是说当程序运行到这个地方时,程序会先停止,然后等线程id为thread的这个线程返回,然后程序才会断续执行。
函数原型:intpthread_join(pthread_tthread, void value_ptr);
参数说明如下:thread等待退出线程的线程号;value_ptr退出线程的返回值。
返回值:若成功,则返回0;若失败,则返回错误号。
线程退出
函数原型:voidpthread_exit(void rval_ptr);
获取当前线程id
函数原型:pthread_tpthread_self(void);
互斥锁
创建pthread_mutex_init;销毁pthread_mutex_destroy;加锁pthread_mutex_lock;解锁pthread_mutex_unlock。
条件锁
创建pthread_cond_init;销毁pthread_cond_destroy;触发pthread_cond_signal;广播pthread_cond_broadcast;等待pthread_cond_wait。
线程之间是共享进程的内存空间的。所以,在一个线程中修改的内容,另外的线程是可以直接访问的。
你想在多个线程之间传递指针,应该是想 *** 作同一块内存。
建议定义一个全局指针,外加一个全局互斥量。
在不同线程中使用同一个指针修改数据之前,加锁互斥量,并且在结束数据 *** 作之后释放互斥量。
以上就是关于如何得到一个线程的句柄全部的内容,包括:如何得到一个线程的句柄、C语言多线程的 *** 作步骤、【C++多线程】 如何在线程之间传递指针。等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)