Oha*_*esh 14 c c++ linux multithreading pthreads
我想从主线程开始一个新线程.我不能使用join,因为我不想等待线程退出而不是继续执行.
基本上我需要的是像pthread_start(...),但是找不到它.
编辑:
因为建议create_thread的所有答案应该启动线程问题是在下面的简单代码中它不起作用.以下程序的输出是"主线程".看起来子线程从未执行过.知道我错了吗?
在Fedora 14 GCC 4.5.1版上编译并运行
void *thread_proc(void* x)
{
printf ("sub thread.\n");
pthread_exit(NULL);
}
int main()
{
pthread_t t1;
int res = pthread_create(&t1, NULL, thread_proc, NULL);
if (res)
{
printf ("error %d\n", res);
}
printf("main thread\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Jam*_*nze 20
启动线程的功能pthread_create不是
pthread_join.您只pthread_join在准备等待和重新同步时使用,如果您分离线程,则根本不需要使用它.您也可以从其他线程加入.
在退出之前(通过调用exit或从返回main),您必须确保没有其他线程正在运行.实现此目的的一种方式(但不是唯一的方法)是加入您创建的所有线程.
| 归档时间: |
|
| 查看次数: |
26514 次 |
| 最近记录: |