- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
(注意:我看到了这个帖子,告诉我是否是同样的问题:C: performance of pthread, low than single thrad)
我正在学习 pthread 库。我写了同一个 C 程序的两个版本。该程序需要一个大列表 BAM files并使用 samtools library 计算记录数.
这是单线程程序:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "bam.h"
/** maximum number of threads */
static const int MAX_COUNT_THREADS=4;
struct Param
{
char* filename;
};
static void printCount(const char* filename,unsigned long count)
{
fprintf(stdout,"%s\t%ld\n",filename,count);
}
static void* scan_bam(void* ptr)
{
unsigned long count=0;
struct Param* params=(struct Param*)ptr;
bamFile in=bam_open(params->filename, "r") ;
bam_header_t *header= NULL;
bam1_t *b=bam_init1();
time_t rawtime;
time ( &rawtime );
fprintf(stderr,"STARTING : %s %s",params->filename,ctime(&rawtime));
if(in==0)
{
fprintf(stderr,"Cannot read %s.\n",params->filename);
exit(EXIT_FAILURE);
}
header= bam_header_read(in);
while((bam_read1(in, b)) > 0)
{
++count;
}
bam_destroy1(b);
bam_header_destroy(header);
bam_close(in);
printCount(params->filename,count);
time ( &rawtime );
fprintf(stderr,"end for %s %s",params->filename,ctime(&rawtime));
free(params);
return NULL;
}
int main(int argc,char** argv)
{
int optind=1;
while(optind<argc)
{
struct Param* params=(struct Param*)malloc(sizeof(struct Param));
if(params==0)
{
fprintf(stderr,"Out of memory.\n");
exit(EXIT_FAILURE);
}
params->filename=argv[optind++];
scan_bam(params);
}
return EXIT_SUCCESS;
}
和多线程程序。该程序最多可以使用 5 个线程,并使用条件锁来计算线程数,并告诉主程序在需要时启动一个新线程。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <time.h>
#include "bam.h"
#define VERIFY_ZERO(a) do {if(a!=0) {\
fprintf(stderr,"Test failed at %s line %d (ret=%d).\n",__FILE__,__LINE__,a);\
exit(EXIT_FAILURE);\
}} while(0)
/** maximum number of threads */
static const int MAX_COUNT_THREADS=5;
struct Param
{
pthread_t thread;
char* filename;
};
struct GLOBALS {
/** lock to print */
pthread_mutex_t mutex_print;
/** condition: wait for free thread */
pthread_cond_t accept_new_thread;
/** condition lock */
pthread_mutex_t accept_new_thread_lock;
/** number of running threads */
int number_of_threads;
};
static struct GLOBALS globals={
PTHREAD_MUTEX_INITIALIZER,
PTHREAD_COND_INITIALIZER,
PTHREAD_MUTEX_INITIALIZER,
0
};
static void printCount(const char* filename,unsigned long count)
{
int ret=pthread_mutex_lock(&globals.mutex_print);
VERIFY_ZERO(ret);
fprintf(stdout,"%s\t%ld\n",filename,count);
ret=pthread_mutex_unlock(&globals.mutex_print);
VERIFY_ZERO(ret);
}
static void* scan_bam(void* ptr)
{
unsigned long count=0;
struct Param* params=(struct Param*)ptr;
bamFile in=bam_open(params->filename, "r") ;
bam_header_t *header= NULL;
bam1_t *b=bam_init1();
time_t rawtime;
time ( &rawtime );
fprintf(stderr,"STARTING : %s %s",params->filename,ctime(&rawtime));
if(in==0)
{
fprintf(stderr,"Cannot read %s.\n",params->filename);
exit(EXIT_FAILURE);
}
header= bam_header_read(in);
while((bam_read1(in, b)) > 0)
{
++count;
}
bam_destroy1(b);
bam_header_destroy(header);
bam_close(in);
printCount(params->filename,count);
time ( &rawtime );
fprintf(stderr,"end1 for %s %s",params->filename,ctime(&rawtime));
pthread_mutex_lock( &globals.accept_new_thread_lock);
globals.number_of_threads--;
pthread_cond_signal(&globals.accept_new_thread);
pthread_mutex_unlock(&globals.accept_new_thread_lock);
time ( &rawtime );
fprintf(stderr,"end2 for %s %s",params->filename,ctime(&rawtime));
free(params);
return NULL;
}
int main(int argc,char** argv)
{
int optind=1;
while(optind<argc)
{
struct Param* params=(struct Param*)malloc(sizeof(struct Param));
if(params==0)
{
fprintf(stderr,"Out of memory.\n");
exit(EXIT_FAILURE);
}
pthread_mutex_lock(&globals.accept_new_thread_lock);
while (globals.number_of_threads > MAX_COUNT_THREADS)
{
pthread_cond_wait(&globals.accept_new_thread, &globals.accept_new_thread_lock);
}
globals.number_of_threads++;
pthread_mutex_unlock(&globals.accept_new_thread_lock);
params->filename=argv[optind++];
fprintf(stderr,"creating %s\n",params->filename);
pthread_create (&(params->thread), NULL, scan_bam,params);
pthread_detach(params->thread);
}
pthread_mutex_lock(&globals.accept_new_thread_lock);
while (globals.number_of_threads > 0)
{
pthread_cond_wait(&globals.accept_new_thread, &globals.accept_new_thread_lock);
}
pthread_mutex_unlock(&globals.accept_new_thread_lock);
pthread_cond_destroy(&globals.accept_new_thread);
return EXIT_SUCCESS;
}
gcc -O3 -Wall jeter.c -pthread -I/usr/local/package/samtools-0.1.18 -L/usr/local/package/samtools-0.1.18/ -lbam -lz
$ time (find .// -name "*recal.bam" | grep Item1[0-9] | xargs ./a.out )
creating ./Item10/recal.bam
creating ./Item11/recal.bam
creating ./Item12/recal.bam
creating ./Item13/recal.bam
creating ./Item14/recal.bam
creating ./Item15/recal.bam
STARTING : ./Item10/recal.bam Tue Dec 18 15:12:48 2012
STARTING : ./Item11/recal.bam Tue Dec 18 15:12:48 2012
STARTING : ./Item12/recal.bam Tue Dec 18 15:12:48 2012
STARTING : ./Item14/recal.bam Tue Dec 18 15:12:48 2012
STARTING : ./Item13/recal.bam Tue Dec 18 15:12:48 2012
STARTING : ./Item15/recal.bam Tue Dec 18 15:12:48 2012
./Item10/recal.bam 185784310
end1 for ./Item10/recal.bam Tue Dec 18 15:38:16 2012
end2 for ./Item10/recal.bam Tue Dec 18 15:38:16 2012
creating ./Item16/recal.bam
STARTING : ./Item16/recal.bam Tue Dec 18 15:38:16 2012
./Item11/recal.bam 204408906
end1 for ./Item11/recal.bam Tue Dec 18 15:41:52 2012
end2 for ./Item11/recal.bam Tue Dec 18 15:41:52 2012
creating ./Item17/recal.bam
STARTING : ./Item17/recal.bam Tue Dec 18 15:41:52 2012
./Item12/recal.bam 207766317
end1 for ./Item12/recal.bam Tue Dec 18 15:42:17 2012
end2 for ./Item12/recal.bam Tue Dec 18 15:42:17 2012
creating ./Item18/recal.bam
STARTING : ./Item18/recal.bam Tue Dec 18 15:42:17 2012
./Item15/recal.bam 224957522
end1 for ./Item15/recal.bam Tue Dec 18 15:44:54 2012
end2 for ./Item15/recal.bam Tue Dec 18 15:44:54 2012
creating ./Item19/recal.bam
STARTING : ./Item19/recal.bam Tue Dec 18 15:44:54 2012
./Item13/recal.bam 224548326
end1 for ./Item13/recal.bam Tue Dec 18 15:45:32 2012
end2 for ./Item13/recal.bam Tue Dec 18 15:45:32 2012
./Item14/recal.bam 241267346
end1 for ./Item14/recal.bam Tue Dec 18 15:48:28 2012
end2 for ./Item14/recal.bam Tue Dec 18 15:48:28 2012
./Item16/recal.bam 227446579
end1 for ./Item16/recal.bam Tue Dec 18 16:12:15 2012
end2 for ./Item16/recal.bam Tue Dec 18 16:12:15 2012
./Item17/recal.bam 215307379
end1 for ./Item17/recal.bam Tue Dec 18 16:13:05 2012
end2 for ./Item17/recal.bam Tue Dec 18 16:13:05 2012
./Item18/recal.bam 225914723
end1 for ./Item18/recal.bam Tue Dec 18 16:13:48 2012
end2 for ./Item18/recal.bam Tue Dec 18 16:13:48 2012
./Item19/recal.bam 225509630
end1 for ./Item19/recal.bam Tue Dec 18 16:14:06 2012
end2 for ./Item19/recal.bam Tue Dec 18 16:14:06 2012
.
real 61m17.560s
user 66m0.476s
sys 4m5.980s
$ gcc -O3 -Wall jeter2.c -I/usr/local/package/samtools-0.1.18 -L/usr/local/package/samtools-0.1.18/ -lbam -lz
time (find .// -name "*recal.bam" | grep Item1[0-9] | xargs ./a.out )
STARTING : ./Item10/recal.bam Tue Dec 18 16:15:25 2012
./Item10/recal.bam 185784310
end for ./Item10/recal.bam Tue Dec 18 16:20:43 2012
STARTING : ./Item11/recal.bam Tue Dec 18 16:20:43 2012
./Item11/recal.bam 204408906
end for ./Item11/recal.bam Tue Dec 18 16:26:20 2012
STARTING : ./Item12/recal.bam Tue Dec 18 16:26:20 2012
./Item12/recal.bam 207766317
end for ./Item12/recal.bam Tue Dec 18 16:31:56 2012
STARTING : ./Item13/recal.bam Tue Dec 18 16:31:56 2012
./Item13/recal.bam 224548326
end for ./Item13/recal.bam Tue Dec 18 16:38:05 2012
STARTING : ./Item14/recal.bam Tue Dec 18 16:38:05 2012
./Item14/recal.bam 241267346
end for ./Item14/recal.bam Tue Dec 18 16:44:59 2012
STARTING : ./Item15/recal.bam Tue Dec 18 16:44:59 2012
./Item15/recal.bam 224957522
end for ./Item15/recal.bam Tue Dec 18 16:50:56 2012
STARTING : ./Item16/recal.bam Tue Dec 18 16:50:56 2012
./Item16/recal.bam 227446579
end for ./Item16/recal.bam Tue Dec 18 16:58:07 2012
STARTING : ./Item17/recal.bam Tue Dec 18 16:58:07 2012
./Item17/recal.bam 215307379
end for ./Item17/recal.bam Tue Dec 18 17:04:58 2012
STARTING : ./Item18/recal.bam Tue Dec 18 17:04:58 2012
./Item18/recal.bam 225914723
end for ./Item18/recal.bam Tue Dec 18 17:11:31 2012
STARTING : ./Item19/recal.bam Tue Dec 18 17:11:31 2012
./Item19/recal.bam 225509630
end for ./Item19/recal.bam Tue Dec 18 17:18:19 2012
.
real 62m54.503s
user 53m39.529s
sys 3m44.580s
这两个程序已经运行了 ~1H00。所以 MT 程序比另一个运行得慢。为什么 ?是否可以加速该代码?
最佳答案
看起来 I/O 操作(从文件中读取)在您的程序中占主导地位,因此您很可能不会从线程中获得太多好处,无论它做得多么好。
另请注意,多线程变体实际上要快一点;您需要比较实时。由于多线程,用户时间 更大,因为它总结了所有线程在用户模式下花费的时间。 内核时间也是如此。
关于c - 库 pthread : single threaded performance vs multithreaded,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13948457/
完成此任务的最佳方法是什么:主线程(线程 A)创建另外两个线程(线程 B 和线程 C)。线程 B 和 C 执行繁重的磁盘 I/O,最终需要将它们创建的资源传递给线程 A,然后调用外部 DLL 文件中的
我是一名对 Julia 语言感兴趣的新手程序员。文档( https://docs.julialang.org/en/v1/base/multi-threading/ )说 Threads.@threa
python中的thread.start_new_thread和threading.Thread.start有什么区别? 我注意到,当调用 start_new_thread 时,新线程会在调用线程终止
我正在学习安卓蓝牙编程。我从 Google 的 Android 开发者网站上复制了大部分代码以供学习。这个想法是监听服务器上的连接是在一个新线程中完成的,而不会阻塞 UI 线程。当收到连接请求时,连接
执行对象方法的线程是否依赖于创建它的线程上的线程? 假设您的 java 应用程序中有两个线程 Thread1 和 Thread2,以及两个类 ClassA 和 ClassB。 您在 Thread1 上
我正在用这段代码测试 C++11 线程,但是在创建线程时,我遇到了错误没有匹配函数调用 'std::thread::thread()'. 这就像我给 std::thread ctr 的函数有什么问题,
我有如下类 eventEngine 和网关: class eventEngine { public: eventEngine(); std::thread threa; std
我需要运行与列表 dirlist 中的元素一样多的 Observer 类线程。当我运行它 python 控制台时,它可以正常工作。 class Observer(Thread): def ru
我在一本 Java 书中读到了下面的代码。我知道主类默认继承 Thread 类,所以 currentThread();而不是 Thread.currentThread();也会做这项工作。 但我不明白
我在我的系统中使用第 3 方 API,该 API 启动一个永久运行的用户线程。一旦我的程序结束,JVM 由于该线程而继续运行,因此我尝试获取此线程引用并通过更改它 thread.setDaemon(t
我所知道的 Python 中的每个对象都可以通过调用来处理其基类初始化: super(BaseClass, self).__init__() threading.Thread 的子类似乎不是这种情况,
在我最近从事的 Xamarin 项目中,我可以看到开发人员使用了 Java.Lang.Thread 以及 System.Threading.Thread(用于非常相似的操作 - 例如在后台加载数据)。
我在 Julia 中运行双循环。代码非常简单。 w = rand(1000,1000) function regular_demo(w::Array{Float64, 2}) n = size
我在 Windows 上使用 Python 3。我正在使用 threading.Thread动态运行一个函数,我可以带参数或不带参数调用它。我正在设置一个列表,其中的第一项是定义路径的字符串。其他参数
我遇到了一些使用线程模块(使用 Python 2.6)管理线程的示例。 我想了解的是这个例子是如何调用“运行”方法的,在哪里调用的。我在任何地方都看不到它。 ThreadUrl 类在 main() 函
假设我从 threading.Thread 派生: from threading import Thread class Worker(Thread): def start(self):
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
使用 WinDbg 和 SOS,我有以下内容: 0:011> !threads ThreadCount: 7 UnstartedThread: 0 BackgroundThread: 4 Pendin
App Engine 给出错误: com.google.apphosting.api.ApiProxy$CallNotFoundException: Can't make API call urlfe
我正在尝试将 Swing JEditorPane 嵌入到 JavaFX 项目中,如下代码所示。 Platform.runLater(() -> { SyntaxTester ob = new
我是一名优秀的程序员,十分优秀!