Tag: multithreading

当前的C11实施状态()?

我很好奇C11实现的状态是什么,特别是关于可选的 。 目前是否有任何平台支持这些接口? 如果没有,是否有任何计划在POSIX和/或WinAPI方面实现接口? 是否有其他人计划使用C11中描述的接口( thrd_t , mtx_t , cond_t等等)?

每个进程的最大线程数 – sysconf(_SC_THREAD_THREADS_MAX)失败

我试图在UNIX机器上找到每个进程的最大线程数,并编写下面的代码以使用sysconf: #include #include #include #include #include int main() { errno = 0; long maxThreads = sysconf(_SC_THREAD_THREADS_MAX); if (maxThreads == -1 && errno == 0) { printf(“the variable corresponding to _SC_THREAD_THREADS_MAX ” “is associated with functionality that is not ” “supported by the system\n”); exit(1); } if (maxThreads == -1) { printf(“errno: %d\n”, errno); exit(1); } […]

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 #include #include 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, NULL, […]

C线程打印数字序列:偶数和奇数打印线程并行运行

我是multithreading编程的新手。 我尝试使用偶数和奇数打印线程打印数字序列,并行运行。 执行时,代码进入死锁状态。 任何人都可以帮我解决这个问题。 #include #include pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t even, odd; void printfun1(void *pnt); void printfun2(void *pnt); main() { pthread_t pthread1, pthread2; int ret1, ret2; char *message = “thread 1”; char *message2 = “thread 2”; ret1 = pthread_create(&pthread1, NULL, printfun1, (void *)message); if(ret1) { printf(“thread creation failed”); } ret2 = pthread_create(&pthread2, NULL, printfun2,(void*) […]

Java在C中易变?

我知道在Java中使用volatile 。 那是(基于维基百科的文章 ): 对volatile变量的读写有一个全局排序。 这意味着访问volatile字段的每个线程将在继续之前读取其当前值,而不是(可能)使用缓存值。 我也知道在C中存在volatile关键字,但在完全不同的上下文中,主要用于内存映射I / O. 所以我想知道,是否有一些像Java在C中volatile构造? 哪个会阻止读取变量的缓存值? 如果它不存在于C中,是否有一个带有这样的结构的库,比如pthread ?

thrd_busy和mtx_lock()/ mtx_timedlock()

我有关于C1x互斥量的以下问题(§7.25.4): 在哪种情况下mtx_lock()返回thrd_busy而不是阻塞? 在哪些情况下mtx_timedlock()返回thrd_busy ? 请注意, thrd_busy在§7.25.1¶5中定义为“ 当测试和返回函数请求的资源已被使用时 ”。 我希望thrd_busy只能由mtx_trylock()返回,或者当用mtx_try或mtx_try | mtx_recursive调用时,最多也只能由mtx_trylock()返回mtx_try | mtx_recursive mtx_try | mtx_recursive互斥锁,但绝对不是来自mtx_timedlock() ,它需要一个支持超时的互斥锁,即mtx_timed或mtx_timed | mtx_recursive mtx_timed | mtx_recursive互斥。 这只是草案中的公正和疏忽吗? 或者我错过了什么?

并发访问和没有数据结构

问题是这样的: 我有一个500指针的数组,指向双链表中的500个元素。 有10个线程并行运行。 每个线程运行50个循环,并尝试释放列表中的某些元素。 列表已排序(包含简单整数),并且还有10个其他线程并行运行,搜索包含特定整数的节点并访问此节点中的其他卫星数据。 所以节点就像: struct node { int key; // Key used to search this nodes int x,y,z; // Satellite data struct node *prev; struct node *right; }; 如果我只是在搜索/删除之前锁定列表,则问题很容易解决。 但这太粗糙了。 我如何同步这些线程,以便我可以实现更好的并发? 编辑: 这不是一个家庭作业问题。 我不属于学术界。 持有500个指针的数组看起来很奇怪。 我已经做到了这样,以尽可能少的复杂性来形象化我的问题。

将工作分成更multithreading需要更多时间,为什么?

我有一个小的C程序,它使用monte-carlo模拟计算pi ,它基本上只测试一个随机点[x,y],如果它在圆圈内部或外部。 为了近似pi,我必须使用大量的样本n ,其具有O(n)的直接比例复杂度。 因此,尝试计算大量样本n,我实现了POSIX线程 api以平衡计算能力。 我的代码如下所示: pthread_t worker[nthreads]; /* creates workers for each thread */ struct param aparam[nthreads]; /* struct param{ long* hits; long rounds; }; */ long nrounds = nsamples / nthreads; /* divide samples to subsets of equal rounds per thread */ for (int i = 0; i < nthreads; ++i) { […]

C OMP omp_get_wtime()返回时间0.00

我使用了omp_get_wtime(),但是当我想打印时间总是得到0.00时,问题出在哪里? #define SIZE 500 #define nthreads 10 (…) void sumTab(int mX[][SIZE], int mY[][SIZE], int mZ[][SIZE]) { int i,k; double start = omp_get_wtime(); #pragma omp parallel for schedule(dynamic,3) private(i) num_threads(nthreads) for(i=0 ; i<SIZE ; i++) { for(k=0 ; k<SIZE ; k++) { mZ[i][k]=mX[i][k]+mY[i][k]; printf("Thread no %d \t [%d] [%d] result: %d\n", omp_get_thread_num(),i,k, mZ[i][k]); } } printf("Time: […]

在c99中使用__thread

我想使用__thread存储类将一些变量定义为特定于线程的。 但是三个问题让我犹豫不决: 它真的是c99的标准吗? 或者更重要的是,编译器支持有多好? 变量是否会在每个线程中初始化? 非multithreading程序是否将它们视为普通的全局变量?