标签: pthread-join

Java多线程概念和join()方法

我对join()Java中的Threads中使用的方法感到困惑.在以下代码中:

// Using join() to wait for threads to finish.
class NewThread implements Runnable {

    String name; // name of thread
    Thread t;

    NewThread(String threadname) {
        name = threadname;
        t = new Thread(this, name);
        System.out.println("New thread: " + t);
        t.start(); // Start the thread
    }
// This is the entry point for thread.

    public void run() {
        try {
            for (int i = 5; i > 0; i--) {
                System.out.println(name + ": " + i);
                Thread.sleep(1000);
            }
        } …
Run Code Online (Sandbox Code Playgroud)

java multithreading synchronized pthread-join

57
推荐指数
2
解决办法
11万
查看次数

我是否需要加入我的应用程序中的每个线程?

我是多线程的新手,我需要了解关于"连接"的全部想法,我是否需要加入我的应用程序中的每个线程?以及它如何与多线程一起工作?

c++ multithreading pthread-join

8
推荐指数
1
解决办法
4126
查看次数

我什么时候应该在多线程编程中使用“锁定”?

我什么时候应该在多线程编程中使用“锁定”?只是锁定每个线程将要修改的区域或锁定每个线程可以访问的区域即使它不会被修改?

struct share_data {
    /* share data */
    thread_id;
}

thread 1 will exceute main() function:
   Initial share data. /* need lock */

   join all thread(share_data.thread_id, &status)   /* access share data thread_id, lock or not? */
   exit.

other threads will:
   access share_data, /* lock or not? */
   modify share_data, /* lock */
   exit.
Run Code Online (Sandbox Code Playgroud)

感谢您的关注,如果您有更多时间,有关真实代码的更多细节:

/* 
the number of threads will be input by user. Structure "tdata" and "tlist" stores 
information of each thread, including: "tid" - thread id which is …
Run Code Online (Sandbox Code Playgroud)

multithreading pthreads pthread-join

5
推荐指数
1
解决办法
4286
查看次数

分离的pthread会导致内存泄漏

当使用未分离的pthreads终止进程时,存在已知的内存泄漏.但是,分离线程似乎不是一个解决方案.请考虑以下最小示例:

#include <pthread.h>
#include <stdio.h>

static void* thread(void* _) {
  for(;;); return NULL;
}

int main(void) {
  pthread_attr_t attr; 
  pthread_attr_init(&attr);
  pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  pthread_t tid; pthread_create(&tid, &attr, thread, NULL);
  pthread_attr_destroy(&attr);
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

创建具有无限循环的分离线程,并立即终止该过程.根据pthread_detach(3),一旦整个过程终止,线程的资源应该自动释放回系统.然而,这显然不是正在发生的事情:

gcc -pthread c.c
valgrind --leak-check=full a.out

==9341== Command: a.out
==9341==
==9341==
==9341== HEAP SUMMARY:
==9341==     in use at exit: 272 bytes in 1 blocks
==9341==   total heap usage: 1 allocs, 0 frees, 272 bytes allocated
==9341==
==9341== 272 bytes in 1 blocks are …
Run Code Online (Sandbox Code Playgroud)

c unix multithreading pthreads pthread-join

5
推荐指数
1
解决办法
4645
查看次数

pthread返回值到数组

我目前正在开发一个使用pthreads的项目.到目前为止,该项目启动了用户指定的线程数,并在每个线程上执行一些工作然后关闭.每个线程都存储在动态分配的内存数组中.我这样做使用:

threads = malloc(number_of_threads * sizeof(pthread_t));

然后我在for循环中创建每个线程:

pthread_create(&(threads[i]), NULL, client_pipe_run, (void *) &param[i]);
Run Code Online (Sandbox Code Playgroud)

我接下来需要做的是存储这些线程的返回值.我的理解是我需要将pthread_join传递给我想要存储返回值的指针的地址.这是我有点困惑的地方.到目前为止我的指针很好,然后我的大脑有一种融化的哈哈.这是我如何实现这一点的想法,但我不相信这是正确的:

int *return_vals = malloc(sizeof(int) * number_of_threads);
for(i = 0; i< number_of_threads; i++)
{
pthread_join(&(threads[i]),(void *) &(return_vals[i]));
}
Run Code Online (Sandbox Code Playgroud)

然后为了得到返回值,我会做类似的事情:

int val = *(return_val[0]);
Run Code Online (Sandbox Code Playgroud)

任何有关这方面的帮助将不胜感激!

c pthreads pthread-join

4
推荐指数
1
解决办法
7435
查看次数

pthread_join()用于异步线程

我编写了一个简单的演示程序,以便我能理解pthread_join()函数.

我知道如何使用pthread_condition_wait()函数来允许异步线程,但我正在尝试理解如何使用pthread_join()函数执行类似的工作.

在下面的程序中,我将Thread 1s ID 传递给Thread 2s函数.在Thread 2s函数中,我调用pthread_join()函数并传入Thread 1s ID.我希望这会导致线程1首先运行,然后线程2运行第二,但我得到的是它们都同时运行.

这是因为一次只有一个线程可以使用pthread_join()函数,并且当我从主线程调用它时我已经在使用pthread_join()函数了吗?

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

void *functionCount1();
void *functionCount2(void*);

int main()
{

    /* 
        How to Compile
         gcc -c foo
         gcc -pthread -o foo foo.o
    */

    printf("\n\n");

    int rc;
    pthread_t thread1, thread2;

    /* Create two thread --I took out error checking for clarity*/
    pthread_create( &thread1, …
Run Code Online (Sandbox Code Playgroud)

c multithreading asynchronous pthreads pthread-join

4
推荐指数
1
解决办法
6989
查看次数

如果线程在调用pthread_join之前退出会怎么样?

我有一个小代码

void *PrintHello(void *threadid)
{
   cout<<"Hello"<<endl;
   pthread_exit(NULL);
}

int main ()
{
   pthread_t threads_id;
   pthread_create(&threads_id, NULL, PrintHello, NULL);
   int i=0;
   for(;i<100;i++){cout<<"Hi"<<endl;}   
   pthread_join(threads_id,NULL);
   return 0;
}
Run Code Online (Sandbox Code Playgroud)

我在创作后的某个时候加入了这个帖子.如果主要尝试加入已经退出的线程会发生什么?

c c++ pthreads pthread-join

4
推荐指数
2
解决办法
3447
查看次数

为什么不能在同一个线程上执行多个pthread_joins?

来自https://computing.llnl.gov/tutorials/pthreads/:

连接线程可以匹配一个pthread_join()调用.在同一个线程上尝试多个连接是一个逻辑错误.

也来自"man pthread_join":

如果多个线程同时尝试使用同一个线程进行连接,则结果是未定义的.

但是,从程序员的角度来看,多个线程可能希望等待单个线程完成(类似于障碍),这是完全合理的.

例如,我们可能有thread1,thread2独立运行,我们可能希望两个线程都等到thread3完成.

这个限制背后有任何技术原因吗?

multithreading posix pthreads pthread-join

4
推荐指数
1
解决办法
1272
查看次数

销毁一个分离的线程 (POSIX)

我只是想知道,如果我使用一个属性和"pthread_attr_setdetachstate"带有参数的函数创建一个分离线程 (POSIX) PTHREAD_CREATE_DETACHED,我是否必须在程序结束时销毁该线程?

我知道我必须销毁为创建分离线程而创建的属性,但对于线程本身,我真的不知道。

c linux multithreading pthreads pthread-join

3
推荐指数
1
解决办法
1327
查看次数

C 中 PThread 的合并排序

旁注:我开始学习如何使用 pthreads,并且开始理解这个概念。我一直在这里使用这个示例脚本(用 C++ 编写)来管理带有线程的合并排序:https ://www.geeksforgeeks.org/merge-sort-using-multi-threading/

由于我用 C 而不是 C++ 编写自己的合并排序,因此我重写了此示例脚本来测试并注意到一个问题。MAX 20我决定选择 15 个元素,而不是 20 个元素的数组。我注意到排序/合并无效(但有点接近),我不明白为什么......此外,我还更改了要使用的代码不同数量的线程而不是所以THREAD_MAX 4我可以将其更改为 5 或 10 个线程。

是否是合并产生了无效结果?我已在 main() 中对其进行了评论。

我的 C++ 到 C 的转换如下:

#include <pthread.h>
#include <time.h>
#include <stdlib.h>

// number of elements in array
#define MAX 15

// number of threads
#define THREAD_MAX 4

//using namespace std;

// array of size MAX
int a[MAX];
int part = 0;

// merge function for merging two parts
void merge(int low, int …
Run Code Online (Sandbox Code Playgroud)

c mergesort multithreading pthreads pthread-join

3
推荐指数
1
解决办法
1万
查看次数