我只是想知道,如果我使用一个属性和"pthread_attr_setdetachstate"带有参数的函数创建一个分离线程 (POSIX) PTHREAD_CREATE_DETACHED,我是否必须在程序结束时销毁该线程?
我知道我必须销毁为创建分离线程而创建的属性,但对于线程本身,我真的不知道。
我想做这样的事情:
void *do_work_son(void *data)
{
mystruct *d = (mystruct*)data;
while(true)
{
// d->whatever is corrupt
}
}
void start_thread(pthread_t *mt)
{
mystruct data = ...;
pthread_create(&(*mt), NULL, do_work_son, (void *)&data);
}
int main()
{
pthread mt;
start_thread(&mt);
// do more stuff here
pthread_join(mt, NULL);
}
Run Code Online (Sandbox Code Playgroud)
这个想法是由一些线程产生的,并继续做更多的工作main...然后当完成更多工作时,等待线程完成.
它编译得很好,但是在data内部访问时结构被破坏了do_work_son.我认为这是因为线程正在退出,即使我正在调用join main.如果我将pthread_{create,join}调用都移动到start_thread,它工作正常,但然后我的main函数被while循环阻止.这样做我疯了吗?
因此,当我运行我的代码时,我在pthread_join上遇到了分段错误.在我的pthread_join之后有一个没有运行的print语句.有谁知道为什么?你能给我一些关于如何解决这个问题的提示或想法吗?
输出打印出矩阵的所有行号,直到结束,然后它离开matrixCalc函数并在"创建线程后"打印.当我为1个线程添加一个参数时会发生这种情况.
我在这里包含了我的一小部分代码:
int main(int argc, char*argv[])
{
//takes in number of threads as 1st arg
pthread_attr_init(&attr);
//initialize matrix here
//passes num of threads through matrixcalc
for(i = 0; i < numberOfThreads; i++)
{
threadCount++;
pthread_create(&tid, &attr, matrixCalc(threadCount), NULL);
}
printf("after threads are created\n");
pthread_join(tid, NULL);
printf("after join\n");
exit(0);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是矩阵计算功能:
void *matrixCalc(threadCount)
{
int i, j, sum, tempNum, currentRow;
currentRow = threadCount;
sum=0;
while(currentRow < 1200)
{
//cycles through the column j for matrix B
for(j=0; j<500; …Run Code Online (Sandbox Code Playgroud) 我想创建多个线程(在下面的示例中为10)并让每个线程都运行一个函数.
这是我的代码:
#include <stdio.h>
#include <pthread.h>
typedef struct arg_struct {
int id;
int val;
} arg_struct;
void *printarg(void *params) {
arg_struct *args = (arg_struct *) params;
printf("id %i value %i\n", args->id, args->val);
return 0;
}
int main() {
int i = 0;
pthread_t threads[10];
for (i = 0; i < 10; i++) {
arg_struct arg;
arg.id = i;
arg.val = i + 10;
pthread_create(&(threads[i]), NULL, &printarg, &arg);
}
for (i = 0; i < 10; i++) {
pthread_join(threads[i], NULL);
} …Run Code Online (Sandbox Code Playgroud) 我试图将 pthread_join 与这个生产者-消费者程序一起使用,但我不断遇到分段错误。我的目的是等待所有生产者线程结束,然后终止所有消费者。但是在第一个线程加入后,我遇到了分段错误。我在网上搜索但没有找到任何有用的东西。有任何想法吗?
using namespace std ;
int main()
{
pthread_mutex_init(&mutex , NULL);
sem_init(&full , 0 ,0);
sem_init(&empty , 0 , BUFFER_SIZE);
pthread_t ProducerThread , ConsumerThread;
int *aa = new int [4];
for(int i = 0 ; i < 4 ; i++)
{
aa[i] = i;
pthread_t t;
int k= pthread_create(&t , NULL, Produce , &aa[i]);
if(k!=0)
cout<<"Errot"<<endl;
else
printf("Creating Producer %d \n", i);
}
int *bb = new int[2];
for(int i = 0 ; i < 2 ; i++) …Run Code Online (Sandbox Code Playgroud) 我是多线程编程的新手,我正在学习本教程。在本教程中,有一个简单的示例展示了如何使用pthread_create()和pthread_join()。我的问题:为什么我们不能放入pthread_join()与 相同的循环中pthread_create()?
参考代码:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define NUM_THREADS 2
/* create thread argument struct for thr_func() */
typedef struct _thread_data_t {
int tid;
double stuff;
} thread_data_t;
/* thread function */
void *thr_func(void *arg) {
thread_data_t *data = (thread_data_t *)arg;
printf("hello from thr_func, thread id: %d\n", data->tid);
pthread_exit(NULL);
}
int main(int argc, char **argv) {
pthread_t thr[NUM_THREADS];
int i, rc;
/* create a thread_data_t argument …Run Code Online (Sandbox Code Playgroud) 我想要做的是......
pthread_create(&producer_thread, &to_join, producer_routine, &queue);
pthread_detach(producer_thread);
...
...
pthread_join(producer_thread, NULL);
Run Code Online (Sandbox Code Playgroud)
这在某种程度上是可行的,在运行上面的代码时,它无法加入线程.
我是并行编程领域的新手,所以我决定摆弄pthread_join()子程序.我想出了以下代码来计算*X + Y,其中a是标量,X,Y是某种大小的向量.
这是我写的:
#include <pthread.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#define NUM_THREADS 4
#define VECTOR_SIZE 65
struct DAXPYdata
{
/* data */
long a;
long X[VECTOR_SIZE];
long Y[VECTOR_SIZE];
};
struct DAXPYdata daxpystr;
void *calcDAXPY(void *);
int main(int argc, char *argv[])
{
int vec_index;
/*Initialize vectors X and Y an scalar a*/
daxpystr.a = 57;
for(vec_index = 0 ; vec_index < 65 ; vec_index++){
daxpystr.X[vec_index] = vec_index + 1;
daxpystr.Y[vec_index] = vec_index + 2;
}
pthread_t call_thread[NUM_THREADS];
pthread_attr_t attr; …Run Code Online (Sandbox Code Playgroud) 我写了这个小程序来理解pthread_create和pthread_join,但我不明白为什么data在thread_join之后变量的值会被改变.它在调用pthread函数后打印为0.
#include <pthread.h>
#include <stdio.h>
void* compute_prime (void* arg)
{
int n = *((int*) arg);
printf("Argument passed is %d\n",n);
return (void *)n;
}
int main ()
{
pthread_t thread;
int data = 5000;
int value=0;
pthread_create (&thread, NULL, &compute_prime, &data);
pthread_join (thread, (void*) &value);
printf("The number is %d and return value is %d.\n", data, value);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出是
Argument passed is 5000
The number is 0 and return value is 5000.
Run Code Online (Sandbox Code Playgroud) 以下代码是从此站点获取的,并且显示了如何使用互斥锁。它同时实现了pthread_join和pthread_mutex_lock:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *functionC();
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
int counter = 0;
main()
{
int rc1, rc2;
pthread_t thread1, thread2;
/* Create independent threads each of which will execute functionC */
if( (rc1=pthread_create( &thread1, NULL, &functionC, NULL)) )
{
printf("Thread creation failed: %d\n", rc1);
}
if( (rc2=pthread_create( &thread2, NULL, &functionC, NULL)) )
{
printf("Thread creation failed: %d\n", rc2);
}
/* Wait till threads are complete before main continues. Unless we */
/* wait …Run Code Online (Sandbox Code Playgroud)