我是新来的,而且我在编程方面也相对较新.我用C编写了一个程序,我需要使用pthread来加速它.我尝试使用OpenMP这样做,但我不知道如何调试它.此外,我需要找出程序是否使用pthreads和时间更快,但我不知道如何在我的代码中写这个.这是我的代码
enter code here
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <pthread.h>
#define NTHREADS 2
#define FYLLO(komvos) ((komvos) * 2 + 1)
long factorial(long);
void heap_function (int [], int, int );
void make_heap(long [], int );
void pop_heap(long [], int );
struct thread_data
{
long int n;
long int k;
long *b;
};
main()
{
long int n,k,c,fact=1;
long *a,*b,*d,p[k];
int i,j,rc;
int q[]={2,3,4,5,6,7,8,9,12,13,14,15,16};
pthread_t thread[NTHREADS];
struct thread_data threada;
for(i=0;i<NTHREADS;i++)
{
threada.n=n;
threada.k=k;
threada.b=b;
pthread_create (&thread[i], NULL, (void *)&threada);
} …Run Code Online (Sandbox Code Playgroud) c parallel-processing performance pthreads serial-processing
我写了这段代码:
void* th (void* arg)
{
sleep(1);
for(int i=0; i<1000;i++)
{
fprintf(stderr,"%d\t",i);
}
pthread_exit(NULL);
}
int main(int argc, char** argv)
{
pthread_t thread;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
pthread_create(&thread,&attr,th,NULL);
pthread_attr_destroy(&attr);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
分离状态应该使线程不可连接,所以它应该在主进程终止后运行.但是它不打印数字,我看到的是线程终止而没有向stderr打印任何内容.
为什么不执行分离的线程?
这是一个说明我问题的最小例子
test.c的:
#include <stdio.h>
#include <pthread.h>
#define CORES 8
pthread_t threads [ CORES ];
int threadRet [ CORES ];
void foo ()
{
printf ("BlahBlahBlah\n" );
}
void distribute ( void ( *f )() )
{
int i;
for ( i = 0; i < CORES; i++ )
{
threadRet [ i ] = pthread_create ( &threads [ i ], NULL, f, NULL );
}
for ( i = 0; i < CORES; i++ )
{
pthread_join ( threads [ …Run Code Online (Sandbox Code Playgroud) 我可以创建pthreads并在每个pthread内部创建opencl环境并调用相同的内核.我想要做的是在同一设备上并行启动opencl内核.这可能吗?
谢谢回答.
是否有任何工具或应用程序将openmp转换为pthreads程序?请告诉我.
我发现很难解释这个问题,所以我会发布代码并解释会发生什么,然后问如何让它做我想做的事情.首先,我在子进程中创建一个线程:
pid_t childpid = fork();
if(childpid == -1){
cout << "Failed to fork." << endl;
}
else if(childpid == 0){
//request threads
pthread_t p1, p2, p3;
struct arg_struct args1, args2, args3;
args1.name = "data Joe Smith";
args1.num_req = n;
args1.buff_size = b;
pthread_create(&p1, NULL, &mythreadfunc, (void *)&args1);
}
Run Code Online (Sandbox Code Playgroud)
这是struct arg_struct:
struct arg_struct{
string name;
int num_req;
int curr_value;
int buff_size;
};
Run Code Online (Sandbox Code Playgroud)
而且 mythreadfunc:
void *mythreadfunc(void *arguments){
struct arg_struct *args = (struct arg_struct *)arguments;
string local_name = args->name;
int …Run Code Online (Sandbox Code Playgroud) 假设我有以下课程:
*.h:
class MyClass{
void caller();
int threadProcuder(void *args);
};
Run Code Online (Sandbox Code Playgroud)
*cpp:
void MyClass::caller()
{
pthread_t i;
pthread_create(&i,NULL,(void*(*)(void*))&MyClass::threadProcedure,(void*)this);
}
int MyClass::threadProcedure(void *args)
{
cout << "body of thread" << endl;
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,线程没有运行.
下面的程序产生这个输出:
$ ./test_condvar 9000
1343868189.623067126 1343868198.623067126 FIRST
1343868197.623132345 1343868206.623132345 TIMEOUT
1343868205.623190120 1343868214.623190120 TIMEOUT
1343868213.623248184 1343868222.623248184 TIMEOUT
1343868221.623311549 1343868230.623311549 TIMEOUT
1343868229.623369718 1343868238.623369718 TIMEOUT
1343868237.623428856 1343868246.623428856 TIMEOUT
Run Code Online (Sandbox Code Playgroud)
请注意,pthread_cond_timedwait跨行读取显示预期9秒的时间增量,但是向下读取列显示在8秒内返回ETIMEDOUT.
pthread lib是glibc 2.12.运行Red Hat EL6.uname -a节目2.6.32-131.12.1.el6.x86_64 #1 SMP Tue Aug 23 11:13:45 CDT 2011 x86_64 x86_64 x86_64 GNU/Linux
它看起来像pthread_cond_timedwait依赖于lll_futex_timed_wait超时行为.
关于在哪里搜索解释的任何想法?
#include <time.h>
#include <sys/time.h>
#include <pthread.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
int main ( int argc, char *argv[] )
{
pthread_mutexattr_t mtx_attr;
pthread_mutex_t mtx; …Run Code Online (Sandbox Code Playgroud) 我是Linux编程的新手,所以请耐心等待.我有2个线程类型执行不同的操作,所以我希望每个都拥有它自己的互斥锁.这是我正在使用的代码,它好吗?如果不是为什么?
static pthread_mutex_t cs_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t cs_mutex2 = PTHREAD_MUTEX_INITALIZER;
void * Thread1(void * lp)
{
int * sock = (int*)lp;
char buffer[2024];
int bytecount = recv(*sock, buffer, 2048, 0);
while (0 == 0)
{
if ((bytecount ==0) || (bytecount == -1))
{
pthread_mutex_lock(&cs_mutex);
//Some uninteresting operations witch plays with set 1 of global variables;
pthread_mutex_unlock(&cs_mutex);
}
}
}
void * Thread2(void * lp)
{
while (0 == 0)
{
pthread_mutex_lock(&cs_mutex2);
//Some uninteresting operations witch plays with some global …Run Code Online (Sandbox Code Playgroud) 我有一个结构
typedef struct something_t {
int a;
int b;
} VALUES;
Run Code Online (Sandbox Code Playgroud)
在我的线程函数中我做
VALUES values;
values.a = 10;
values.b = 11;
pthread_exit((void*)&values);
Run Code Online (Sandbox Code Playgroud)
我试着通过这样做来接受
VALUES values;
pthread_join(thread, (void*)&values);
printf("A: %d\B: %d\n", values.a, values.b);
Run Code Online (Sandbox Code Playgroud)
我收到的价值每次都很奇怪.我很困惑如何接收我最终在线程中创建的值.我试图在C中学习线程,似乎我已经掌握了它,但我无法返回值.有办法吗?感谢任何人的帮助.